@Test public void shouldExecuteAndReturnSuccessWithDefaultCharsetInContentType() throws Exception { when(connection.getContentType()).thenReturn("text/html"); when(connection.getErrorStream()).thenReturn(null); when(connection.getInputStream()).thenReturn(inputStream); when(connection.getResponseCode()).thenReturn(200); HttpResponse expectedResponse = new HttpResponse(200, new String("çãàâ".getBytes(), Charset.forName("ISO-8859-1"))); HttpResponse response = httpClient.execute(httpMethod, targetUrl, headers, httpRequestBody); assertEquals(expectedResponse, response); InOrder inOrder = inOrder(connection); inOrder.verify(connection, times(headers.size())).setRequestProperty(anyString(), anyString()); inOrder.verify(connection).getOutputStream(); inOrder.verify(connection).getContentType(); inOrder.verify(connection).getErrorStream(); inOrder.verify(connection).getInputStream(); inOrder.verify(connection).disconnect(); }
@Override public HttpResponse execute(HttpMethod method, String targetURL, Map<String, String> headers, HttpRequestBody body) throws IOException { LOGGER.info(String.format("Executando [%s] em [%s]", method.toString(), targetURL)); HttpURLConnection connection = null; try { final URL url = new URL(targetURL); LOGGER.info("Abrindo conexao"); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(method.toString()); connection.setUseCaches(false); writeHeaders(connection, headers); LOGGER.info("Escrevendo body"); writeBody(connection, body); final String responseCharset = getResponseCharset(connection); LOGGER.info("Lendo resposta"); final InputStream responseStream = getResponseStream(connection); final String responseString = getResponseString(responseStream, responseCharset); return new HttpResponse(connection.getResponseCode(), responseString); } finally { if (connection != null) { LOGGER.info("Fechando conexao"); connection.disconnect(); } } }
JSEHttpClient implements HttpClient { @Override public HttpResponse execute(HttpMethod method, String targetURL, Map<String, String> headers, HttpRequestBody body) throws IOException { LOGGER.info(String.format("Executando [%s] em [%s]", method.toString(), targetURL)); HttpURLConnection connection = null; try { final URL url = new URL(targetURL); LOGGER.info("Abrindo conexao"); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(method.toString()); connection.setUseCaches(false); writeHeaders(connection, headers); LOGGER.info("Escrevendo body"); writeBody(connection, body); final String responseCharset = getResponseCharset(connection); LOGGER.info("Lendo resposta"); final InputStream responseStream = getResponseStream(connection); final String responseString = getResponseString(responseStream, responseCharset); return new HttpResponse(connection.getResponseCode(), responseString); } finally { if (connection != null) { LOGGER.info("Fechando conexao"); connection.disconnect(); } } } }
JSEHttpClient implements HttpClient { @Override public HttpResponse execute(HttpMethod method, String targetURL, Map<String, String> headers, HttpRequestBody body) throws IOException { LOGGER.info(String.format("Executando [%s] em [%s]", method.toString(), targetURL)); HttpURLConnection connection = null; try { final URL url = new URL(targetURL); LOGGER.info("Abrindo conexao"); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(method.toString()); connection.setUseCaches(false); writeHeaders(connection, headers); LOGGER.info("Escrevendo body"); writeBody(connection, body); final String responseCharset = getResponseCharset(connection); LOGGER.info("Lendo resposta"); final InputStream responseStream = getResponseStream(connection); final String responseString = getResponseString(responseStream, responseCharset); return new HttpResponse(connection.getResponseCode(), responseString); } finally { if (connection != null) { LOGGER.info("Fechando conexao"); connection.disconnect(); } } }  }
JSEHttpClient implements HttpClient { @Override public HttpResponse execute(HttpMethod method, String targetURL, Map<String, String> headers, HttpRequestBody body) throws IOException { LOGGER.info(String.format("Executando [%s] em [%s]", method.toString(), targetURL)); HttpURLConnection connection = null; try { final URL url = new URL(targetURL); LOGGER.info("Abrindo conexao"); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(method.toString()); connection.setUseCaches(false); writeHeaders(connection, headers); LOGGER.info("Escrevendo body"); writeBody(connection, body); final String responseCharset = getResponseCharset(connection); LOGGER.info("Lendo resposta"); final InputStream responseStream = getResponseStream(connection); final String responseString = getResponseString(responseStream, responseCharset); return new HttpResponse(connection.getResponseCode(), responseString); } finally { if (connection != null) { LOGGER.info("Fechando conexao"); connection.disconnect(); } } }  @Override HttpResponse execute(HttpMethod method, String targetURL, Map<String, String> headers,
                                HttpRequestBody body); @Override HttpResponse executeXML(HttpMethod method, String targetURL, Map<String, String> headers,
                                HttpXMLRequestBody body); @Override HttpResponse executeJson(HttpMethod method, String targetURL, Map<String, String> headers,
                                    HttpJsonRequestBody body); HttpResponse execute(HttpMethod method, String targetURL, Map<String, String> headers); }
JSEHttpClient implements HttpClient { @Override public HttpResponse execute(HttpMethod method, String targetURL, Map<String, String> headers, HttpRequestBody body) throws IOException { LOGGER.info(String.format("Executando [%s] em [%s]", method.toString(), targetURL)); HttpURLConnection connection = null; try { final URL url = new URL(targetURL); LOGGER.info("Abrindo conexao"); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(method.toString()); connection.setUseCaches(false); writeHeaders(connection, headers); LOGGER.info("Escrevendo body"); writeBody(connection, body); final String responseCharset = getResponseCharset(connection); LOGGER.info("Lendo resposta"); final InputStream responseStream = getResponseStream(connection); final String responseString = getResponseString(responseStream, responseCharset); return new HttpResponse(connection.getResponseCode(), responseString); } finally { if (connection != null) { LOGGER.info("Fechando conexao"); connection.disconnect(); } } }  @Override HttpResponse execute(HttpMethod method, String targetURL, Map<String, String> headers,
                                HttpRequestBody body); @Override HttpResponse executeXML(HttpMethod method, String targetURL, Map<String, String> headers,
                                HttpXMLRequestBody body); @Override HttpResponse executeJson(HttpMethod method, String targetURL, Map<String, String> headers,
                                    HttpJsonRequestBody body); HttpResponse execute(HttpMethod method, String targetURL, Map<String, String> headers);  }