@Test public void testLookup() throws Exception { final UriPatternMatcher<HttpRequestHandler> matcher = Mockito.spy(new UriPatternMatcher<HttpRequestHandler>()); final UriHttpRequestHandlerMapper registry = new UriHttpRequestHandlerMapper(matcher); final HttpRequest request = new BasicHttpRequest("GET", "/"); registry.lookup(request); registry.unregister("/h1"); Mockito.verify(matcher).lookup("/"); }
@Test public void testRegisterUnregister() throws Exception { final HttpRequestHandler h = Mockito.mock(HttpRequestHandler.class); final UriPatternMatcher<HttpRequestHandler> matcher = Mockito.spy(new UriPatternMatcher<HttpRequestHandler>()); final UriHttpRequestHandlerMapper registry = new UriHttpRequestHandlerMapper(matcher); registry.register("/h1", h); registry.unregister("/h1"); Mockito.verify(matcher).register("/h1", h); Mockito.verify(matcher).unregister("/h1"); }
/** * Tests that an abort called after a redirect has found a new host still aborts in the correct * place (while trying to get the new host's route, not while doing the subsequent request). */ @Test public void testAbortAfterRedirectedRoute() throws Exception { final UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper(); this.serverBootstrap.setHandlerMapper(reqistry); final CountDownLatch connLatch = new CountDownLatch(1); final CountDownLatch awaitLatch = new CountDownLatch(1); final ConnMan4 conMan = new ConnMan4(connLatch, awaitLatch); final AtomicReference<Throwable> throwableRef = new AtomicReference<Throwable>(); final CountDownLatch getLatch = new CountDownLatch(1); this.clientBuilder.setConnectionManager(conMan); final HttpContext context = new BasicHttpContext(); final HttpGet httpget = new HttpGet("a"); final HttpHost target = start(); reqistry.register("*", new BasicRedirectService(target.getPort())); new Thread( new Runnable() { @Override public void run() { try { final HttpHost host = new HttpHost("127.0.0.1", target.getPort()); httpclient.execute(host, httpget, context); } catch (final Throwable t) { throwableRef.set(t); } finally { getLatch.countDown(); } } }) .start(); Assert.assertTrue( "should have tried to get a connection", connLatch.await(1, TimeUnit.SECONDS)); httpget.abort(); Assert.assertTrue("should have finished get request", getLatch.await(1, TimeUnit.SECONDS)); Assert.assertTrue( "should be instanceof IOException, was: " + throwableRef.get(), throwableRef.get() instanceof IOException); Assert.assertTrue( "cause should be InterruptedException, was: " + throwableRef.get().getCause(), throwableRef.get().getCause() instanceof InterruptedException); }
@Test public void testWildCardMatchingWithQuery() throws Exception { final HttpRequestHandler h1 = Mockito.mock(HttpRequestHandler.class); final HttpRequestHandler h2 = Mockito.mock(HttpRequestHandler.class); final HttpRequestHandler def = Mockito.mock(HttpRequestHandler.class); final UriPatternMatcher<HttpRequestHandler> matcher = Mockito.spy(new UriPatternMatcher<HttpRequestHandler>()); final UriHttpRequestHandlerMapper registry = new UriHttpRequestHandlerMapper(matcher); registry.register("*", def); registry.register("*.view", h1); registry.register("*.form", h2); HttpRequestHandler h; h = registry.lookup(new BasicHttpRequest("GET", "/that.view?param=value")); Assert.assertNotNull(h); Assert.assertTrue(h1 == h); h = registry.lookup(new BasicHttpRequest("GET", "/that.form?whatever")); Assert.assertNotNull(h); Assert.assertTrue(h2 == h); h = registry.lookup(new BasicHttpRequest("GET", "/whatever")); Assert.assertNotNull(h); Assert.assertTrue(def == h); }
/** * Unregister a given path. Further requests to paths matching the specified pattern will result * in a 404 being delivered to the client * * @param pattern The pattern to unregister. Must have previously been registered via {@link * #register(String, org.apache.http.protocol.HttpRequestHandler)} */ public void unregister(String pattern) { registry.unregister(pattern); registry.unregister(pattern + "/"); }
/** * Register a path with a handler * * @param pattern The path to register * @param handler The handler to handle the path * @see {@link org.apache.http.protocol.UriHttpRequestHandlerMapper} */ public void register(String pattern, HttpRequestHandler handler) { registry.register(pattern, handler); registry.register(pattern + "/", handler); }
@Test(expected = IllegalArgumentException.class) public void testLookupNull() throws Exception { final UriHttpRequestHandlerMapper registry = new UriHttpRequestHandlerMapper(); registry.register(null, null); }