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