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.http.protocol;
29  
30  import org.apache.http.HttpRequest;
31  import org.apache.http.message.BasicHttpRequest;
32  import org.junit.Assert;
33  import org.junit.Test;
34  import org.mockito.Mockito;
35  
36  public class TestUriHttpRequestHandlerMapper {
37  
38      @Test
39      public void testRegisterUnregister() throws Exception {
40          final HttpRequestHandler h = Mockito.mock(HttpRequestHandler.class);
41  
42          final UriPatternMatcher<HttpRequestHandler> matcher = Mockito.spy(new UriPatternMatcher<HttpRequestHandler>());
43          final UriHttpRequestHandlerMapper registry = new UriHttpRequestHandlerMapper(matcher);
44  
45          registry.register("/h1", h);
46          registry.unregister("/h1");
47  
48          Mockito.verify(matcher).register("/h1", h);
49          Mockito.verify(matcher).unregister("/h1");
50      }
51  
52      @Test
53      public void testLookup() throws Exception {
54          final UriPatternMatcher<HttpRequestHandler> matcher = Mockito.spy(new UriPatternMatcher<HttpRequestHandler>());
55          final UriHttpRequestHandlerMapper registry = new UriHttpRequestHandlerMapper(matcher);
56  
57          final HttpRequest request = new BasicHttpRequest("GET", "/");
58          registry.lookup(request);
59          registry.unregister("/h1");
60  
61          Mockito.verify(matcher).lookup("/");
62      }
63  
64      @Test(expected=IllegalArgumentException.class)
65      public void testRegisterNull() throws Exception {
66          final UriHttpRequestHandlerMapper registry = new UriHttpRequestHandlerMapper();
67          registry.register(null, null);
68      }
69  
70      @Test(expected=IllegalArgumentException.class)
71      public void testLookupNull() throws Exception {
72          final UriHttpRequestHandlerMapper registry = new UriHttpRequestHandlerMapper();
73          registry.register(null, null);
74      }
75  
76      @Test
77      public void testWildCardMatchingWithQuery() throws Exception {
78          final HttpRequestHandler h1 = Mockito.mock(HttpRequestHandler.class);
79          final HttpRequestHandler h2 = Mockito.mock(HttpRequestHandler.class);
80          final HttpRequestHandler def = Mockito.mock(HttpRequestHandler.class);
81  
82          final UriPatternMatcher<HttpRequestHandler> matcher = Mockito.spy(new UriPatternMatcher<HttpRequestHandler>());
83          final UriHttpRequestHandlerMapper registry = new UriHttpRequestHandlerMapper(matcher);
84          registry.register("*", def);
85          registry.register("*.view", h1);
86          registry.register("*.form", h2);
87  
88          HttpRequestHandler h;
89  
90          h = registry.lookup(new BasicHttpRequest("GET", "/that.view?param=value"));
91          Assert.assertNotNull(h);
92          Assert.assertTrue(h1 == h);
93  
94          h = registry.lookup(new BasicHttpRequest("GET", "/that.form?whatever"));
95          Assert.assertNotNull(h);
96          Assert.assertTrue(h2 == h);
97  
98          h = registry.lookup(new BasicHttpRequest("GET", "/whatever"));
99          Assert.assertNotNull(h);
100         Assert.assertTrue(def == h);
101     }
102 
103 }