Search in sources :

Example 41 with MockWebServer

use of okhttp3.mockwebserver.MockWebServer in project scribejava by scribejava.

the class OkHttpHttpClientTest method shouldSendPostRequest.

@Test
public void shouldSendPostRequest() throws Exception {
    final String expectedResponseBody = "response body";
    final String expectedRequestBody = "request body";
    final MockWebServer server = new MockWebServer();
    server.enqueue(new MockResponse().setBody(expectedResponseBody));
    server.enqueue(new MockResponse().setBody(expectedResponseBody));
    server.start();
    final HttpUrl baseUrl = server.url("/testUrl");
    // request with body
    OAuthRequest request = new OAuthRequest(Verb.POST, baseUrl.toString());
    request.setPayload(expectedRequestBody);
    Response response = oAuthService.execute(request, null).get(30, TimeUnit.SECONDS);
    assertEquals(expectedResponseBody, response.getBody());
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("POST", recordedRequest.getMethod());
    assertEquals(expectedRequestBody, recordedRequest.getBody().readUtf8());
    // request with empty body
    request = new OAuthRequest(Verb.POST, baseUrl.toString());
    response = oAuthService.execute(request, null).get(30, TimeUnit.SECONDS);
    assertEquals(expectedResponseBody, response.getBody());
    recordedRequest = server.takeRequest();
    assertEquals("POST", recordedRequest.getMethod());
    assertEquals("", recordedRequest.getBody().readUtf8());
    server.shutdown();
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Example 42 with MockWebServer

use of okhttp3.mockwebserver.MockWebServer in project scribejava by scribejava.

the class OkHttpHttpClientTest method shouldSendGetRequest.

@Test
public void shouldSendGetRequest() throws Exception {
    final String expectedResponseBody = "response body";
    final MockWebServer server = new MockWebServer();
    server.enqueue(new MockResponse().setBody(expectedResponseBody));
    server.start();
    final HttpUrl baseUrl = server.url("/testUrl");
    final OAuthRequest request = new OAuthRequest(Verb.GET, baseUrl.toString());
    final Response response = oAuthService.execute(request, null).get(30, TimeUnit.SECONDS);
    assertEquals(expectedResponseBody, response.getBody());
    final RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("GET", recordedRequest.getMethod());
    server.shutdown();
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Example 43 with MockWebServer

use of okhttp3.mockwebserver.MockWebServer in project spring-framework by spring-projects.

the class AbstractMockWebServerTestCase method setUp.

@Before
public void setUp() throws Exception {
    this.server = new MockWebServer();
    this.server.setDispatcher(new TestDispatcher());
    this.server.start();
    this.port = this.server.getPort();
    this.baseUrl = "http://localhost:" + this.port;
}
Also used : MockWebServer(okhttp3.mockwebserver.MockWebServer) Before(org.junit.Before)

Example 44 with MockWebServer

use of okhttp3.mockwebserver.MockWebServer in project spring-framework by spring-projects.

the class WebClientIntegrationTests method setup.

@Before
public void setup() {
    this.server = new MockWebServer();
    String baseUrl = this.server.url("/").toString();
    this.webClient = WebClient.create(baseUrl);
}
Also used : MockWebServer(okhttp3.mockwebserver.MockWebServer) Before(org.junit.Before)

Example 45 with MockWebServer

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

the class Benchmark method startServer.

private MockWebServer startServer() throws IOException {
    Logger.getLogger(MockWebServer.class.getName()).setLevel(Level.WARNING);
    MockWebServer server = new MockWebServer();
    if (tls) {
        SslClient sslClient = SslClient.localhost();
        server.useHttps(sslClient.socketFactory, false);
        server.setProtocols(protocols);
    }
    final MockResponse response = newResponse();
    server.setDispatcher(new Dispatcher() {

        @Override
        public MockResponse dispatch(RecordedRequest request) {
            return response;
        }
    });
    server.start();
    return server;
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) SslClient(okhttp3.internal.tls.SslClient) MockWebServer(okhttp3.mockwebserver.MockWebServer) Dispatcher(okhttp3.mockwebserver.Dispatcher)

Aggregations

MockWebServer (okhttp3.mockwebserver.MockWebServer)48 MockResponse (okhttp3.mockwebserver.MockResponse)31 Test (org.junit.Test)20 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)16 Before (org.junit.Before)14 IOException (java.io.IOException)9 CookieManager (java.net.CookieManager)7 HttpCookie (java.net.HttpCookie)5 Retrofit (retrofit2.Retrofit)5 ServerSocket (java.net.ServerSocket)4 Socket (java.net.Socket)4 Request (okhttp3.Request)4 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)3 Response (com.github.scribejava.core.model.Response)3 InputStream (java.io.InputStream)3 InterruptedIOException (java.io.InterruptedIOException)3 HttpURLConnection (java.net.HttpURLConnection)3 URI (java.net.URI)3 URL (java.net.URL)3 List (java.util.List)3