Home | History | Annotate | Download | only in integration
      1 package fi.iki.elonen.integration;
      2 
      3 /*
      4  * #%L
      5  * NanoHttpd-Core
      6  * %%
      7  * Copyright (C) 2012 - 2015 nanohttpd
      8  * %%
      9  * Redistribution and use in source and binary forms, with or without modification,
     10  * are permitted provided that the following conditions are met:
     11  *
     12  * 1. Redistributions of source code must retain the above copyright notice, this
     13  *    list of conditions and the following disclaimer.
     14  *
     15  * 2. Redistributions in binary form must reproduce the above copyright notice,
     16  *    this list of conditions and the following disclaimer in the documentation
     17  *    and/or other materials provided with the distribution.
     18  *
     19  * 3. Neither the name of the nanohttpd nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software without
     21  *    specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     31  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
     32  * OF THE POSSIBILITY OF SUCH DAMAGE.
     33  * #L%
     34  */
     35 
     36 import static org.junit.Assert.assertEquals;
     37 
     38 import java.nio.charset.Charset;
     39 import java.util.ArrayList;
     40 import java.util.Collections;
     41 import java.util.List;
     42 import java.util.Map;
     43 
     44 import org.apache.http.HttpEntity;
     45 import org.apache.http.HttpResponse;
     46 import org.apache.http.NameValuePair;
     47 import org.apache.http.client.ResponseHandler;
     48 import org.apache.http.client.entity.UrlEncodedFormEntity;
     49 import org.apache.http.client.methods.HttpGet;
     50 import org.apache.http.client.methods.HttpPost;
     51 import org.apache.http.entity.mime.HttpMultipartMode;
     52 import org.apache.http.entity.mime.MultipartEntity;
     53 import org.apache.http.entity.mime.content.StringBody;
     54 import org.apache.http.impl.client.BasicResponseHandler;
     55 import org.apache.http.message.BasicNameValuePair;
     56 import org.apache.http.util.EntityUtils;
     57 import org.junit.Test;
     58 
     59 import fi.iki.elonen.NanoHTTPD;
     60 import fi.iki.elonen.NanoHTTPD.Response.Status;
     61 
     62 /**
     63  * @author Paul S. Hawke (paul.hawke (at) gmail.com) On: 5/19/13 at 5:36 PM
     64  */
     65 public class GetAndPostIntegrationTest extends IntegrationTestBase<GetAndPostIntegrationTest.TestServer> {
     66 
     67     public static class TestServer extends NanoHTTPD {
     68 
     69         public String response;
     70 
     71         public TestServer() {
     72             super(8192);
     73         }
     74 
     75         @Override
     76         public Response serve(String uri, Method method, Map<String, String> header, Map<String, String> parms, Map<String, String> files) {
     77             StringBuilder sb = new StringBuilder(String.valueOf(method) + ':' + this.response);
     78 
     79             if (parms.size() > 1) {
     80                 parms.remove("NanoHttpd.QUERY_STRING");
     81                 sb.append("-params=").append(parms.size());
     82                 List<String> p = new ArrayList<String>(parms.keySet());
     83                 Collections.sort(p);
     84                 for (String k : p) {
     85                     sb.append(';').append(k).append('=').append(parms.get(k));
     86                 }
     87             }
     88             if ("/chin".equals(uri)) {
     89                 return newFixedLengthResponse(Status.OK, "application/octet-stream", sb.toString());
     90             } else {
     91                 return newFixedLengthResponse(sb.toString());
     92             }
     93         }
     94     }
     95 
     96     @Override
     97     public TestServer createTestServer() {
     98         return new TestServer();
     99     }
    100 
    101     @Test
    102     public void testGetRequestWithParameters() throws Exception {
    103         this.testServer.response = "testGetRequestWithParameters";
    104 
    105         HttpGet httpget = new HttpGet("http://localhost:8192/?age=120&gender=Male");
    106         ResponseHandler<String> responseHandler = new BasicResponseHandler();
    107         String responseBody = this.httpclient.execute(httpget, responseHandler);
    108 
    109         assertEquals("GET:testGetRequestWithParameters-params=2;age=120;gender=Male", responseBody);
    110     }
    111 
    112     @Test
    113     public void testPostRequestWithFormEncodedParameters() throws Exception {
    114         this.testServer.response = "testPostRequestWithFormEncodedParameters";
    115 
    116         HttpPost httppost = new HttpPost("http://localhost:8192/");
    117         List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
    118         postParameters.add(new BasicNameValuePair("age", "120"));
    119         postParameters.add(new BasicNameValuePair("gender", "Male"));
    120         httppost.setEntity(new UrlEncodedFormEntity(postParameters));
    121 
    122         ResponseHandler<String> responseHandler = new BasicResponseHandler();
    123         String responseBody = this.httpclient.execute(httppost, responseHandler);
    124 
    125         assertEquals("POST:testPostRequestWithFormEncodedParameters-params=2;age=120;gender=Male", responseBody);
    126     }
    127 
    128     @Test
    129     public void testPostRequestWithMultipartEncodedParameters() throws Exception {
    130         this.testServer.response = "testPostRequestWithMultipartEncodedParameters";
    131 
    132         HttpPost httppost = new HttpPost("http://localhost:8192/");
    133         MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    134         reqEntity.addPart("age", new StringBody("120"));
    135         reqEntity.addPart("gender", new StringBody("Male"));
    136         httppost.setEntity(reqEntity);
    137 
    138         ResponseHandler<String> responseHandler = new BasicResponseHandler();
    139         String responseBody = this.httpclient.execute(httppost, responseHandler);
    140 
    141         assertEquals("POST:testPostRequestWithMultipartEncodedParameters-params=2;age=120;gender=Male", responseBody);
    142     }
    143 
    144     @Test
    145     public void testPostWithNoParameters() throws Exception {
    146         this.testServer.response = "testPostWithNoParameters";
    147 
    148         HttpPost httppost = new HttpPost("http://localhost:8192/");
    149         ResponseHandler<String> responseHandler = new BasicResponseHandler();
    150         String responseBody = this.httpclient.execute(httppost, responseHandler);
    151 
    152         assertEquals("POST:testPostWithNoParameters", responseBody);
    153     }
    154 
    155     @Test
    156     public void testSimpleGetRequest() throws Exception {
    157         this.testServer.response = "testSimpleGetRequest";
    158 
    159         HttpGet httpget = new HttpGet("http://localhost:8192/");
    160         ResponseHandler<String> responseHandler = new BasicResponseHandler();
    161         String responseBody = this.httpclient.execute(httpget, responseHandler);
    162 
    163         assertEquals("GET:testSimpleGetRequest", responseBody);
    164     }
    165 
    166     @Test
    167     public void testPostRequestWithMultipartExtremEncodedParameters() throws Exception {
    168         this.testServer.response = "testPostRequestWithMultipartEncodedParameters";
    169 
    170         HttpPost httppost = new HttpPost("http://localhost:8192/chin");
    171         MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "sfsadfasdf", Charset.forName("UTF-8"));
    172         reqEntity.addPart("specialString", new StringBody("", "text/plain", Charset.forName("UTF-8")));
    173         reqEntity.addPart("gender", new StringBody("", Charset.forName("UTF-8")) {
    174 
    175             @Override
    176             public String getFilename() {
    177                 return "";
    178             }
    179         });
    180         httppost.setEntity(reqEntity);
    181         HttpResponse response = this.httpclient.execute(httppost);
    182 
    183         HttpEntity entity = response.getEntity();
    184         String responseBody = EntityUtils.toString(entity, "UTF-8");
    185 
    186         assertEquals("POST:testPostRequestWithMultipartEncodedParameters-params=2;gender=;specialString=", responseBody);
    187     }
    188 }
    189