16964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawkepackage fi.iki.elonen.integration; 26964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 38b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie/* 48b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * #%L 58b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * NanoHttpd-Core 68b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * %% 78b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * Copyright (C) 2012 - 2015 nanohttpd 88b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * %% 98b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * Redistribution and use in source and binary forms, with or without modification, 108b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * are permitted provided that the following conditions are met: 119e1ec7bff40d70d31953a04dd448665aaf549395ritchie * 128b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * 1. Redistributions of source code must retain the above copyright notice, this 138b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * list of conditions and the following disclaimer. 149e1ec7bff40d70d31953a04dd448665aaf549395ritchie * 158b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * 2. Redistributions in binary form must reproduce the above copyright notice, 168b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * this list of conditions and the following disclaimer in the documentation 178b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * and/or other materials provided with the distribution. 189e1ec7bff40d70d31953a04dd448665aaf549395ritchie * 198b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * 3. Neither the name of the nanohttpd nor the names of its contributors 208b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * may be used to endorse or promote products derived from this software without 218b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * specific prior written permission. 229e1ec7bff40d70d31953a04dd448665aaf549395ritchie * 238b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 248b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 258b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 268b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 278b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 288b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 298b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 308b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 318b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 328b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * OF THE POSSIBILITY OF SUCH DAMAGE. 338b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie * #L% 348b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie */ 358b3f33ce61812b29a5cacd1e7e6d350b40531f9britchie 365e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchieimport static org.junit.Assert.assertEquals; 375e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie 38a86bc4b8d23f1d481ea7781f86b88eeb2e3e2546ritchieimport java.nio.charset.Charset; 395e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchieimport java.util.ArrayList; 405e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchieimport java.util.Collections; 415e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchieimport java.util.List; 425e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchieimport java.util.Map; 435e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie 446b302d1735579ac02f327b87cc99fdbfea09f61britchieimport org.apache.http.HttpEntity; 456b302d1735579ac02f327b87cc99fdbfea09f61britchieimport org.apache.http.HttpResponse; 466964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawkeimport org.apache.http.NameValuePair; 476964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawkeimport org.apache.http.client.ResponseHandler; 486964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawkeimport org.apache.http.client.entity.UrlEncodedFormEntity; 496964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawkeimport org.apache.http.client.methods.HttpGet; 506964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawkeimport org.apache.http.client.methods.HttpPost; 516964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawkeimport org.apache.http.entity.mime.HttpMultipartMode; 526964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawkeimport org.apache.http.entity.mime.MultipartEntity; 536964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawkeimport org.apache.http.entity.mime.content.StringBody; 546964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawkeimport org.apache.http.impl.client.BasicResponseHandler; 556964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawkeimport org.apache.http.message.BasicNameValuePair; 566b302d1735579ac02f327b87cc99fdbfea09f61britchieimport org.apache.http.util.EntityUtils; 576964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawkeimport org.junit.Test; 586964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 595e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchieimport fi.iki.elonen.NanoHTTPD; 606b302d1735579ac02f327b87cc99fdbfea09f61britchieimport fi.iki.elonen.NanoHTTPD.Response.Status; 616964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 626964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke/** 639058464950a9734da0a7ff2dc47f3081bbb5117critchie * @author Paul S. Hawke (paul.hawke@gmail.com) On: 5/19/13 at 5:36 PM 646964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke */ 655820f308631a825a05feddddd0aafb962f272b9bPaul Hawkepublic class GetAndPostIntegrationTest extends IntegrationTestBase<GetAndPostIntegrationTest.TestServer> { 666964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 675e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie public static class TestServer extends NanoHTTPD { 686964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 695e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie public String response; 706964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 715e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie public TestServer() { 725e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie super(8192); 735e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie } 746964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 755e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie @Override 765e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie public Response serve(String uri, Method method, Map<String, String> header, Map<String, String> parms, Map<String, String> files) { 775e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie StringBuilder sb = new StringBuilder(String.valueOf(method) + ':' + this.response); 786964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 795e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie if (parms.size() > 1) { 805e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie parms.remove("NanoHttpd.QUERY_STRING"); 815e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie sb.append("-params=").append(parms.size()); 825e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie List<String> p = new ArrayList<String>(parms.keySet()); 835e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie Collections.sort(p); 845e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie for (String k : p) { 855e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie sb.append(';').append(k).append('=').append(parms.get(k)); 865e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie } 875e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie } 886b302d1735579ac02f327b87cc99fdbfea09f61britchie if ("/chin".equals(uri)) { 896b302d1735579ac02f327b87cc99fdbfea09f61britchie return newFixedLengthResponse(Status.OK, "application/octet-stream", sb.toString()); 906b302d1735579ac02f327b87cc99fdbfea09f61britchie } else { 916b302d1735579ac02f327b87cc99fdbfea09f61britchie return newFixedLengthResponse(sb.toString()); 926b302d1735579ac02f327b87cc99fdbfea09f61britchie } 935e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie } 945e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie } 955e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie 965e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie @Override 975e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie public TestServer createTestServer() { 985e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie return new TestServer(); 996964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke } 1006964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 1016964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke @Test 1025e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie public void testGetRequestWithParameters() throws Exception { 1035e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie this.testServer.response = "testGetRequestWithParameters"; 1046964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 1055e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie HttpGet httpget = new HttpGet("http://localhost:8192/?age=120&gender=Male"); 1066964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke ResponseHandler<String> responseHandler = new BasicResponseHandler(); 1075e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie String responseBody = this.httpclient.execute(httpget, responseHandler); 1086964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 1095e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie assertEquals("GET:testGetRequestWithParameters-params=2;age=120;gender=Male", responseBody); 1106964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke } 1116964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 1126964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke @Test 1136964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke public void testPostRequestWithFormEncodedParameters() throws Exception { 1145e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie this.testServer.response = "testPostRequestWithFormEncodedParameters"; 1156964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 1162cbbb4d4eb8dea11a87bc60ff9672558fc290486Paul Hawke HttpPost httppost = new HttpPost("http://localhost:8192/"); 1176964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke List<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 1186964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke postParameters.add(new BasicNameValuePair("age", "120")); 1196964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke postParameters.add(new BasicNameValuePair("gender", "Male")); 1206964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke httppost.setEntity(new UrlEncodedFormEntity(postParameters)); 1216964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 1226964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke ResponseHandler<String> responseHandler = new BasicResponseHandler(); 1235e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie String responseBody = this.httpclient.execute(httppost, responseHandler); 1246964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 1256964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke assertEquals("POST:testPostRequestWithFormEncodedParameters-params=2;age=120;gender=Male", responseBody); 1266964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke } 1276964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 1286964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke @Test 1296964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke public void testPostRequestWithMultipartEncodedParameters() throws Exception { 1305e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie this.testServer.response = "testPostRequestWithMultipartEncodedParameters"; 1316964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 1322cbbb4d4eb8dea11a87bc60ff9672558fc290486Paul Hawke HttpPost httppost = new HttpPost("http://localhost:8192/"); 1336964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 1346964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke reqEntity.addPart("age", new StringBody("120")); 1356964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke reqEntity.addPart("gender", new StringBody("Male")); 1366964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke httppost.setEntity(reqEntity); 1376964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 1386964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke ResponseHandler<String> responseHandler = new BasicResponseHandler(); 1395e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie String responseBody = this.httpclient.execute(httppost, responseHandler); 1406964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 1416964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke assertEquals("POST:testPostRequestWithMultipartEncodedParameters-params=2;age=120;gender=Male", responseBody); 1426964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke } 1436964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 1445e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie @Test 1455e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie public void testPostWithNoParameters() throws Exception { 1465e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie this.testServer.response = "testPostWithNoParameters"; 1479058464950a9734da0a7ff2dc47f3081bbb5117critchie 1485e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie HttpPost httppost = new HttpPost("http://localhost:8192/"); 1495e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie ResponseHandler<String> responseHandler = new BasicResponseHandler(); 1505e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie String responseBody = this.httpclient.execute(httppost, responseHandler); 1516964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 1525e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie assertEquals("POST:testPostWithNoParameters", responseBody); 1535e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie } 1546964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 1555e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie @Test 1565e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie public void testSimpleGetRequest() throws Exception { 1575e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie this.testServer.response = "testSimpleGetRequest"; 1586964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 1595e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie HttpGet httpget = new HttpGet("http://localhost:8192/"); 1605e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie ResponseHandler<String> responseHandler = new BasicResponseHandler(); 1615e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie String responseBody = this.httpclient.execute(httpget, responseHandler); 1626964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke 1635e2e2f19e06b9f4a01c6da83a2297eb18fe2b546ritchie assertEquals("GET:testSimpleGetRequest", responseBody); 1646964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke } 165a86bc4b8d23f1d481ea7781f86b88eeb2e3e2546ritchie 166a86bc4b8d23f1d481ea7781f86b88eeb2e3e2546ritchie @Test 167a86bc4b8d23f1d481ea7781f86b88eeb2e3e2546ritchie public void testPostRequestWithMultipartExtremEncodedParameters() throws Exception { 168a86bc4b8d23f1d481ea7781f86b88eeb2e3e2546ritchie this.testServer.response = "testPostRequestWithMultipartEncodedParameters"; 169a86bc4b8d23f1d481ea7781f86b88eeb2e3e2546ritchie 1706b302d1735579ac02f327b87cc99fdbfea09f61britchie HttpPost httppost = new HttpPost("http://localhost:8192/chin"); 171a86bc4b8d23f1d481ea7781f86b88eeb2e3e2546ritchie MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "sfsadfasdf", Charset.forName("UTF-8")); 172a86bc4b8d23f1d481ea7781f86b88eeb2e3e2546ritchie reqEntity.addPart("specialString", new StringBody("拖拉图片到浏览器,可以实现预览功能", "text/plain", Charset.forName("UTF-8"))); 173a86bc4b8d23f1d481ea7781f86b88eeb2e3e2546ritchie reqEntity.addPart("gender", new StringBody("图片名称", Charset.forName("UTF-8")) { 174a86bc4b8d23f1d481ea7781f86b88eeb2e3e2546ritchie 175a86bc4b8d23f1d481ea7781f86b88eeb2e3e2546ritchie @Override 176a86bc4b8d23f1d481ea7781f86b88eeb2e3e2546ritchie public String getFilename() { 177a86bc4b8d23f1d481ea7781f86b88eeb2e3e2546ritchie return "图片名称"; 178a86bc4b8d23f1d481ea7781f86b88eeb2e3e2546ritchie } 179a86bc4b8d23f1d481ea7781f86b88eeb2e3e2546ritchie }); 180a86bc4b8d23f1d481ea7781f86b88eeb2e3e2546ritchie httppost.setEntity(reqEntity); 1816b302d1735579ac02f327b87cc99fdbfea09f61britchie HttpResponse response = this.httpclient.execute(httppost); 182a86bc4b8d23f1d481ea7781f86b88eeb2e3e2546ritchie 1836b302d1735579ac02f327b87cc99fdbfea09f61britchie HttpEntity entity = response.getEntity(); 1846b302d1735579ac02f327b87cc99fdbfea09f61britchie String responseBody = EntityUtils.toString(entity, "UTF-8"); 185a86bc4b8d23f1d481ea7781f86b88eeb2e3e2546ritchie 1866b302d1735579ac02f327b87cc99fdbfea09f61britchie assertEquals("POST:testPostRequestWithMultipartEncodedParameters-params=2;gender=图片名称;specialString=拖拉图片到浏览器,可以实现预览功能", responseBody); 187a86bc4b8d23f1d481ea7781f86b88eeb2e3e2546ritchie } 1886964c6f930efb87083c36bbcd1f9298c7246d280Paul Hawke} 189