View Javadoc

1   package org.apache.mina.http;
2   
3   import static org.junit.Assert.*;
4   
5   import java.util.List;
6   import java.util.Map;
7   
8   import org.apache.mina.http.api.HttpMethod;
9   import org.apache.mina.http.api.HttpRequest;
10  import org.apache.mina.http.api.HttpVersion;
11  import org.junit.Test;
12  
13  public class HttpRequestImplTestCase {
14  
15  	@Test
16  	public void testGetParameterNoParameter() {
17  		HttpRequest req = new HttpRequestImpl(HttpVersion.HTTP_1_1, HttpMethod.GET, "/","", null);
18  		assertNull("p0 doesn't exist", req.getParameter("p0"));
19  	}
20  
21  	@Test
22  	public void testGetParameterOneEmptyParameter() {
23  		HttpRequest req = new HttpRequestImpl(HttpVersion.HTTP_1_1, HttpMethod.GET, "/", "p0=", null);
24  		assertEquals("p0 is emtpy", "", req.getParameter("p0"));
25  		assertNull("p1 doesn't exist", req.getParameter("p1"));
26  	}
27  
28  	@Test
29  	public void testGetParameterOneParameter() {
30  		HttpRequest req = new HttpRequestImpl(HttpVersion.HTTP_1_1, HttpMethod.GET, "/", "p0=0", null);
31  		assertEquals("p0 is '0'", "0", req.getParameter("p0"));
32  		assertNull("p1 doesn't exist", req.getParameter("p1"));
33  	}
34  
35  	@Test
36  	public void testGetParameter3Parameters() {
37  		HttpRequest req = new HttpRequestImpl(HttpVersion.HTTP_1_1, HttpMethod.GET, "/", "p0=&p1=1&p2=2", null);
38  		assertEquals("p0 is emtpy", "", req.getParameter("p0"));
39  		assertEquals("p1 is '1'", "1", req.getParameter("p1"));
40  		assertEquals("p2 is '2'", "2", req.getParameter("p2"));
41  		assertNull("p3 doesn't exist", req.getParameter("p3"));
42  	}
43  
44  	@Test
45  	public void testGetParametersNoParameter() {
46  		HttpRequest req = new HttpRequestImpl(HttpVersion.HTTP_1_1, HttpMethod.GET, "/", "", null);
47  		assertTrue("Empty Map", req.getParameters().isEmpty());
48  	}
49  
50  	@Test
51  	public void testGetParameters3Parameters() {
52  		HttpRequest req = new HttpRequestImpl(HttpVersion.HTTP_1_1, HttpMethod.GET, "/","p0=&p1=1&p2=2", null);
53  		Map<String, List<String>> parameters = req.getParameters();
54  		assertEquals("3 parameters", 3, parameters.size());
55  		assertEquals("one p0", 1, parameters.get("p0").size());
56  		assertEquals("p0 is emtpy", "", parameters.get("p0").get(0));
57  		assertEquals("one p1", 1, parameters.get("p1").size());
58  		assertEquals("p1 is '1'", "1", parameters.get("p1").get(0));
59  		assertEquals("one p2", 1, parameters.get("p2").size());
60  		assertEquals("p2 is '2'", "2", parameters.get("p2").get(0));
61  	}
62  
63  	@Test
64  	public void testGetParameters3ParametersWithDuplicate() {
65  		HttpRequest req = new HttpRequestImpl(HttpVersion.HTTP_1_1, HttpMethod.GET, "/","p0=&p1=1&p0=2", null);
66  		Map<String, List<String>> parameters = req.getParameters();
67  		assertEquals("2 parameters", 2, parameters.size());
68  		assertEquals("two p0", 2, parameters.get("p0").size());
69  		assertEquals("1st p0 is emtpy", "", parameters.get("p0").get(0));
70  		assertEquals("2nd p0 is '2'", "2", parameters.get("p0").get(1));
71  		assertEquals("one p1", 1, parameters.get("p1").size());
72  		assertEquals("p1 is '1'", "1", parameters.get("p1").get(0));
73  		assertNull("No p2", parameters.get("p2"));
74  	}
75  
76  }