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.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          Assert.assertEquals("stuff", handlerRegistry.resolve(new BasicHttpRequest(Method.GET, new HttpHost("127.0.0.1"), "/testabc"), context));
86      }
87  
88      @Test(expected = MisdirectedRequestException.class)
89      public void testResolveByUnknownHostname() throws Exception {
90          handlerRegistry.register("myhost", "/test*", "stuff");
91          handlerRegistry.resolve(new BasicHttpRequest(Method.GET, new HttpHost("otherhost"), "/test"), context);
92      }
93  
94  }