Search in sources :

Example 71 with RecordedRequest

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");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 72 with RecordedRequest

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());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 73 with RecordedRequest

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"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 74 with RecordedRequest

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()));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 75 with RecordedRequest

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);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Aggregations

RecordedRequest (okhttp3.mockwebserver.RecordedRequest)206 MockResponse (okhttp3.mockwebserver.MockResponse)188 Test (org.junit.Test)168 HttpURLConnection (java.net.HttpURLConnection)18 MockWebServer (okhttp3.mockwebserver.MockWebServer)14 IOException (java.io.IOException)13 URL (java.net.URL)13 OutputStream (java.io.OutputStream)12 Response (okhttp3.Response)12 OkHttpURLConnection (okhttp3.internal.huc.OkHttpURLConnection)12 AbstractTest (org.openstack4j.api.AbstractTest)12 Test (org.testng.annotations.Test)12 Call (okhttp3.Call)11 Buffer (okio.Buffer)11 Request (okhttp3.Request)8 RecordingOkAuthenticator (okhttp3.internal.RecordingOkAuthenticator)7 Dispatcher (okhttp3.mockwebserver.Dispatcher)6 ByteString (okio.ByteString)6 CookieManager (java.net.CookieManager)5 URLConnection (java.net.URLConnection)5