### Question:
ServletUtil { public static Uri validateUrl(Uri urlToValidate) throws GadgetException { if (urlToValidate == null) { throw new GadgetException(GadgetException.Code.MISSING_PARAMETER, "Missing url param", HttpResponse.SC_BAD_REQUEST); } UriBuilder url = new UriBuilder(urlToValidate); if (!"http".equals(url.getScheme()) && !"https".equals(url.getScheme())) { throw new GadgetException(GadgetException.Code.INVALID_PARAMETER, "Invalid request url scheme in url: " + Utf8UrlCoder.encode(urlToValidate.toString()) + "; only \"http\" and \"https\" supported.", HttpResponse.SC_BAD_REQUEST); } if (url.getPath() == null || url.getPath().length() == 0) { url.setPath("/"); } return url.toUri(); } private  ServletUtil(); static HttpRequest fromHttpServletRequest(HttpServletRequest servletReq); static void setCachingHeaders(HttpResponseBuilder response, int ttl, boolean noProxy); static void copyResponseToServlet(HttpResponse response, HttpServletResponse servletResponse); static Uri validateUrl(Uri urlToValidate); static void setXForwardedForHeader(HttpRequest inboundRequest, HttpRequest req); static void setXForwardedForHeader(HttpServletRequest inboundRequest, HttpRequest req); static HttpResponse errorResponse(GadgetException e); static HttpResponse convertToJsonResponse(HttpResponse response); static final String REMOTE_ADDR_KEY; static final String DATA_URI_KEY; }### Answer:
@Test public void testValidateUrlNoPath() throws Exception { Uri url = ServletUtil.validateUrl(Uri.parse("http: assertEquals("http", url.getScheme()); assertEquals("www.example.com", url.getAuthority()); assertEquals("/", url.getPath()); assertNull(url.getQuery()); assertNull(url.getFragment()); }
@Test public void testValidateUrlHttps() throws Exception { Uri url = ServletUtil.validateUrl(Uri.parse("https: assertEquals("https", url.getScheme()); assertEquals("www.example.com", url.getAuthority()); assertEquals("/", url.getPath()); assertNull(url.getQuery()); assertNull(url.getFragment()); }
@Test public void testValidateUrlWithPath() throws Exception { Uri url = ServletUtil.validateUrl(Uri.parse("http: assertEquals("http", url.getScheme()); assertEquals("www.example.com", url.getAuthority()); assertEquals("/foo", url.getPath()); assertNull(url.getQuery()); assertNull(url.getFragment()); }
@Test public void testValidateUrlWithPort() throws Exception { Uri url = ServletUtil.validateUrl(Uri.parse("http: assertEquals("http", url.getScheme()); assertEquals("www.example.com:8080", url.getAuthority()); assertEquals("/foo", url.getPath()); assertNull(url.getQuery()); assertNull(url.getFragment()); }
@Test public void testValidateUrlWithEncodedPath() throws Exception { Uri url = ServletUtil.validateUrl(Uri.parse("http: assertEquals("http", url.getScheme()); assertEquals("www.example.com", url.getAuthority()); assertEquals("/foo%20bar", url.getPath()); assertNull(url.getQuery()); assertNull(url.getFragment()); }
@Test public void testValidateUrlWithEncodedQuery() throws Exception { Uri url = ServletUtil.validateUrl(Uri.parse("http: assertEquals("http", url.getScheme()); assertEquals("www.example.com", url.getAuthority()); assertEquals("/foo", url.getPath()); assertEquals("q=with%20space", url.getQuery()); assertEquals("with space", url.getQueryParameter("q")); assertNull(url.getFragment()); }
@Test public void testValidateUrlWithNoPathAndEncodedQuery() throws Exception { Uri url = ServletUtil.validateUrl(Uri.parse("http: assertEquals("http", url.getScheme()); assertEquals("www.example.com", url.getAuthority()); assertEquals("/", url.getPath()); assertEquals("q=with%20space", url.getQuery()); assertNull(url.getFragment()); }
@Test(expected = GadgetException.class) public void testValidateUrlNullInput() throws Exception { ServletUtil.validateUrl(null); }