@Test public void request() throws IOException { { String responseContent = "Response"; String requestContent = "Request"; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayInputStream inputStream = new ByteArrayInputStream(responseContent.getBytes("utf-8")); HttpURLConnection httpURLConnection = mock(HttpURLConnection.class); when(httpURLConnection.getOutputStream()).thenReturn(outputStream); when(httpURLConnection.getResponseCode()).thenReturn(-1); when(httpURLConnection.getContentLength()).thenReturn(responseContent.length()); when(httpURLConnection.getInputStream()).thenReturn(inputStream); HttpClient httpClient = new HttpClient(); httpClient.httpURLConnection = httpURLConnection; HttpResponse response = httpClient.request(requestContent, "utf-8"); assertThat(response, is(not(nullValue()))); assertThat(response.getStatusCode(), is(-1)); assertThat(response.getContent(), is("Response")); } { String responseContent = ""; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayInputStream inputStream = new ByteArrayInputStream(responseContent.getBytes("utf-8")); HttpURLConnection httpURLConnection = mock(HttpURLConnection.class); when(httpURLConnection.getDoOutput()).thenReturn(false); when(httpURLConnection.getOutputStream()).thenReturn(outputStream); when(httpURLConnection.getResponseCode()).thenReturn(-1); when(httpURLConnection.getContentLength()).thenReturn(responseContent.length()); when(httpURLConnection.getInputStream()).thenReturn(inputStream); HttpClient httpClient = new HttpClient(); httpClient.httpURLConnection = httpURLConnection; httpClient.request("something", "utf-8"); assertThat(outputStream.size(), is(0)); } { String responseContent = ""; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayInputStream inputStream = new ByteArrayInputStream(responseContent.getBytes("utf-8")); HttpURLConnection httpURLConnection = mock(HttpURLConnection.class); when(httpURLConnection.getDoOutput()).thenReturn(true); when(httpURLConnection.getOutputStream()).thenReturn(outputStream); when(httpURLConnection.getResponseCode()).thenReturn(-1); when(httpURLConnection.getContentLength()).thenReturn(responseContent.length()); when(httpURLConnection.getInputStream()).thenReturn(inputStream); HttpClient httpClient = new HttpClient(); httpClient.httpURLConnection = httpURLConnection; httpClient.request("key=value&key2=value2", "utf-8"); String expectedString = "key=value&key2=value2"; assertThat(outputStream.size(), is(expectedString.length())); assertThat(outputStream.toByteArray(), is(expectedString.getBytes("utf-8"))); } }
public HttpResponse request(HttpRequest httpRequest) throws IOException { URL url = new URL(httpRequest.getUrl()); HttpRequestMethod method = httpRequest.getHttpRequestMethod(); String encoding = httpRequest.getEncoding(); httpURLConnection = newHttpURLConnection(url, method, encoding); String content = httpRequest.getContent(); if (content == null) { content = HttpParameter.encodeParameters(httpRequest.getParameters(), encoding); } if (method == HttpRequestMethod.POST) { httpURLConnection.setRequestProperty(HttpHeaders.CONTENT_LENGTH, String.valueOf(content.length())); } return request(content, encoding); }
HttpClient { public HttpResponse request(HttpRequest httpRequest) throws IOException { URL url = new URL(httpRequest.getUrl()); HttpRequestMethod method = httpRequest.getHttpRequestMethod(); String encoding = httpRequest.getEncoding(); httpURLConnection = newHttpURLConnection(url, method, encoding); String content = httpRequest.getContent(); if (content == null) { content = HttpParameter.encodeParameters(httpRequest.getParameters(), encoding); } if (method == HttpRequestMethod.POST) { httpURLConnection.setRequestProperty(HttpHeaders.CONTENT_LENGTH, String.valueOf(content.length())); } return request(content, encoding); } }
HttpClient { public HttpResponse request(HttpRequest httpRequest) throws IOException { URL url = new URL(httpRequest.getUrl()); HttpRequestMethod method = httpRequest.getHttpRequestMethod(); String encoding = httpRequest.getEncoding(); httpURLConnection = newHttpURLConnection(url, method, encoding); String content = httpRequest.getContent(); if (content == null) { content = HttpParameter.encodeParameters(httpRequest.getParameters(), encoding); } if (method == HttpRequestMethod.POST) { httpURLConnection.setRequestProperty(HttpHeaders.CONTENT_LENGTH, String.valueOf(content.length())); } return request(content, encoding); } HttpClient(); }
HttpClient { public HttpResponse request(HttpRequest httpRequest) throws IOException { URL url = new URL(httpRequest.getUrl()); HttpRequestMethod method = httpRequest.getHttpRequestMethod(); String encoding = httpRequest.getEncoding(); httpURLConnection = newHttpURLConnection(url, method, encoding); String content = httpRequest.getContent(); if (content == null) { content = HttpParameter.encodeParameters(httpRequest.getParameters(), encoding); } if (method == HttpRequestMethod.POST) { httpURLConnection.setRequestProperty(HttpHeaders.CONTENT_LENGTH, String.valueOf(content.length())); } return request(content, encoding); } HttpClient(); HttpResponse request(HttpRequest httpRequest); void disconnect(); }
HttpClient { public HttpResponse request(HttpRequest httpRequest) throws IOException { URL url = new URL(httpRequest.getUrl()); HttpRequestMethod method = httpRequest.getHttpRequestMethod(); String encoding = httpRequest.getEncoding(); httpURLConnection = newHttpURLConnection(url, method, encoding); String content = httpRequest.getContent(); if (content == null) { content = HttpParameter.encodeParameters(httpRequest.getParameters(), encoding); } if (method == HttpRequestMethod.POST) { httpURLConnection.setRequestProperty(HttpHeaders.CONTENT_LENGTH, String.valueOf(content.length())); } return request(content, encoding); } HttpClient(); HttpResponse request(HttpRequest httpRequest); void disconnect();  }