Skip to content

Commit 2f5d9b1

Browse files
committed
Merge branch 'apache-httpclient' of https://github.com/sschwieb/scribejava into sschwieb-apache-httpclient
2 parents 73c29f5 + 5ee2173 commit 2f5d9b1

File tree

19 files changed

+762
-110
lines changed

19 files changed

+762
-110
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<module>scribejava-httpclient-ahc</module>
2121
<module>scribejava-httpclient-ning</module>
2222
<module>scribejava-httpclient-okhttp</module>
23+
<module>scribejava-httpclient-apache</module>
2324
</modules>
2425

2526
<licenses>

scribejava-core/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@
1414
<name>ScribeJava Core</name>
1515
<packaging>jar</packaging>
1616

17+
<dependencies>
18+
<dependency>
19+
<groupId>com.squareup.okhttp3</groupId>
20+
<artifactId>mockwebserver</artifactId>
21+
<version>3.8.1</version>
22+
<scope>test</scope>
23+
</dependency>
24+
</dependencies>
25+
1726
<build>
1827
<plugins>
1928
<plugin>
@@ -23,6 +32,13 @@
2332
<plugin>
2433
<groupId>org.apache.maven.plugins</groupId>
2534
<artifactId>maven-jar-plugin</artifactId>
35+
<executions>
36+
<execution>
37+
<goals>
38+
<goal>test-jar</goal>
39+
</goals>
40+
</execution>
41+
</executions>
2642
</plugin>
2743
</plugins>
2844
</build>

