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