Search in sources :

Example 46 with RecordedRequest

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

the class HttpOverHttp2Test method sendRequestCookies.

@Test
public void sendRequestCookies() throws Exception {
    RecordingCookieJar cookieJar = new RecordingCookieJar();
    Cookie requestCookie = new Cookie.Builder().name("a").value("b").domain(server.getHostName()).build();
    cookieJar.enqueueRequestCookies(requestCookie);
    client = client.newBuilder().cookieJar(cookieJar).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 request = server.takeRequest();
    assertEquals("a=b", request.getHeader("Cookie"));
}
Also used : Cookie(okhttp3.Cookie) MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) RecordingCookieJar(okhttp3.RecordingCookieJar) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 47 with RecordedRequest

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

the class ChunkingConverter method main.

public static void main(String... args) throws IOException, InterruptedException {
    MockWebServer server = new MockWebServer();
    server.enqueue(new MockResponse());
    server.enqueue(new MockResponse());
    server.start();
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new ChunkingConverterFactory()).addConverterFactory(GsonConverterFactory.create()).build();
    Service service = retrofit.create(Service.class);
    Repo retrofitRepo = new Repo("square", "retrofit");
    service.sendNormal(retrofitRepo).execute();
    RecordedRequest normalRequest = server.takeRequest();
    System.out.println("Normal @Body Transfer-Encoding: " + normalRequest.getHeader("Transfer-Encoding"));
    service.sendChunked(retrofitRepo).execute();
    RecordedRequest chunkedRequest = server.takeRequest();
    System.out.println("@Chunked @Body Transfer-Encoding: " + chunkedRequest.getHeader("Transfer-Encoding"));
    server.shutdown();
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Retrofit(retrofit2.Retrofit) MockWebServer(okhttp3.mockwebserver.MockWebServer)

Example 48 with RecordedRequest

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

the class JsonQueryParameters method main.

public static void main(String... args) throws IOException, InterruptedException {
    MockWebServer server = new MockWebServer();
    server.start();
    server.enqueue(new MockResponse());
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(new JsonStringConverterFactory(GsonConverterFactory.create())).build();
    Service service = retrofit.create(Service.class);
    Call<ResponseBody> call = service.example(new Filter("123"));
    Response<ResponseBody> response = call.execute();
    // TODO handle user response...
    // Print the request path that the server saw to show the JSON query param:
    RecordedRequest recordedRequest = server.takeRequest();
    System.out.println(recordedRequest.getPath());
    server.shutdown();
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Retrofit(retrofit2.Retrofit) MockWebServer(okhttp3.mockwebserver.MockWebServer) ResponseBody(okhttp3.ResponseBody)

Example 49 with RecordedRequest

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

the class URLConnectionTest method testResponseRedirectedWithPost.

private void testResponseRedirectedWithPost(int redirectCode, TransferKind transferKind) throws Exception {
    server.enqueue(new MockResponse().setResponseCode(redirectCode).addHeader("Location: /page2").setBody("This page has moved!"));
    server.enqueue(new MockResponse().setBody("Page 2"));
    connection = urlFactory.open(server.url("/page1").url());
    connection.setDoOutput(true);
    transferKind.setForRequest(connection, 4);
    byte[] requestBody = { 'A', 'B', 'C', 'D' };
    OutputStream outputStream = connection.getOutputStream();
    outputStream.write(requestBody);
    outputStream.close();
    assertEquals("Page 2", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
    assertTrue(connection.getDoOutput());
    RecordedRequest page1 = server.takeRequest();
    assertEquals("POST /page1 HTTP/1.1", page1.getRequestLine());
    assertEquals("ABCD", page1.getBody().readUtf8());
    RecordedRequest page2 = server.takeRequest();
    assertEquals("GET /page2 HTTP/1.1", page2.getRequestLine());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) OutputStream(java.io.OutputStream)

Example 50 with RecordedRequest

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

the class URLConnectionTest method redirectedPostStripsRequestBodyHeaders.

@Test
public void redirectedPostStripsRequestBodyHeaders() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: /page2"));
    server.enqueue(new MockResponse().setBody("Page 2"));
    connection = urlFactory.open(server.url("/page1").url());
    connection.setDoOutput(true);
    connection.addRequestProperty("Content-Length", "4");
    connection.addRequestProperty("Content-Type", "text/plain; charset=utf-8");
    connection.addRequestProperty("Transfer-Encoding", "identity");
    OutputStream outputStream = connection.getOutputStream();
    outputStream.write("ABCD".getBytes("UTF-8"));
    outputStream.close();
    assertEquals("Page 2", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
    assertEquals("POST /page1 HTTP/1.1", server.takeRequest().getRequestLine());
    RecordedRequest page2 = server.takeRequest();
    assertEquals("GET /page2 HTTP/1.1", page2.getRequestLine());
    assertNull(page2.getHeader("Content-Length"));
    assertNull(page2.getHeader("Content-Type"));
    assertNull(page2.getHeader("Transfer-Encoding"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) OutputStream(java.io.OutputStream) 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