@Test public void testBasicPathHanding() throws IOException { TestHttpClient client = new TestHttpClient(); try { final PathHandler handler = new PathHandler(); handler.addPath("a", new RemainingPathHandler("/a")); handler.addPath("/aa", new RemainingPathHandler("/aa")); handler.addPath("/aa/anotherSubPath", new RemainingPathHandler("/aa/anotherSubPath")); final PathHandler sub = new PathHandler(); handler.addPath("/path", sub); sub.addPath("/subpath", new RemainingPathHandler("/subpath")); sub.setDefaultHandler(new RemainingPathHandler("/path")); DefaultServer.setRootHandler(handler); HttpGet get = new HttpGet(DefaultServer.getDefaultServerAddress() + "/notamatchingpath"); HttpResponse result = client.execute(get); Assert.assertEquals(404, result.getStatusLine().getStatusCode()); HttpClientUtils.readResponse(result); get = new HttpGet(DefaultServer.getDefaultServerAddress() + "/"); result = client.execute(get); Assert.assertEquals(404, result.getStatusLine().getStatusCode()); HttpClientUtils.readResponse(result); runPathTest(client, "/path", "/path", ""); runPathTest(client, "/path/a", "/path", "/a"); runPathTest(client, "/path/subpath", "/subpath", ""); runPathTest(client, "/path/subpath/", "/subpath", "/"); runPathTest(client, "/path/subpath/foo", "/subpath", "/foo"); runPathTest(client, "/a", "/a", ""); runPathTest(client, "/aa/anotherSubPath", "/aa/anotherSubPath", ""); runPathTest(client, "/aa/anotherSubPath/bob", "/aa/anotherSubPath", "/bob"); runPathTest(client, "/aa?a=b", "/aa", "", Collections.singletonMap("a", "b")); } finally { client.getConnectionManager().shutdown(); } }
@Test public void testFileBasedErrorPageIsGenerated() throws IOException { DefaultHttpClient client = new DefaultHttpClient(); try { final FileErrorPageHandler handler = new FileErrorPageHandler( new File(getClass().getResource("errorpage.html").getFile()), 404); DefaultServer.setRootHandler(handler); HttpGet get = new HttpGet(DefaultServer.getDefaultServerAddress() + "/path"); HttpResponse result = client.execute(get); Assert.assertEquals(404, result.getStatusLine().getStatusCode()); final String response = HttpClientUtils.readResponse(result); Assert.assertTrue(response, response.contains("Custom Error Page")); } finally { client.getConnectionManager().shutdown(); } }
private void runPathTest( TestHttpClient client, String path, String expectedMatch, String expectedRemaining, Map<String, String> queryParams) throws IOException { HttpResponse result; HttpGet get = new HttpGet(DefaultServer.getDefaultServerAddress() + path); result = client.execute(get); Assert.assertEquals(200, result.getStatusLine().getStatusCode()); Header[] header = result.getHeaders(MATCHED); Assert.assertEquals(expectedMatch, header[0].getValue()); header = result.getHeaders(PATH); Assert.assertEquals(expectedRemaining, header[0].getValue()); HttpClientUtils.readResponse(result); for (Map.Entry<String, String> entry : queryParams.entrySet()) { header = result.getHeaders(entry.getKey()); Assert.assertEquals(entry.getValue(), header[0].getValue()); } }