001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.mina.http;
021
022import static org.junit.Assert.*;
023
024import java.util.List;
025import java.util.Map;
026
027import org.apache.mina.http.api.HttpMethod;
028import org.apache.mina.http.api.HttpRequest;
029import org.apache.mina.http.api.HttpVersion;
030import org.junit.Test;
031
032public class HttpRequestImplTestCase {
033
034    @Test
035    public void testGetParameterNoParameter() {
036        HttpRequest req = new HttpRequestImpl(HttpVersion.HTTP_1_1, HttpMethod.GET, "/","", null);
037        assertNull("p0 doesn't exist", req.getParameter("p0"));
038    }
039
040    @Test
041    public void testGetParameterOneEmptyParameter() {
042        HttpRequest req = new HttpRequestImpl(HttpVersion.HTTP_1_1, HttpMethod.GET, "/", "p0=", null);
043        assertEquals("p0 is emtpy", "", req.getParameter("p0"));
044        assertNull("p1 doesn't exist", req.getParameter("p1"));
045    }
046
047    @Test
048    public void testGetParameterOneParameter() {
049        HttpRequest req = new HttpRequestImpl(HttpVersion.HTTP_1_1, HttpMethod.GET, "/", "p0=0", null);
050        assertEquals("p0 is '0'", "0", req.getParameter("p0"));
051        assertNull("p1 doesn't exist", req.getParameter("p1"));
052    }
053
054    @Test
055    public void testGetParameter3Parameters() {
056        HttpRequest req = new HttpRequestImpl(HttpVersion.HTTP_1_1, HttpMethod.GET, "/", "p0=&p1=1&p2=2", null);
057        assertEquals("p0 is emtpy", "", req.getParameter("p0"));
058        assertEquals("p1 is '1'", "1", req.getParameter("p1"));
059        assertEquals("p2 is '2'", "2", req.getParameter("p2"));
060        assertNull("p3 doesn't exist", req.getParameter("p3"));
061    }
062
063    @Test
064    public void testGetParametersNoParameter() {
065        HttpRequest req = new HttpRequestImpl(HttpVersion.HTTP_1_1, HttpMethod.GET, "/", "", null);
066        assertTrue("Empty Map", req.getParameters().isEmpty());
067    }
068
069    @Test
070    public void testGetParameters3Parameters() {
071        HttpRequest req = new HttpRequestImpl(HttpVersion.HTTP_1_1, HttpMethod.GET, "/","p0=&p1=1&p2=2", null);
072        Map<String, List<String>> parameters = req.getParameters();
073        assertEquals("3 parameters", 3, parameters.size());
074        assertEquals("one p0", 1, parameters.get("p0").size());
075        assertEquals("p0 is emtpy", "", parameters.get("p0").get(0));
076        assertEquals("one p1", 1, parameters.get("p1").size());
077        assertEquals("p1 is '1'", "1", parameters.get("p1").get(0));
078        assertEquals("one p2", 1, parameters.get("p2").size());
079        assertEquals("p2 is '2'", "2", parameters.get("p2").get(0));
080    }
081
082    @Test
083    public void testGetParameters3ParametersWithDuplicate() {
084        HttpRequest req = new HttpRequestImpl(HttpVersion.HTTP_1_1, HttpMethod.GET, "/","p0=&p1=1&p0=2", null);
085        Map<String, List<String>> parameters = req.getParameters();
086        assertEquals("2 parameters", 2, parameters.size());
087        assertEquals("two p0", 2, parameters.get("p0").size());
088        assertEquals("1st p0 is emtpy", "", parameters.get("p0").get(0));
089        assertEquals("2nd p0 is '2'", "2", parameters.get("p0").get(1));
090        assertEquals("one p1", 1, parameters.get("p1").size());
091        assertEquals("p1 is '1'", "1", parameters.get("p1").get(0));
092        assertNull("No p2", parameters.get("p2"));
093    }
094
095}