### Question:
Http { public Response post(String url, Map<String, String> headers, HttpEntity httpEntity, boolean allowRedirect) throws Exception { HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection(); InputStream in = null; urlConnection.setInstanceFollowRedirects(allowRedirect); urlConnection.setRequestProperty(httpEntity.getContentType().getName(), httpEntity.getContentType().getValue()); urlConnection.setRequestMethod("POST"); urlConnection.setDoOutput(true); setCommonProperties(headers, urlConnection, allowRedirect); try { DataOutputStream wr = new DataOutputStream( urlConnection.getOutputStream()); BufferedInputStream bis = new BufferedInputStream(httpEntity.getContent()); byte[] buffer = new byte[1024]; int len = 0; while ((len = bis.read(buffer)) != -1) { wr.write(buffer, 0, len); } wr.flush(); wr.close(); bis.close(); in = readInputStream(urlConnection); } catch (Exception e) { throw e; } finally { urlConnection.disconnect(); } return new Response(urlConnection.getResponseCode(), in, urlConnection.getHeaderFields()); }  static void initCookieManager(Context context); static void clearCookie(Context context); Response get(String url, Map<String, String> headers, boolean allowRedirect); Response post(String url, Map<String, String> headers, HttpEntity httpEntity, boolean allowRedirect); static byte[] unGZIP(byte[] zipped); static ByteArrayInputStream readDataForZgip(InputStream inStream, String charsetName); static ByteArrayInputStream read(InputStream inStream, String charsetName);  }### Answer:
@Test public void testPost_shouldUseCorrectMethod() throws Exception { server.enqueue(new MockResponse().setBody("it's all cool")); server.play(); StringEntity stringEntity = new StringEntity("a post body"); http.post(server.getUrl("/").toString(), new HashMap<String, String>(), stringEntity, false); RecordedRequest recordedRequest = server.takeRequest(); assertThat(recordedRequest.getMethod(), equalTo(HttpPost.METHOD_NAME)); assertThat(recordedRequest.getUtf8Body(), equalTo("a post body")); }
@Test public void tesPost_shouldIncludePostBody() throws Exception { server.enqueue(new MockResponse().setBody("it's all cool")); server.play(); StringEntity stringEntity = new StringEntity("a post body"); stringEntity.setContentType("text/plain; charset=UTF-8"); http.post(server.getUrl("/").toString(), new HashMap<String, String>(), stringEntity, false); RecordedRequest recordedRequest = server.takeRequest(); assertThat(recordedRequest.getUtf8Body(), equalTo("a post body")); assertThat(recordedRequest.getHeader(HTTP.CONTENT_TYPE), equalTo("text/plain; charset=UTF-8")); }
@Test public void testPostFile_shouldWithCorrectContentType() throws Exception { server.enqueue(new MockResponse().setBody("it's all cool")); server.play(); File file = new File(ClassLoader.getSystemResource("test.jpg").toURI()); assertNotNull(file); InputStream resourceAsStream = ClassLoader.getSystemResourceAsStream("mimetypes.default"); assertNotNull(resourceAsStream); FileEntity fileEntity = new FileEntity(file, new MimetypesFileTypeMap(resourceAsStream).getContentType(file)); http.post(server.getUrl("/").toString(), new HashMap<String, String>(), fileEntity, false); RecordedRequest recordedRequest = server.takeRequest(); assertThat(recordedRequest.getHeader(HTTP.CONTENT_TYPE), equalTo("image/jpeg")); }