1 package fi.iki.elonen.integration; 2 3 import fi.iki.elonen.NanoHTTPD; 4 import org.apache.http.NameValuePair; 5 import org.apache.http.client.HttpClient; 6 import org.apache.http.client.ResponseHandler; 7 import org.apache.http.client.entity.UrlEncodedFormEntity; 8 import org.apache.http.client.methods.HttpGet; 9 import org.apache.http.client.methods.HttpPost; 10 import org.apache.http.entity.mime.HttpMultipartMode; 11 import org.apache.http.entity.mime.MultipartEntity; 12 import org.apache.http.entity.mime.content.StringBody; 13 import org.apache.http.impl.client.BasicResponseHandler; 14 import org.apache.http.impl.client.DefaultHttpClient; 15 import org.apache.http.message.BasicNameValuePair; 16 import org.junit.After; 17 import org.junit.Before; 18 import org.junit.Test; 19 20 import java.io.IOException; 21 import java.util.ArrayList; 22 import java.util.Collections; 23 import java.util.List; 24 import java.util.Map; 25 26 import static org.junit.Assert.assertEquals; 27 28 /** 29 * @author Paul S. Hawke (paul.hawke (at) gmail.com) 30 * On: 5/19/13 at 5:36 PM 31 */ 32 public class GetAndPostIntegrationTest extends IntegrationTestBase<GetAndPostIntegrationTest.TestServer> { 33 34 @Test 35 public void testSimpleGetRequest() throws Exception { 36 testServer.response = "testSimpleGetRequest"; 37 38 HttpGet httpget = new HttpGet("http://localhost:8192/"); 39 ResponseHandler<String> responseHandler = new BasicResponseHandler(); 40 String responseBody = httpclient.execute(httpget, responseHandler); 41 42 assertEquals("GET:testSimpleGetRequest", responseBody); 43 } 44 45 @Test 46 public void testGetRequestWithParameters() throws Exception { 47 testServer.response = "testGetRequestWithParameters"; 48 49 HttpGet httpget = new HttpGet("http://localhost:8192/?age=120&gender=Male"); 50 ResponseHandler<String> responseHandler = new BasicResponseHandler(); 51 String responseBody = httpclient.execute(httpget, responseHandler); 52 53 assertEquals("GET:testGetRequestWithParameters-params=2;age=120;gender=Male", responseBody); 54 } 55 56 @Test 57 public void testPostWithNoParameters() throws Exception { 58 testServer.response = "testPostWithNoParameters"; 59 60 HttpPost httppost = new HttpPost("http://localhost:8192/"); 61 ResponseHandler<String> responseHandler = new BasicResponseHandler(); 62 String responseBody = httpclient.execute(httppost, responseHandler); 63 64 assertEquals("POST:testPostWithNoParameters", responseBody); 65 } 66 67 @Test 68 public void testPostRequestWithFormEncodedParameters() throws Exception { 69 testServer.response = "testPostRequestWithFormEncodedParameters"; 70 71 HttpPost httppost = new HttpPost("http://localhost:8192/"); 72 List<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 73 postParameters.add(new BasicNameValuePair("age", "120")); 74 postParameters.add(new BasicNameValuePair("gender", "Male")); 75 httppost.setEntity(new UrlEncodedFormEntity(postParameters)); 76 77 ResponseHandler<String> responseHandler = new BasicResponseHandler(); 78 String responseBody = httpclient.execute(httppost, responseHandler); 79 80 assertEquals("POST:testPostRequestWithFormEncodedParameters-params=2;age=120;gender=Male", responseBody); 81 } 82 83 @Test 84 public void testPostRequestWithMultipartEncodedParameters() throws Exception { 85 testServer.response = "testPostRequestWithMultipartEncodedParameters"; 86 87 HttpPost httppost = new HttpPost("http://localhost:8192/"); 88 MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 89 reqEntity.addPart("age", new StringBody("120")); 90 reqEntity.addPart("gender", new StringBody("Male")); 91 httppost.setEntity(reqEntity); 92 93 ResponseHandler<String> responseHandler = new BasicResponseHandler(); 94 String responseBody = httpclient.execute(httppost, responseHandler); 95 96 assertEquals("POST:testPostRequestWithMultipartEncodedParameters-params=2;age=120;gender=Male", responseBody); 97 } 98 99 @Override public TestServer createTestServer() { 100 return new TestServer(); 101 } 102 103 public static class TestServer extends NanoHTTPD { 104 public String response; 105 106 public TestServer() { 107 super(8192); 108 } 109 110 @Override 111 public Response serve(String uri, Method method, Map<String, String> header, Map<String, String> parms, Map<String, String> files) { 112 StringBuilder sb = new StringBuilder(String.valueOf(method) + ':' + response); 113 114 if (parms.size() > 1) { 115 parms.remove("NanoHttpd.QUERY_STRING"); 116 sb.append("-params=").append(parms.size()); 117 List<String> p = new ArrayList<String>(parms.keySet()); 118 Collections.sort(p); 119 for (String k : p) { 120 sb.append(';').append(k).append('=').append(parms.get(k)); 121 } 122 } 123 124 return new Response(sb.toString()); 125 } 126 } 127 } 128