View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.mina.http;
21  
22  import static org.junit.Assert.*;
23  
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.mina.http.api.HttpMethod;
28  import org.apache.mina.http.api.HttpRequest;
29  import org.apache.mina.http.api.HttpVersion;
30  import org.junit.Test;
31  
32  public class HttpRequestImplTestCase {
33  
34      @Test
35      public void testGetParameterNoParameter() {
36          HttpRequest req = new HttpRequestImpl(HttpVersion.HTTP_1_1, HttpMethod.GET, "/","", null);
37          assertNull("p0 doesn't exist", req.getParameter("p0"));
38      }
39  
40      @Test
41      public void testGetParameterOneEmptyParameter() {
42          HttpRequest req = new HttpRequestImpl(HttpVersion.HTTP_1_1, HttpMethod.GET, "/", "p0=", null);
43          assertEquals("p0 is emtpy", "", req.getParameter("p0"));
44          assertNull("p1 doesn't exist", req.getParameter("p1"));
45      }
46  
47      @Test
48      public void testGetParameterOneParameter() {
49          HttpRequest req = new HttpRequestImpl(HttpVersion.HTTP_1_1, HttpMethod.GET, "/", "p0=0", null);
50          assertEquals("p0 is '0'", "0", req.getParameter("p0"));
51          assertNull("p1 doesn't exist", req.getParameter("p1"));
52      }
53  
54      @Test
55      public void testGetParameter3Parameters() {
56          HttpRequest req = new HttpRequestImpl(HttpVersion.HTTP_1_1, HttpMethod.GET, "/", "p0=&p1=1&p2=2", null);
57          assertEquals("p0 is emtpy", "", req.getParameter("p0"));
58          assertEquals("p1 is '1'", "1", req.getParameter("p1"));
59          assertEquals("p2 is '2'", "2", req.getParameter("p2"));
60          assertNull("p3 doesn't exist", req.getParameter("p3"));
61      }
62  
63      @Test
64      public void testGetParametersNoParameter() {
65          HttpRequest req = new HttpRequestImpl(HttpVersion.HTTP_1_1, HttpMethod.GET, "/", "", null);
66          assertTrue("Empty Map", req.getParameters().isEmpty());
67      }
68  
69      @Test
70      public void testGetParameters3Parameters() {
71          HttpRequest req = new HttpRequestImpl(HttpVersion.HTTP_1_1, HttpMethod.GET, "/","p0=&p1=1&p2=2", null);
72          Map<String, List<String>> parameters = req.getParameters();
73          assertEquals("3 parameters", 3, parameters.size());
74          assertEquals("one p0", 1, parameters.get("p0").size());
75          assertEquals("p0 is emtpy", "", parameters.get("p0").get(0));
76          assertEquals("one p1", 1, parameters.get("p1").size());
77          assertEquals("p1 is '1'", "1", parameters.get("p1").get(0));
78          assertEquals("one p2", 1, parameters.get("p2").size());
79          assertEquals("p2 is '2'", "2", parameters.get("p2").get(0));
80      }
81  
82      @Test
83      public void testGetParameters3ParametersWithDuplicate() {
84          HttpRequest req = new HttpRequestImpl(HttpVersion.HTTP_1_1, HttpMethod.GET, "/","p0=&p1=1&p0=2", null);
85          Map<String, List<String>> parameters = req.getParameters();
86          assertEquals("2 parameters", 2, parameters.size());
87          assertEquals("two p0", 2, parameters.get("p0").size());
88          assertEquals("1st p0 is emtpy", "", parameters.get("p0").get(0));
89          assertEquals("2nd p0 is '2'", "2", parameters.get("p0").get(1));
90          assertEquals("one p1", 1, parameters.get("p1").size());
91          assertEquals("p1 is '1'", "1", parameters.get("p1").get(0));
92          assertNull("No p2", parameters.get("p2"));
93      }
94  
95  }