### Question:
UrlConnectionClient implements Client { @Override public Response execute(Request request) throws IOException { HttpURLConnection connection = openConnection(request); prepareRequest(connection, request); Response response = readResponse(connection); return followRedirectsIfNecessary(request, response); } UrlConnectionClient();  UrlConnectionClient(CookieHandler cookieHandler); @Override Response execute(Request request);  }### Answer:
@Test public void get() throws Exception { given(connection.getURL()).willReturn(new URL(URL)); given(connection.getResponseCode()).willReturn(200); given(connection.getResponseMessage()).willReturn("OK"); Map<String, List<String>> responseHeaderFields = new LinkedHashMap<String, List<String>>(); responseHeaderFields.put("Set-Cookie", Arrays.asList("aaa")); given(connection.getHeaderFields()).willReturn(responseHeaderFields); Request request = new Request("GET", URL, Arrays.asList(new Header("Hoge", "Piyo")), null); Response response = underTest.execute(request); verify(connection).setRequestMethod("GET"); verify(connection).addRequestProperty("Hoge", "Piyo"); verify(connection, never()).getOutputStream(); assertThat(response.getUrl()).isEqualTo(URL); assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getReason()).isEqualTo("OK"); assertThat(response.getHeaders()).isEqualTo(Arrays.asList(new Header("Set-Cookie", "aaa"))); }
@Test public void post() throws Exception { given(connection.getURL()).willReturn(new URL(URL)); given(connection.getResponseCode()).willReturn(200); given(connection.getResponseMessage()).willReturn("OK"); Map<String, List<String>> responseHeaderFields = new LinkedHashMap<String, List<String>>(); responseHeaderFields.put("Set-Cookie", Arrays.asList("aaa")); given(connection.getHeaderFields()).willReturn(responseHeaderFields); Request request = new Request("POST", URL, Arrays.asList(new Header("Hoge", "Piyo")), new FormUrlEncodedTypedOutput().addField("foo", "bar")); Response response = underTest.execute(request); verify(connection).setRequestMethod("POST"); verify(connection).addRequestProperty("Hoge", "Piyo"); verify(connection).getOutputStream(); assertThat(response.getUrl()).isEqualTo(URL); assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getReason()).isEqualTo("OK"); assertThat(response.getHeaders()).isEqualTo(Arrays.asList(new Header("Set-Cookie", "aaa"))); assertThat(output.toString("UTF-8")).isEqualTo("foo=bar"); }
@Test public void postAndGet() throws Exception { given(connection.getURL()).willReturn(new URL(URL), new URL(URL + "/secret")); given(connection.getResponseCode()).willReturn(302, 200); Map<String, List<String>> responseHeaderFields = new LinkedHashMap<String, List<String>>(); responseHeaderFields.put("Location", Arrays.asList("http: given(connection.getHeaderFields()).willReturn(responseHeaderFields, Collections.<String, List<String>> emptyMap()); Request request = new Request("POST", URL, Collections.<Header> emptyList(), null); Response response = underTest.execute(request); assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getUrl()).isEqualTo(URL + "/secret"); }
@Test public void errorResponseThrowsHttpError() throws Exception { given(connection.getURL()).willReturn(new URL(URL)); given(connection.getResponseCode()).willReturn(400); Map<String, List<String>> responseHeaderFields = new LinkedHashMap<String, List<String>>(); given(connection.getHeaderFields()).willReturn(responseHeaderFields, Collections.<String, List<String>> emptyMap()); Request request = new Request("GET", URL, Collections.<Header> emptyList(), null); Response response = underTest.execute(request); assertThat(response.getStatus()).isEqualTo(400); }