|
| 1 | +package com.github.scribejava.httpclient.okhttp; |
| 2 | + |
| 3 | +import com.github.scribejava.core.model.*; |
| 4 | +import com.github.scribejava.core.oauth.OAuth20Service; |
| 5 | +import com.github.scribejava.core.oauth.OAuthService; |
| 6 | +import okhttp3.HttpUrl; |
| 7 | +import okhttp3.OkHttpClient; |
| 8 | +import okhttp3.mockwebserver.MockResponse; |
| 9 | +import okhttp3.mockwebserver.MockWebServer; |
| 10 | +import okhttp3.mockwebserver.RecordedRequest; |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +import java.net.MalformedURLException; |
| 15 | +import java.util.concurrent.TimeUnit; |
| 16 | + |
| 17 | +import static org.junit.Assert.assertEquals; |
| 18 | + |
| 19 | +public class OkHttpHttpClientTest { |
| 20 | + private OAuthService<?> oAuthService; |
| 21 | + |
| 22 | + @Before |
| 23 | + public void setUp() throws MalformedURLException { |
| 24 | + HttpClient client = new OkHttpHttpClient(new OkHttpClient()); |
| 25 | + oAuthService = new OAuth20Service(null, |
| 26 | + new OAuthConfig("test", "test", null, null, null, null, null, null, null, null, null, null, client)); |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | + @Test |
| 31 | + public void shouldSendGetRequest() throws Exception { |
| 32 | + String expectedResponseBody = "response body"; |
| 33 | + |
| 34 | + MockWebServer server = new MockWebServer(); |
| 35 | + server.enqueue(new MockResponse().setBody(expectedResponseBody)); |
| 36 | + server.start(); |
| 37 | + |
| 38 | + HttpUrl baseUrl = server.url("/testUrl"); |
| 39 | + |
| 40 | + OAuthRequestAsync request = new OAuthRequestAsync(Verb.GET, baseUrl.toString(), oAuthService); |
| 41 | + Response response = request.sendAsync(null).get(30, TimeUnit.SECONDS); |
| 42 | + |
| 43 | + assertEquals(expectedResponseBody, response.getBody()); |
| 44 | + |
| 45 | + RecordedRequest recordedRequest = server.takeRequest(); |
| 46 | + assertEquals("GET", recordedRequest.getMethod()); |
| 47 | + |
| 48 | + server.shutdown(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void shouldSendPostRequest() throws Exception { |
| 53 | + String expectedResponseBody = "response body"; |
| 54 | + String expectedRequestBody = "request body"; |
| 55 | + |
| 56 | + MockWebServer server = new MockWebServer(); |
| 57 | + server.enqueue(new MockResponse().setBody(expectedResponseBody)); |
| 58 | + server.enqueue(new MockResponse().setBody(expectedResponseBody)); |
| 59 | + server.start(); |
| 60 | + |
| 61 | + HttpUrl baseUrl = server.url("/testUrl"); |
| 62 | + |
| 63 | + // request with body |
| 64 | + OAuthRequestAsync request = new OAuthRequestAsync(Verb.POST, baseUrl.toString(), oAuthService); |
| 65 | + request.addPayload(expectedRequestBody); |
| 66 | + Response response = request.sendAsync(null).get(30, TimeUnit.SECONDS); |
| 67 | + |
| 68 | + assertEquals(expectedResponseBody, response.getBody()); |
| 69 | + |
| 70 | + RecordedRequest recordedRequest = server.takeRequest(); |
| 71 | + assertEquals("POST", recordedRequest.getMethod()); |
| 72 | + assertEquals(expectedRequestBody, recordedRequest.getBody().readUtf8()); |
| 73 | + |
| 74 | + |
| 75 | + // request with empty body |
| 76 | + request = new OAuthRequestAsync(Verb.POST, baseUrl.toString(), oAuthService); |
| 77 | + response = request.sendAsync(null).get(30, TimeUnit.SECONDS); |
| 78 | + |
| 79 | + assertEquals(expectedResponseBody, response.getBody()); |
| 80 | + |
| 81 | + recordedRequest = server.takeRequest(); |
| 82 | + assertEquals("POST", recordedRequest.getMethod()); |
| 83 | + assertEquals("", recordedRequest.getBody().readUtf8()); |
| 84 | + |
| 85 | + server.shutdown(); |
| 86 | + } |
| 87 | +} |
0 commit comments