/src/test/me/fumiz/test/nano/cases/functions/PostTest.java

https://github.com/fumiz/NanoHTTPd · Java · 94 lines · 75 code · 14 blank · 5 comment · 0 complexity · 597869aa83d203ce6bce73efff2ebd84 MD5 · raw file

  1. package me.fumiz.test.nano.cases.functions;
  2. import me.fumiz.nano.NanoHTTPd;
  3. import me.fumiz.test.nano.lib.NanoHTTPdTestCase;
  4. import org.apache.http.HttpResponse;
  5. import org.apache.http.NameValuePair;
  6. import org.apache.http.client.ResponseHandler;
  7. import org.apache.http.client.entity.UrlEncodedFormEntity;
  8. import org.apache.http.client.methods.HttpPost;
  9. import org.apache.http.message.BasicNameValuePair;
  10. import org.apache.http.util.EntityUtils;
  11. import java.io.IOException;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.Properties;
  15. /**
  16. * User: fumiz
  17. * Date: 11/09/25
  18. * Time: 14:53
  19. */
  20. public class PostTest extends NanoHTTPdTestCase {
  21. public void testSimplePost() throws IOException {
  22. HttpPost request;
  23. String response;
  24. request = new HttpPost(getTestUrl("/post.php"));
  25. List<NameValuePair> params = new ArrayList<NameValuePair>();
  26. params.add(new BasicNameValuePair("foo","body"));
  27. params.add(new BasicNameValuePair("multibyte","外语的字符串"));
  28. params.add(new BasicNameValuePair("multilinecrlf","\r\n\r\n外\r\n语\r\n\r\n的\r\n字\r\n符\r\n\r\n串\r\n\r\n\r\n"));
  29. params.add(new BasicNameValuePair("multilinelf","\n\nマルチ\nバイト\n文字列\n改行込みテストLF\n\n"));
  30. params.add(new BasicNameValuePair("multilinecr","\rマルチ\rバイト\r文字列\r改行込みテストCR\r\r"));
  31. request.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
  32. response = execute(new TestRequestHandler() {
  33. public NanoHTTPd.Response onRequest(String uri, String method, Properties header, Properties parms, Properties files) {
  34. assertEquals("/post.php", uri);
  35. assertEquals("POST", method);
  36. assertEquals(5, parms.size());
  37. assertEquals(0, files.size());
  38. assertEquals("body", parms.getProperty("foo"));
  39. assertEquals("外语的字符串", parms.getProperty("multibyte"));
  40. assertEquals("\r\n\r\n外\r\n语\r\n\r\n的\r\n字\r\n符\r\n\r\n串\r\n\r\n\r\n", parms.getProperty("multilinecrlf"));
  41. assertEquals("\n\nマルチ\nバイト\n文字列\n改行込みテストLF\n\n", parms.getProperty("multilinelf"));
  42. assertEquals("\rマルチ\rバイト\r文字列\r改行込みテストCR\r\r", parms.getProperty("multilinecr"));
  43. return createSimpleResponse("");
  44. }
  45. }, request, new ResponseHandler<String>() {
  46. public String handleResponse(HttpResponse httpResponse) throws IOException {
  47. assertEquals(200, httpResponse.getStatusLine().getStatusCode());
  48. assertEquals("OK", httpResponse.getStatusLine().getReasonPhrase());
  49. return EntityUtils.toString(httpResponse.getEntity());
  50. }
  51. });
  52. assertEquals("", response);
  53. }
  54. public void testPostResponse() throws IOException {
  55. HttpPost request;
  56. String response;
  57. request = new HttpPost(getTestUrl("/post.php"));
  58. List<NameValuePair> params = new ArrayList<NameValuePair>();
  59. params.add(new BasicNameValuePair("foo","body"));
  60. request.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
  61. response = execute(new TestRequestHandler() {
  62. public NanoHTTPd.Response onRequest(String uri, String method, Properties header, Properties parms, Properties files) {
  63. assertEquals("/post.php", uri);
  64. assertEquals("POST", method);
  65. assertEquals(1, parms.size());
  66. assertEquals(0, files.size());
  67. assertEquals("body", parms.getProperty("foo"));
  68. return new NanoHTTPd.Response(NanoHTTPd.HTTP_OK, NanoHTTPd.MIME_PLAINTEXT, "\n多\n字节\n\n进行反应的确认\n\n");
  69. }
  70. }, request, new ResponseHandler<String>() {
  71. public String handleResponse(HttpResponse httpResponse) throws IOException {
  72. assertEquals(200, httpResponse.getStatusLine().getStatusCode());
  73. assertEquals("OK", httpResponse.getStatusLine().getReasonPhrase());
  74. assertEquals("text/plain", httpResponse.getHeaders("Content-Type")[0].getValue());
  75. return EntityUtils.toString(httpResponse.getEntity(), "utf-8");
  76. }
  77. });
  78. assertEquals("\n多\n字节\n\n进行反应的确认\n\n", response);
  79. }
  80. }