use of okhttp3.mockwebserver.RecordedRequest in project retrofit by square.
the class MoshiConverterFactoryTest method anInterface.
@Test
public void anInterface() throws IOException, InterruptedException {
server.enqueue(new MockResponse().setBody("{\"name\":\"value\"}"));
Call<AnInterface> call = service.anInterface(new AnImplementation("value"));
Response<AnInterface> response = call.execute();
AnInterface body = response.body();
assertThat(body.getName()).isEqualTo("value");
RecordedRequest request = server.takeRequest();
assertThat(request.getBody().readUtf8()).isEqualTo("{\"name\":\"value\"}");
assertThat(request.getHeader("Content-Type")).isEqualTo("application/json; charset=UTF-8");
}
use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class CallTest method propfindRedirectsToPropfindAndMaintainsRequestBody.
@Test
public void propfindRedirectsToPropfindAndMaintainsRequestBody() throws Exception {
// given
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: /page2").setBody("This page has moved!"));
server.enqueue(new MockResponse().setBody("Page 2"));
// when
Response response = client.newCall(new Request.Builder().url(server.url("/page1")).method("PROPFIND", RequestBody.create(MediaType.parse("text/plain"), "Request Body")).build()).execute();
// then
assertEquals("Page 2", response.body().string());
RecordedRequest page1 = server.takeRequest();
assertEquals("PROPFIND /page1 HTTP/1.1", page1.getRequestLine());
assertEquals("Request Body", page1.getBody().readUtf8());
RecordedRequest page2 = server.takeRequest();
assertEquals("PROPFIND /page2 HTTP/1.1", page2.getRequestLine());
assertEquals("Request Body", page2.getBody().readUtf8());
}
use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class CallTest method post_Async.
@Test
public void post_Async() throws Exception {
server.enqueue(new MockResponse().setBody("abc"));
Request request = new Request.Builder().url(server.url("/")).post(RequestBody.create(MediaType.parse("text/plain"), "def")).build();
client.newCall(request).enqueue(callback);
callback.await(request.url()).assertCode(200).assertBody("abc");
RecordedRequest recordedRequest = server.takeRequest();
assertEquals("def", recordedRequest.getBody().readUtf8());
assertEquals("3", recordedRequest.getHeader("Content-Length"));
assertEquals("text/plain; charset=utf-8", recordedRequest.getHeader("Content-Type"));
}
use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class CallTest method userAgentIsIncludedByDefault.
@Test
public void userAgentIsIncludedByDefault() throws Exception {
server.enqueue(new MockResponse());
executeSynchronously("/");
RecordedRequest recordedRequest = server.takeRequest();
assertTrue(recordedRequest.getHeader("User-Agent").matches(Version.userAgent()));
}
use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class CallTest method proxyConnectOmitsApplicationHeaders.
/** Test which headers are sent unencrypted to the HTTP proxy. */
@Test
public void proxyConnectOmitsApplicationHeaders() throws Exception {
server.useHttps(sslClient.socketFactory, true);
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
server.enqueue(new MockResponse().setBody("encrypted response from the origin server"));
RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).proxy(server.toProxyAddress()).hostnameVerifier(hostnameVerifier).build();
Request request = new Request.Builder().url("https://android.com/foo").header("Private", "Secret").header("User-Agent", "App 1.0").build();
Response response = client.newCall(request).execute();
assertEquals("encrypted response from the origin server", response.body().string());
RecordedRequest connect = server.takeRequest();
assertNull(connect.getHeader("Private"));
assertEquals(Version.userAgent(), connect.getHeader("User-Agent"));
assertEquals("Keep-Alive", connect.getHeader("Proxy-Connection"));
assertEquals("android.com:443", connect.getHeader("Host"));
RecordedRequest get = server.takeRequest();
assertEquals("Secret", get.getHeader("Private"));
assertEquals("App 1.0", get.getHeader("User-Agent"));
assertEquals(Arrays.asList("verify android.com"), hostnameVerifier.calls);
}
Aggregations