View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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  }