Skip to content

Commit 9bc1ff8

Browse files
committed
Fixed not working Response.getStream method
1 parent 9787c7d commit 9bc1ff8

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

scribejava-httpclient-okhttp/src/main/java/com/github/scribejava/httpclient/okhttp/OAuthAsyncCompletionHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public void onResponse(Call call, okhttp3.Response okHttpResponse) throws IOExce
5858
final Response response = new Response(okHttpResponse.code(),
5959
okHttpResponse.message(),
6060
headersMap,
61-
okHttpResponse.body().string(),
62-
null); // cannot return both body String and InputStream
61+
null, // cannot return both body String and InputStream
62+
okHttpResponse.body().byteStream());
6363

6464
@SuppressWarnings("unchecked")
6565
final T t = converter == null ? (T) response : converter.convert(response);

scribejava-httpclient-okhttp/src/test/java/com/github/scribejava/httpclient/okhttp/OkHttpHttpClientTest.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package com.github.scribejava.httpclient.okhttp;
22

3-
import com.github.scribejava.core.model.*;
3+
import com.github.scribejava.core.model.HttpClient;
4+
import com.github.scribejava.core.model.OAuthConfig;
5+
import com.github.scribejava.core.model.OAuthRequestAsync;
6+
import com.github.scribejava.core.model.Response;
7+
import com.github.scribejava.core.model.Verb;
48
import com.github.scribejava.core.oauth.OAuth20Service;
59
import com.github.scribejava.core.oauth.OAuthService;
10+
import com.github.scribejava.core.utils.StreamUtils;
611
import okhttp3.HttpUrl;
712
import okhttp3.OkHttpClient;
813
import okhttp3.mockwebserver.MockResponse;
@@ -84,4 +89,25 @@ public void shouldSendPostRequest() throws Exception {
8489

8590
server.shutdown();
8691
}
92+
93+
@Test
94+
public void shouldReadResponseStream() throws Exception {
95+
String expectedResponseBody = "response body";
96+
97+
MockWebServer server = new MockWebServer();
98+
server.enqueue(new MockResponse().setBody(expectedResponseBody));
99+
server.start();
100+
101+
HttpUrl baseUrl = server.url("/testUrl");
102+
103+
OAuthRequestAsync request = new OAuthRequestAsync(Verb.GET, baseUrl.toString(), oAuthService);
104+
Response response = request.sendAsync(null).get(30, TimeUnit.SECONDS);
105+
106+
assertEquals(expectedResponseBody, StreamUtils.getStreamContents(response.getStream()));
107+
108+
RecordedRequest recordedRequest = server.takeRequest();
109+
assertEquals("GET", recordedRequest.getMethod());
110+
111+
server.shutdown();
112+
}
87113
}

0 commit comments

Comments
 (0)