@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://example.com/secret")); 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"); }