Search in sources :

Example 41 with RecordedRequest

use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.

the class HttpOverHttp2Test method userSuppliedContentLengthHeader.

@Test
public void userSuppliedContentLengthHeader() throws Exception {
    final byte[] postBytes = "FGHIJ".getBytes(Util.UTF_8);
    server.enqueue(new MockResponse().setBody("ABCDE"));
    Call call = client.newCall(new Request.Builder().url(server.url("/foo")).post(new RequestBody() {

        @Override
        public MediaType contentType() {
            return MediaType.parse("text/plain; charset=utf-8");
        }

        @Override
        public long contentLength() throws IOException {
            return postBytes.length;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            sink.write(postBytes);
        }
    }).build());
    Response response = call.execute();
    assertEquals("ABCDE", response.body().string());
    RecordedRequest request = server.takeRequest();
    assertEquals("POST /foo HTTP/1.1", request.getRequestLine());
    assertArrayEquals(postBytes, request.getBody().readByteArray());
    assertEquals(postBytes.length, Integer.parseInt(request.getHeader("Content-Length")));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) MediaType(okhttp3.MediaType) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 42 with RecordedRequest

use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.

the class HttpOverHttp2Test method get.

@Test
public void get() throws Exception {
    server.enqueue(new MockResponse().setBody("ABCDE").setStatus("HTTP/1.1 200 Sweet"));
    Call call = client.newCall(new Request.Builder().url(server.url("/foo")).build());
    Response response = call.execute();
    assertEquals("ABCDE", response.body().string());
    assertEquals(200, response.code());
    assertEquals("Sweet", response.message());
    RecordedRequest request = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
    assertEquals("https", request.getHeader(":scheme"));
    assertEquals(server.getHostName() + ":" + server.getPort(), request.getHeader(":authority"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 43 with RecordedRequest

use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.

the class HttpOverHttp2Test method serverSendsPushPromise_GET.

@Test
public void serverSendsPushPromise_GET() throws Exception {
    PushPromise pushPromise = new PushPromise("GET", "/foo/bar", Headers.of("foo", "bar"), new MockResponse().setBody("bar").setStatus("HTTP/1.1 200 Sweet"));
    server.enqueue(new MockResponse().setBody("ABCDE").setStatus("HTTP/1.1 200 Sweet").withPush(pushPromise));
    Call call = client.newCall(new Request.Builder().url(server.url("/foo")).build());
    Response response = call.execute();
    assertEquals("ABCDE", response.body().string());
    assertEquals(200, response.code());
    assertEquals("Sweet", response.message());
    RecordedRequest request = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
    assertEquals("https", request.getHeader(":scheme"));
    assertEquals(server.getHostName() + ":" + server.getPort(), request.getHeader(":authority"));
    RecordedRequest pushedRequest = server.takeRequest();
    assertEquals("GET /foo/bar HTTP/1.1", pushedRequest.getRequestLine());
    assertEquals("bar", pushedRequest.getHeader("foo"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) PushPromise(okhttp3.mockwebserver.PushPromise) Test(org.junit.Test)

Example 44 with RecordedRequest

use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.

the class HttpOverHttp2Test method serverSendsPushPromise_HEAD.

@Test
public void serverSendsPushPromise_HEAD() throws Exception {
    PushPromise pushPromise = new PushPromise("HEAD", "/foo/bar", Headers.of("foo", "bar"), new MockResponse().setStatus("HTTP/1.1 204 Sweet"));
    server.enqueue(new MockResponse().setBody("ABCDE").setStatus("HTTP/1.1 200 Sweet").withPush(pushPromise));
    Call call = client.newCall(new Request.Builder().url(server.url("/foo")).build());
    Response response = call.execute();
    assertEquals("ABCDE", response.body().string());
    assertEquals(200, response.code());
    assertEquals("Sweet", response.message());
    RecordedRequest request = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
    assertEquals("https", request.getHeader(":scheme"));
    assertEquals(server.getHostName() + ":" + server.getPort(), request.getHeader(":authority"));
    RecordedRequest pushedRequest = server.takeRequest();
    assertEquals("HEAD /foo/bar HTTP/1.1", pushedRequest.getRequestLine());
    assertEquals("bar", pushedRequest.getHeader("foo"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) PushPromise(okhttp3.mockwebserver.PushPromise) Test(org.junit.Test)

Example 45 with RecordedRequest

use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.

the class HttpOverHttp2Test method domainFronting.

/** https://github.com/square/okhttp/issues/3103 */
@Test
public void domainFronting() throws Exception {
    client = client.newBuilder().addNetworkInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request().newBuilder().header("Host", "privateobject.com").build();
            return chain.proceed(request);
        }
    }).build();
    server.enqueue(new MockResponse());
    Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
    Response response = call.execute();
    assertEquals("", response.body().string());
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("privateobject.com", recordedRequest.getHeader(":authority"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) IOException(java.io.IOException) Interceptor(okhttp3.Interceptor) 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