Skip to content

Commit a1cb90a

Browse files
committed
prepare to move HTTP communications logic from OAuthRequest to separate package
1 parent 7450a60 commit a1cb90a

File tree

3 files changed

+18
-160
lines changed

3 files changed

+18
-160
lines changed

scribejava-core/src/main/java/com/github/scribejava/core/model/OAuthRequest.java

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@
22

33
import java.io.IOException;
44
import java.net.HttpURLConnection;
5-
import java.net.URL;
65
import java.util.Map;
76
import com.github.scribejava.core.exceptions.OAuthConnectionException;
87
import com.github.scribejava.core.exceptions.OAuthException;
98
import com.github.scribejava.core.oauth.OAuthService;
109
import java.io.File;
10+
import java.net.URL;
1111
import java.net.UnknownHostException;
1212
import java.util.HashMap;
1313
import java.util.List;
1414

1515
public class OAuthRequest extends AbstractRequest {
1616

17-
private HttpURLConnection connection;
18-
1917
private final OAuthConfig config;
2018

2119
public OAuthRequest(Verb verb, String url, OAuthConfig config) {
@@ -34,31 +32,32 @@ public OAuthRequest(Verb verb, String url, OAuthConfig config) {
3432
*/
3533
public Response send() {
3634
try {
37-
createConnection();
38-
return doSend();
35+
return doSend(config, isFollowRedirects(), getHeaders(), getVerb(), getCompleteUrl(), this);
3936
} catch (IOException | RuntimeException e) {
4037
throw new OAuthConnectionException(getCompleteUrl(), e);
4138
}
4239
}
4340

44-
Response doSend() throws IOException {
45-
final Verb verb = getVerb();
46-
connection.setRequestMethod(verb.name());
41+
private static Response doSend(OAuthConfig config, boolean followRedirects, Map<String, String> headers,
42+
Verb httpVerb, String completeUrl, OAuthRequest request) throws IOException {
43+
final HttpURLConnection connection = (HttpURLConnection) new URL(completeUrl).openConnection();
44+
connection.setInstanceFollowRedirects(followRedirects);
45+
connection.setRequestMethod(httpVerb.name());
4746
if (config.getConnectTimeout() != null) {
4847
connection.setConnectTimeout(config.getConnectTimeout());
4948
}
5049
if (config.getReadTimeout() != null) {
5150
connection.setReadTimeout(config.getReadTimeout());
5251
}
53-
addHeaders();
54-
if (hasBodyContent()) {
55-
final File filePayload = getFilePayload();
52+
addHeaders(connection, headers, config.getUserAgent());
53+
if (httpVerb == Verb.POST || httpVerb == Verb.PUT || httpVerb == Verb.DELETE) {
54+
final File filePayload = request.getFilePayload();
5655
if (filePayload != null) {
5756
throw new UnsupportedOperationException("Sync Requests do not support File payload for the moment");
58-
} else if (getStringPayload() != null) {
59-
addBody(getStringPayload().getBytes(getCharset()));
57+
} else if (request.getStringPayload() != null) {
58+
addBody(connection, request.getStringPayload().getBytes(request.getCharset()));
6059
} else {
61-
addBody(getByteArrayPayload());
60+
addBody(connection, request.getByteArrayPayload());
6261
}
6362
}
6463

@@ -86,25 +85,16 @@ private static Map<String, String> parseHeaders(HttpURLConnection conn) {
8685
return headers;
8786
}
8887

89-
private void createConnection() throws IOException {
90-
final String completeUrl = getCompleteUrl();
91-
if (connection == null) {
92-
connection = (HttpURLConnection) new URL(completeUrl).openConnection();
93-
connection.setInstanceFollowRedirects(isFollowRedirects());
94-
}
95-
}
96-
97-
void addHeaders() {
98-
for (Map.Entry<String, String> entry : getHeaders().entrySet()) {
88+
private static void addHeaders(HttpURLConnection connection, Map<String, String> headers, String userAgent) {
89+
for (Map.Entry<String, String> entry : headers.entrySet()) {
9990
connection.setRequestProperty(entry.getKey(), entry.getValue());
10091
}
101-
final String userAgent = config.getUserAgent();
10292
if (userAgent != null) {
10393
connection.setRequestProperty(OAuthConstants.USER_AGENT_HEADER_NAME, userAgent);
10494
}
10595
}
10696

107-
void addBody(byte[] content) throws IOException {
97+
private static void addBody(HttpURLConnection connection, byte[] content) throws IOException {
10898
connection.setRequestProperty(CONTENT_LENGTH, String.valueOf(content.length));
10999

110100
if (connection.getRequestProperty(CONTENT_TYPE) == null) {
@@ -113,8 +103,4 @@ void addBody(byte[] content) throws IOException {
113103
connection.setDoOutput(true);
114104
connection.getOutputStream().write(content);
115105
}
116-
117-
void setConnection(HttpURLConnection connection) {
118-
this.connection = connection;
119-
}
120106
}

scribejava-core/src/test/java/com/github/scribejava/core/model/ConnectionStub.java

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.github.scribejava.core.model;
22

33
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertFalse;
54
import static org.junit.Assert.assertTrue;
65
import org.junit.Before;
76
import org.junit.Test;
@@ -11,26 +10,18 @@ public class RequestTest {
1110

1211
private OAuthRequest getRequest;
1312
private OAuthRequest postRequest;
14-
private ConnectionStub connection;
1513
private OAuthConfig config;
1614

1715
@Before
1816
public void setUp() throws MalformedURLException {
19-
connection = new ConnectionStub();
2017
config = new OAuthConfig("test", "test");
18+
2119
postRequest = new OAuthRequest(Verb.POST, "http://example.com", config);
2220
postRequest.addBodyParameter("param", "value");
2321
postRequest.addBodyParameter("param with spaces", "value with spaces");
24-
postRequest.setConnection(connection);
22+
2523
getRequest = new OAuthRequest(Verb.GET, "http://example.com?qsparam=value&other+param=value+with+spaces",
2624
config);
27-
getRequest.setConnection(connection);
28-
}
29-
30-
@Test
31-
public void shouldSetRequestVerb() {
32-
getRequest.send();
33-
assertEquals("GET", connection.getRequestMethod());
3425
}
3526

3627
@Test
@@ -40,29 +31,16 @@ public void shouldGetQueryStringParameters() {
4031
assertTrue(getRequest.getQueryStringParams().contains(new Parameter("qsparam", "value")));
4132
}
4233

43-
@Test
44-
public void shouldAddRequestHeaders() {
45-
getRequest.addHeader("Header", "1");
46-
getRequest.addHeader("Header2", "2");
47-
getRequest.send();
48-
assertEquals(2, getRequest.getHeaders().size());
49-
assertEquals(2, connection.getHeaders().size());
50-
}
51-
5234
@Test
5335
public void shouldSetBodyParamsAndAddContentLength() {
5436
assertEquals("param=value&param%20with%20spaces=value%20with%20spaces",
5537
new String(postRequest.getByteArrayPayload()));
56-
postRequest.send();
57-
assertTrue(connection.getHeaders().containsKey("Content-Length"));
5838
}
5939

6040
@Test
6141
public void shouldSetPayloadAndHeaders() {
6242
postRequest.setPayload("PAYLOAD");
63-
postRequest.send();
6443
assertEquals("PAYLOAD", postRequest.getStringPayload());
65-
assertTrue(connection.getHeaders().containsKey("Content-Length"));
6644
}
6745

6846
@Test
@@ -85,37 +63,4 @@ public void shouldReturnTheCompleteUrl() {
8563
public void shouldHandleQueryStringSpaceEncodingProperly() {
8664
assertTrue(getRequest.getQueryStringParams().contains(new Parameter("other param", "value with spaces")));
8765
}
88-
89-
@Test
90-
public void shouldAutomaticallyAddContentTypeForPostRequestsWithBytePayload() {
91-
postRequest.setPayload("PAYLOAD".getBytes());
92-
postRequest.send();
93-
assertEquals(OAuthRequest.DEFAULT_CONTENT_TYPE, connection.getHeaders().get("Content-Type"));
94-
}
95-
96-
@Test
97-
public void shouldAutomaticallyAddContentTypeForPostRequestsWithStringPayload() {
98-
postRequest.setPayload("PAYLOAD");
99-
postRequest.send();
100-
assertEquals(OAuthRequest.DEFAULT_CONTENT_TYPE, connection.getHeaders().get("Content-Type"));
101-
}
102-
103-
@Test
104-
public void shouldAutomaticallyAddContentTypeForPostRequestsWithBodyParameters() {
105-
postRequest.send();
106-
assertEquals(OAuthRequest.DEFAULT_CONTENT_TYPE, connection.getHeaders().get("Content-Type"));
107-
}
108-
109-
@Test
110-
public void shouldBeAbleToOverrideItsContentType() {
111-
postRequest.addHeader("Content-Type", "my-content-type");
112-
postRequest.send();
113-
assertEquals("my-content-type", connection.getHeaders().get("Content-Type"));
114-
}
115-
116-
@Test
117-
public void shouldNotAddContentTypeForGetRequests() {
118-
getRequest.send();
119-
assertFalse(connection.getHeaders().containsKey("Content-Type"));
120-
}
12166
}

0 commit comments

Comments
 (0)