@Test public void newHttpURLConnection() throws Exception { { HttpClient httpClient = new HttpClient(); HttpURLConnection httpURLConnection = httpClient.newHttpURLConnection(new URL("http: assertThat(httpURLConnection.getURL(), is(new URL("http: assertThat(httpURLConnection.getDoInput(), is(true)); assertThat(httpURLConnection.getDoOutput(), is(false)); assertThat(httpURLConnection.getUseCaches(), is(false)); assertThat(httpURLConnection.getRequestMethod(), is(not(nullValue()))); assertThat(httpURLConnection.getRequestMethod().toUpperCase(), is("GET")); assertThat(httpURLConnection.getRequestProperties().size(), is(1)); assertThat(httpURLConnection.getRequestProperties().containsKey(HttpHeaders.ACCEPT_CHARSET), is(true)); assertThat(httpURLConnection.getRequestProperties().get(HttpHeaders.ACCEPT_CHARSET).size(), is(1)); assertThat(httpURLConnection.getRequestProperties().get(HttpHeaders.ACCEPT_CHARSET).get(0), is("utf-8")); } { HttpClient httpClient = new HttpClient(); HttpURLConnection httpURLConnection = httpClient.newHttpURLConnection(new URL("http: assertThat(httpURLConnection.getURL(), is(new URL("http: assertThat(httpURLConnection.getDoInput(), is(true)); assertThat(httpURLConnection.getDoOutput(), is(true)); assertThat(httpURLConnection.getUseCaches(), is(false)); assertThat(httpURLConnection.getRequestMethod(), is(not(nullValue()))); assertThat(httpURLConnection.getRequestMethod().toUpperCase(), is("POST")); assertThat(httpURLConnection.getRequestProperties().size(), is(2)); assertThat(httpURLConnection.getRequestProperties().containsKey(HttpHeaders.ACCEPT_CHARSET), is(true)); assertThat(httpURLConnection.getRequestProperties().get(HttpHeaders.ACCEPT_CHARSET).size(), is(1)); assertThat(httpURLConnection.getRequestProperties().get(HttpHeaders.ACCEPT_CHARSET).get(0), is("utf-8")); assertThat(httpURLConnection.getRequestProperties().containsKey(HttpHeaders.CONTENT_TYPE), is(true)); assertThat(httpURLConnection.getRequestProperties().get(HttpHeaders.CONTENT_TYPE).size(), is(1)); assertThat(httpURLConnection.getRequestProperties().get(HttpHeaders.CONTENT_TYPE).get(0), is("application/x-www-form-urlencoded;charset=utf-8")); } }
@VisibleForTesting HttpURLConnection newHttpURLConnection(URL url, HttpRequestMethod method, String encoding) throws IOException { HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(method == HttpRequestMethod.POST); httpURLConnection.setUseCaches(false); httpURLConnection.setRequestMethod(method.toString()); httpURLConnection.setRequestProperty(HttpHeaders.ACCEPT_CHARSET, encoding); if (method == HttpRequestMethod.POST) { httpURLConnection.setRequestProperty(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded;charset=" + encoding); } return httpURLConnection; }
HttpClient { @VisibleForTesting HttpURLConnection newHttpURLConnection(URL url, HttpRequestMethod method, String encoding) throws IOException { HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(method == HttpRequestMethod.POST); httpURLConnection.setUseCaches(false); httpURLConnection.setRequestMethod(method.toString()); httpURLConnection.setRequestProperty(HttpHeaders.ACCEPT_CHARSET, encoding); if (method == HttpRequestMethod.POST) { httpURLConnection.setRequestProperty(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded;charset=" + encoding); } return httpURLConnection; } }
HttpClient { @VisibleForTesting HttpURLConnection newHttpURLConnection(URL url, HttpRequestMethod method, String encoding) throws IOException { HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(method == HttpRequestMethod.POST); httpURLConnection.setUseCaches(false); httpURLConnection.setRequestMethod(method.toString()); httpURLConnection.setRequestProperty(HttpHeaders.ACCEPT_CHARSET, encoding); if (method == HttpRequestMethod.POST) { httpURLConnection.setRequestProperty(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded;charset=" + encoding); } return httpURLConnection; } HttpClient(); }
HttpClient { @VisibleForTesting HttpURLConnection newHttpURLConnection(URL url, HttpRequestMethod method, String encoding) throws IOException { HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(method == HttpRequestMethod.POST); httpURLConnection.setUseCaches(false); httpURLConnection.setRequestMethod(method.toString()); httpURLConnection.setRequestProperty(HttpHeaders.ACCEPT_CHARSET, encoding); if (method == HttpRequestMethod.POST) { httpURLConnection.setRequestProperty(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded;charset=" + encoding); } return httpURLConnection; } HttpClient(); HttpResponse request(HttpRequest httpRequest); void disconnect(); }
HttpClient { @VisibleForTesting HttpURLConnection newHttpURLConnection(URL url, HttpRequestMethod method, String encoding) throws IOException { HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(method == HttpRequestMethod.POST); httpURLConnection.setUseCaches(false); httpURLConnection.setRequestMethod(method.toString()); httpURLConnection.setRequestProperty(HttpHeaders.ACCEPT_CHARSET, encoding); if (method == HttpRequestMethod.POST) { httpURLConnection.setRequestProperty(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded;charset=" + encoding); } return httpURLConnection; } HttpClient(); HttpResponse request(HttpRequest httpRequest); void disconnect();  }