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.hc.core5.http.protocol;
29
30 import org.apache.hc.core5.function.Supplier;
31 import org.apache.hc.core5.http.HttpHost;
32 import org.apache.hc.core5.http.Method;
33 import org.apache.hc.core5.http.MisdirectedRequestException;
34 import org.apache.hc.core5.http.message.BasicHttpRequest;
35 import org.junit.Assert;
36 import org.junit.Before;
37 import org.junit.Test;
38
39 public class TestRequestHandlerRegistry {
40
41 private RequestHandlerRegistry<String> handlerRegistry;
42 private HttpContext context;
43
44 @Before
45 public void setUp() {
46 handlerRegistry = new RequestHandlerRegistry<>("myhost", new Supplier<LookupRegistry<String>>() {
47
48 @Override
49 public LookupRegistry<String> get() {
50 return new UriPatternMatcher<>();
51 }
52
53 });
54 context = new BasicHttpContext();
55 }
56
57 @Test
58 public void testResolveByRequestUri() throws Exception {
59 handlerRegistry.register(null, "/test*", "stuff");
60 Assert.assertNotEquals("stuff", handlerRegistry.resolve(new BasicHttpRequest(Method.GET, "/"), context));
61 Assert.assertNotEquals("stuff", handlerRegistry.resolve(new BasicHttpRequest(Method.GET, "/abc"), context));
62 Assert.assertEquals("stuff", handlerRegistry.resolve(new BasicHttpRequest(Method.GET, "/test"), context));
63 Assert.assertEquals("stuff", handlerRegistry.resolve(new BasicHttpRequest(Method.GET, "/testabc"), context));
64 }
65
66 @Test
67 public void testByRequestUriWithQuery() throws Exception {
68 handlerRegistry.register(null, "/test*", "stuff");
69 Assert.assertEquals("stuff", handlerRegistry.resolve(new BasicHttpRequest(Method.GET, "/test?test=a"), context));
70 Assert.assertNotEquals("stuff", handlerRegistry.resolve(new BasicHttpRequest(Method.GET, "/tes?test=a"), context));
71 }
72
73 @Test
74 public void testResolveByHostnameAndRequestUri() throws Exception {
75 handlerRegistry.register("myhost", "/test*", "stuff");
76 Assert.assertEquals("stuff", handlerRegistry.resolve(new BasicHttpRequest(Method.GET, new HttpHost("myhost"), "/test"), context));
77 Assert.assertEquals("stuff", handlerRegistry.resolve(new BasicHttpRequest(Method.GET, new HttpHost("MyHost"), "/testabc"), context));
78 }
79
80 @Test
81 public void testResolveByLocalhostAndRequestUri() throws Exception {
82 handlerRegistry.register("myhost", "/test*", "stuff");
83 Assert.assertEquals("stuff", handlerRegistry.resolve(new BasicHttpRequest(Method.GET, new HttpHost("localhost"), "/test"), context));
84 Assert.assertEquals("stuff", handlerRegistry.resolve(new BasicHttpRequest(Method.GET, new HttpHost("LocalHost"), "/testabc"), context));
85 }
86
87 @Test(expected = MisdirectedRequestException.class)
88 public void testResolveByUnknownHostname() throws Exception {
89 handlerRegistry.register("myhost", "/test*", "stuff");
90 handlerRegistry.resolve(new BasicHttpRequest(Method.GET, new HttpHost("otherhost"), "/test"), context);
91 }
92
93 }