1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.http.nio.protocol;
29
30 import org.apache.http.HttpRequest;
31 import org.apache.http.message.BasicHttpRequest;
32 import org.apache.http.protocol.UriPatternMatcher;
33 import org.junit.Assert;
34 import org.junit.Test;
35 import org.mockito.Mockito;
36
37 public class TestUriHttpAsyncRequestHandlerMapper {
38
39 @Test
40 public void testRegisterUnregister() throws Exception {
41 final HttpAsyncRequestHandler<?> h = Mockito.mock(HttpAsyncRequestHandler.class);
42
43 final UriPatternMatcher<HttpAsyncRequestHandler<?>> matcher = Mockito.spy(
44 new UriPatternMatcher<HttpAsyncRequestHandler<?>>());
45 final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper(matcher);
46
47 registry.register("/h1", h);
48 registry.unregister("/h1");
49
50 Mockito.verify(matcher).register("/h1", h);
51 Mockito.verify(matcher).unregister("/h1");
52 }
53
54 @Test
55 public void testLookup() throws Exception {
56 final UriPatternMatcher<HttpAsyncRequestHandler<?>> matcher = Mockito.spy(
57 new UriPatternMatcher<HttpAsyncRequestHandler<?>>());
58 final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper(matcher);
59
60 final HttpRequest request = new BasicHttpRequest("GET", "/");
61 registry.lookup(request);
62 registry.unregister("/h1");
63
64 Mockito.verify(matcher).lookup("/");
65 }
66
67 @Test(expected=IllegalArgumentException.class)
68 public void testRegisterNull() throws Exception {
69 final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
70 registry.register(null, null);
71 }
72
73 @Test(expected=IllegalArgumentException.class)
74 public void testLookupNull() throws Exception {
75 final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper();
76 registry.register(null, null);
77 }
78
79 @Test
80 public void testWildCardMatchingWithQuery() throws Exception {
81 final HttpAsyncRequestHandler<?> h1 = Mockito.mock(HttpAsyncRequestHandler.class);
82 final HttpAsyncRequestHandler<?> h2 = Mockito.mock(HttpAsyncRequestHandler.class);
83 final HttpAsyncRequestHandler<?> def = Mockito.mock(HttpAsyncRequestHandler.class);
84
85 final UriPatternMatcher<HttpAsyncRequestHandler<?>> matcher = Mockito.spy(
86 new UriPatternMatcher<HttpAsyncRequestHandler<?>>());
87 final UriHttpAsyncRequestHandlerMapper registry = new UriHttpAsyncRequestHandlerMapper(matcher);
88 registry.register("*", def);
89 registry.register("*.view", h1);
90 registry.register("*.form", h2);
91
92 HttpAsyncRequestHandler<?> h;
93
94 h = registry.lookup(new BasicHttpRequest("GET", "/that.view?param=value"));
95 Assert.assertNotNull(h);
96 Assert.assertTrue(h1 == h);
97
98 h = registry.lookup(new BasicHttpRequest("GET", "/that.form?whatever"));
99 Assert.assertNotNull(h);
100 Assert.assertTrue(h2 == h);
101
102 h = registry.lookup(new BasicHttpRequest("GET", "/whatever"));
103 Assert.assertNotNull(h);
104 Assert.assertTrue(def == h);
105 }
106
107 }