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