scribejava-core/src/main/java/com/github/scribejava/core/httpclient/jdk/JDKHttpClient.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ public void close() throws IOException {
3333
public <T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
3434
byte[] bodyContents, OAuthAsyncRequestCallback<T> callback, OAuthRequest.ResponseConverter<T> converter) {
3535
try {
36-
final T response = converter.convert(execute(userAgent, headers, httpVerb, completeUrl, bodyContents));
37-
callback.onCompleted(response);
38-
return new JDKHttpFuture<>(response);
36+
final Response response = execute(userAgent, headers, httpVerb, completeUrl, bodyContents);
37+
@SuppressWarnings("unchecked")
38+
final T t = converter == null ? (T) response : converter.convert(response);
39+
if (callback != null) {
40+
callback.onCompleted(t);
41+
}
42+
return new JDKHttpFuture<>(t);
3943
} catch (InterruptedException | ExecutionException | IOException e) {
4044
callback.onThrowable(e);
4145
return new JDKHttpFuture<>(e);
@@ -46,11 +50,13 @@ public <T> Future<T> executeAsync(String userAgent, Map<String, String> headers,
4650
public <T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
4751
String bodyContents, OAuthAsyncRequestCallback<T> callback, OAuthRequest.ResponseConverter<T> converter) {
4852
try {
49-
final T response = converter.convert(execute(userAgent, headers, httpVerb, completeUrl, bodyContents));
53+
final Response response = execute(userAgent, headers, httpVerb, completeUrl, bodyContents);
54+
@SuppressWarnings("unchecked")
55+
final T t = converter == null ? (T) response : converter.convert(response);
5056
if (callback != null) {
51-
callback.onCompleted(response);
57+
callback.onCompleted(t);
5258
}
53-
return new JDKHttpFuture<>(response);
59+
return new JDKHttpFuture<>(t);
5460
} catch (InterruptedException | ExecutionException | IOException e) {
5561
if (callback != null) {
5662
callback.onThrowable(e);
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package com.github.scribejava.core;
2+
3+
import com.github.scribejava.core.httpclient.HttpClient;
4+
import com.github.scribejava.core.model.OAuthAsyncRequestCallback;
5+
import com.github.scribejava.core.model.OAuthConfig;
6+
import com.github.scribejava.core.model.OAuthRequest;
7+
import com.github.scribejava.core.model.Response;
8+
import com.github.scribejava.core.model.Verb;
9+
import com.github.scribejava.core.oauth.OAuth20Service;
10+
import com.github.scribejava.core.oauth.OAuthService;
11+
import com.github.scribejava.core.utils.StreamUtils;
12+
import okhttp3.HttpUrl;
13+
import okhttp3.mockwebserver.MockResponse;
14+
import okhttp3.mockwebserver.MockWebServer;
15+
import okhttp3.mockwebserver.RecordedRequest;
16+
import org.junit.After;
17+
import org.junit.Before;
18+
import org.junit.Test;
19+
20+
import java.io.IOException;
21+
import java.util.concurrent.TimeUnit;
22+
23+
import static org.junit.Assert.assertEquals;
24+
25+
public abstract class AbstractClientTest {
26+
27+
class TestCallback<T> implements OAuthAsyncRequestCallback<T> {
28+
29+
private Throwable throwable;
30+
private T response;
31+
32+
@Override
33+
public void onCompleted(T response) {
34+
this.response = response;
35+
}
36+
37+
@Override
38+
public void onThrowable(Throwable throwable) {
39+
this.throwable = throwable;
40+
}
41+
}
42+
43+
private OAuthService<?> oAuthService;
44+
private HttpClient client;
45+
46+
@Before
47+
public void setUp() {
48+
client = createNewClient();
49+
oAuthService = new OAuth20Service(null,
50+
new OAuthConfig("test", "test", null, null, null, null, null, null, null, client));
51+
}
52+
53+
@After
54+
public void shutDown() throws IOException {
55+
client.close();
56+
}
57+
58+
protected abstract HttpClient createNewClient();
59+
60+
@Test
61+
public void shouldSendGetRequest() throws Exception {
62+
final String expectedResponseBody = "response body";
63+
64+
final MockWebServer server = new MockWebServer();
65+
server.enqueue(new MockResponse().setBody(expectedResponseBody));
66+
server.start();
67+
68+
final HttpUrl baseUrl = server.url("/testUrl");
69+
70+
final OAuthRequest request = new OAuthRequest(Verb.GET, baseUrl.toString());
71+
final Response response = oAuthService.execute(request, null).get(30, TimeUnit.SECONDS);
72+
73+
assertEquals(expectedResponseBody, response.getBody());
74+
75+
final RecordedRequest recordedRequest = server.takeRequest();
76+
assertEquals("GET", recordedRequest.getMethod());
77+
78+
server.shutdown();
79+
}
80+
81+
@Test
82+
public void shouldSendPostRequest() throws Exception {
83+
final String expectedResponseBody = "response body";
84+
final String expectedRequestBody = "request body";
85+
86+
final MockWebServer server = new MockWebServer();
87+
server.enqueue(new MockResponse().setBody(expectedResponseBody));
88+
server.enqueue(new MockResponse().setBody(expectedResponseBody));
89+
server.start();
90+
91+
final HttpUrl baseUrl = server.url("/testUrl");
92+
93+
// request with body
94+
OAuthRequest request = new OAuthRequest(Verb.POST, baseUrl.toString());
95+
request.setPayload(expectedRequestBody);
96+
Response response = oAuthService.execute(request, null).get(30, TimeUnit.SECONDS);
97+
98+
assertEquals(expectedResponseBody, response.getBody());
99+
100+
RecordedRequest recordedRequest = server.takeRequest();
101+
assertEquals("POST", recordedRequest.getMethod());
102+
assertEquals(expectedRequestBody, recordedRequest.getBody().readUtf8());
103+
104+
105+
// request with empty body
106+
request = new OAuthRequest(Verb.POST, baseUrl.toString());
107+
response = oAuthService.execute(request, null).get(30, TimeUnit.SECONDS);
108+
109+
assertEquals(expectedResponseBody, response.getBody());
110+
111+
recordedRequest = server.takeRequest();
112+
assertEquals("POST", recordedRequest.getMethod());
113+
assertEquals("", recordedRequest.getBody().readUtf8());
114+
115+
server.shutdown();
116+
}
117+
118+
@Test
119+
public void shouldReadResponseStream() throws Exception {
120+
final String expectedResponseBody = "response body";
121+
122+
final MockWebServer server = new MockWebServer();
123+
server.enqueue(new MockResponse().setBody(expectedResponseBody));
124+
server.start();
125+
126+
final HttpUrl baseUrl = server.url("/testUrl");
127+
128+
final OAuthRequest request = new OAuthRequest(Verb.GET, baseUrl.toString());
129+
final Response response = oAuthService.execute(request, null).get(30, TimeUnit.SECONDS);
130+
131+
assertEquals(expectedResponseBody, StreamUtils.getStreamContents(response.getStream()));
132+
133+
final RecordedRequest recordedRequest = server.takeRequest();
134+
assertEquals("GET", recordedRequest.getMethod());
135+
136+
server.shutdown();
137+
}
138+
139+
@Test
140+
public void shouldCallCallback() throws Exception {
141+
final String expectedResponseBody = "response body";
142+
143+
final MockWebServer server = new MockWebServer();
144+
server.enqueue(new MockResponse().setBody(expectedResponseBody));
145+
server.start();
146+
147+
final HttpUrl baseUrl = server.url("/testUrl");
148+
149+
final OAuthRequest request = new OAuthRequest(Verb.GET, baseUrl.toString());
150+
151+
final TestCallback<Response> callback = new TestCallback<>();
152+
oAuthService.execute(request, callback).get();
153+
154+
assertEquals(expectedResponseBody, callback.response.getBody());
155+
156+
server.shutdown();
157+
}
158+
159+
@Test
160+
public void shouldPassErrors() throws Exception {
161+
162+
final MockWebServer server = new MockWebServer();
163+
server.enqueue(new MockResponse().setResponseCode(500));
164+
server.start();
165+
166+
final HttpUrl baseUrl = server.url("/testUrl");
167+
168+
final OAuthRequest request = new OAuthRequest(Verb.GET, baseUrl.toString());
169+
170+
final TestCallback<Response> callback = new TestCallback<>();
171+
final Response response = oAuthService.execute(request, callback).get();
172+
173+
assertEquals(500, response.getCode());
174+
assertEquals(500, callback.response.getCode());
175+
176+
server.shutdown();
177+
}
178+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.github.scribejava.core.httpclient.jdk;
2+
3+
import com.github.scribejava.core.AbstractClientTest;
4+
import com.github.scribejava.core.httpclient.HttpClient;
5+
6+
public class JDKHttpClientTest extends AbstractClientTest {
7+
8+
@Override
9+
protected HttpClient createNewClient() {
10+
return new JDKHttpClient(JDKHttpClientConfig.defaultConfig());
11+
}
12+
}

scribejava-httpclient-ahc/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@
2525
<artifactId>async-http-client</artifactId>
2626
<version>2.0.37</version>
2727
</dependency>
28+
<dependency>
29+
<groupId>com.github.scribejava</groupId>
30+
<artifactId>scribejava-core</artifactId>
31+
<version>${project.version}</version>
32+
<type>test-jar</type>
33+
<scope>test</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>com.squareup.okhttp3</groupId>
37+
<artifactId>mockwebserver</artifactId>
38+
<version>3.8.1</version>
39+
<scope>test</scope>
40+
</dependency>
2841
</dependencies>
2942

3043
<build>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.github.scribejava.httpclient.ahc;
2+
3+
import com.github.scribejava.core.AbstractClientTest;
4+
import com.github.scribejava.core.httpclient.HttpClient;
5+
6+
public class AhcHttpClientTest extends AbstractClientTest {
7+
8+
@Override
9+
protected HttpClient createNewClient() {
10+
return new AhcHttpClient(AhcHttpClientConfig.defaultConfig());
11+
}
12+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>com.github.scribejava</groupId>
7+
<artifactId>scribejava</artifactId>
8+
<version>4.2.1-SNAPSHOT</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
12+
<groupId>com.github.scribejava</groupId>
13+
<artifactId>scribejava-httpclient-apache</artifactId>
14+
<name>ScribeJava Apache Http Client support</name>
15+
<packaging>jar</packaging>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.github.scribejava</groupId>
20+
<artifactId>scribejava-core</artifactId>
21+
<version>${project.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.apache.httpcomponents</groupId>
25+
<artifactId>httpclient</artifactId>
26+
<version>4.5.2</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.apache.httpcomponents</groupId>
30+
<artifactId>httpasyncclient</artifactId>
31+
<version>4.1.3</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.github.scribejava</groupId>
35+
<artifactId>scribejava-core</artifactId>
36+
<version>${project.version}</version>
37+
<type>test-jar</type>
38+
<scope>test</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>com.squareup.okhttp3</groupId>
42+
<artifactId>mockwebserver</artifactId>
43+
<version>3.8.1</version>
44+
<scope>test</scope>
45+
</dependency>
46+
</dependencies>
47+
48+
<build>
49+
<plugins>
50+
<plugin>
51+
<groupId>org.apache.felix</groupId>
52+
<artifactId>maven-bundle-plugin</artifactId>
53+
</plugin>
54+
<plugin>
55+
<groupId>org.apache.maven.plugins</groupId>
56+
<artifactId>maven-jar-plugin</artifactId>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
</project>

0 commit comments

Comments
 (0)