Search in sources :

Example 51 with HttpUrl

use of okhttp3.HttpUrl in project scribejava by scribejava.

the class AbstractClientTest method shouldCallCallback.

@Test
public void shouldCallCallback() throws Exception {
    final String expectedResponseBody = "response body for test shouldCallCallback";
    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 TestCallback callback = new TestCallback();
    oAuthService.execute(request, callback).get();
    assertEquals(expectedResponseBody, callback.getResponse().getBody());
    server.shutdown();
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Example 52 with HttpUrl

use of okhttp3.HttpUrl in project scribejava by scribejava.

the class AbstractClientTest method shouldReadResponseStream.

@Test
public void shouldReadResponseStream() throws Exception {
    final String expectedResponseBody = "response body for test shouldReadResponseStream";
    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, StreamUtils.getStreamContents(response.getStream()));
    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 53 with HttpUrl

use of okhttp3.HttpUrl in project scribejava by scribejava.

the class AbstractClientTest method shouldPassErrors.

@Test
public void shouldPassErrors() throws Exception {
    final MockWebServer server = new MockWebServer();
    server.enqueue(new MockResponse().setResponseCode(500));
    server.start();
    final HttpUrl baseUrl = server.url("/testUrl");
    final OAuthRequest request = new OAuthRequest(Verb.GET, baseUrl.toString());
    final TestCallback callback = new TestCallback();
    final Response response = oAuthService.execute(request, callback).get();
    assertEquals(500, response.getCode());
    assertEquals(500, callback.getResponse().getCode());
    server.shutdown();
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Example 54 with HttpUrl

use of okhttp3.HttpUrl in project scribejava by scribejava.

the class AbstractClientTest method shouldSendPostRequest.

@Test
public void shouldSendPostRequest() throws Exception {
    final String expectedResponseBody = "response body for test shouldSendPostRequest";
    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 55 with HttpUrl

use of okhttp3.HttpUrl in project MiMangaNu by raulhaag.

the class NineManga method generateNeededCookie.

/**
 * Helper function to generate the Cookie needed by NineManga.
 */
private static void generateNeededCookie() {
    long aTime = System.currentTimeMillis() / 1000 - (int) (Math.random() * 100);
    HttpUrl url = HttpUrl.parse("https://www.ninemanga.com/");
    // pasring the valid constant URL will never fail
    assert url != null;
    Navigator.getCookieJar().saveFromResponse(url, Arrays.asList(Cookie.parse(url, "ninemanga_country_code=AR; Domain=ninemanga.com"), Cookie.parse(url, "__utma=128769555.619121721." + aTime + "." + aTime + "." + aTime + ".1; Domain=ninemanga.com"), Cookie.parse(url, "__utmc=128769555; Domain=ninemanga.com"), Cookie.parse(url, "__utmz=128769555." + aTime + ".1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); Domain=ninemanga.com")));
    cookieInit = true;
}
Also used : HttpUrl(okhttp3.HttpUrl)

Aggregations

HttpUrl (okhttp3.HttpUrl)302 Request (okhttp3.Request)130 Test (org.junit.Test)86 Response (okhttp3.Response)69 IOException (java.io.IOException)61 MockResponse (okhttp3.mockwebserver.MockResponse)40 Cookie (okhttp3.Cookie)35 ArrayList (java.util.ArrayList)28 OkHttpClient (okhttp3.OkHttpClient)27 MockWebServer (okhttp3.mockwebserver.MockWebServer)26 InputStream (java.io.InputStream)21 InputStreamReader (java.io.InputStreamReader)20 HashMap (java.util.HashMap)19 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)19 RequestBody (okhttp3.RequestBody)18 JsonParseException (com.google.gson.JsonParseException)13 File (java.io.File)12 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)10 List (java.util.List)10 Test (org.junit.jupiter.api.Test)10