X Tutup
Skip to content

Commit 68a73ac

Browse files
committed
really fix url encoding in POST payload, it was needed for 'application/x-www-form-urlencoded' Content-Type + unit tests (thanks to https://github.com/max904-github)
1 parent 8b86919 commit 68a73ac

File tree

7 files changed

+77
-102
lines changed

7 files changed

+77
-102
lines changed

changelog

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
[SNAPSHOT]
22
* Add Polar API (https://www.polar.com/) (thanks to https://github.com/vidi42)
33
* make Response accept resources to autoclose and autoclose it (thanks to https://github.com/drei01)
4-
* fix url encoding in POST payload, it wasn't needed (thanks to https://github.com/max904-github)
4+
* fix url encoding in POST payload (it's needed for 'application/x-www-form-urlencoded' Content-Type)
5+
+ unit tests (thanks to https://github.com/max904-github)
56

67
[6.9.0]
78
* Add Xero API (https://www.xero.com/) (thanks to https://github.com/SidneyAllen)

scribejava-apis/src/main/java/com/github/scribejava/apis/SalesforceApi.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717

1818
/**
1919
* This class is an implementation of the Salesforce OAuth2 API.
20-
* The default implementation connects to the Salesforce
21-
* production environment.
22-
* If you want to connect to a Sandbox environment you've to use {@link #sandbox()} method to
23-
* get sandbox instance of this API
20+
*
21+
* The default implementation connects to the Salesforce production environment. If you want to connect to a Sandbox
22+
* environment you've to use {@link #sandbox()} method to get sandbox instance of this API
2423
*/
2524
public class SalesforceApi extends DefaultApi20 {
2625

@@ -52,6 +51,7 @@ protected SalesforceApi(String hostName) {
5251
}
5352

5453
private static class InstanceHolder {
54+
5555
private static final SalesforceApi INSTANCE = new SalesforceApi(PRODUCTION_HOST);
5656
}
5757

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ public byte[] getByteArrayPayload() {
360360
if (byteArrayPayload != null) {
361361
return byteArrayPayload;
362362
}
363-
final String body = bodyParams.asString();
363+
final String body = bodyParams.asFormUrlEncodedString();
364364
try {
365365
return body.getBytes(getCharset());
366366
} catch (UnsupportedEncodingException uee) {

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ public String asUrlEncodedPair() {
1616
return OAuthEncoder.encode(key).concat("=").concat(OAuthEncoder.encode(value));
1717
}
1818

19-
public String asPair() {
20-
return key + '=' + value;
21-
}
22-
2319
@Override
2420
public boolean equals(Object other) {
2521
if (other == null) {

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,6 @@ public String asFormUrlEncodedString() {
6565
return builder.substring(1);
6666
}
6767

68-
public String asString() {
69-
if (params.isEmpty()) {
70-
return EMPTY_STRING;
71-
}
72-
73-
final StringBuilder builder = new StringBuilder();
74-
for (Parameter p : params) {
75-
builder.append(PARAM_SEPARATOR).append(p.asPair());
76-
}
77-
return builder.substring(1);
78-
}
79-
8068
public void addAll(ParameterList other) {
8169
params.addAll(other.getParams());
8270
}

scribejava-core/src/test/java/com/github/scribejava/core/AbstractClientTest.java

Lines changed: 18 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.concurrent.TimeUnit;
2020

2121
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertTrue;
23+
import static org.junit.Assert.assertNotNull;
2224

2325
public abstract class AbstractClientTest {
2426

@@ -78,32 +80,27 @@ public void shouldSendGetRequest() throws Exception {
7880
}
7981

8082
@Test
81-
public void shouldSendPostRequestWithEmptyBody() throws Exception {
82-
final String expectedResponseBody = "response body for test shouldSendPostRequest";
83-
final String expectedRequestBody = "";
84-
83+
public void shouldSendPostWithApplicationXWwwFormUrlencodedRequestContentTypeHeader() throws Exception {
8584
final MockWebServer server = new MockWebServer();
86-
server.enqueue(new MockResponse().setBody(expectedResponseBody));
85+
server.enqueue(new MockResponse());
8786
server.start();
8887

8988
final HttpUrl baseUrl = server.url("/testUrl");
9089

9190
final OAuthRequest request = new OAuthRequest(Verb.POST, baseUrl.toString());
92-
try (Response response = oAuthService.execute(request, null).get(30, TimeUnit.SECONDS)) {
93-
assertEquals(expectedResponseBody, response.getBody());
94-
}
91+
oAuthService.execute(request, null).get(30, TimeUnit.SECONDS).close();
9592

9693
final RecordedRequest recordedRequest = server.takeRequest();
9794
assertEquals("POST", recordedRequest.getMethod());
98-
assertEquals(expectedRequestBody, recordedRequest.getBody().readUtf8());
95+
assertEquals(HttpClient.DEFAULT_CONTENT_TYPE, recordedRequest.getHeader(HttpClient.CONTENT_TYPE));
9996

10097
server.shutdown();
10198
}
10299

103100
@Test
104-
public void shouldSendPostRequestWithStringBody() throws Exception {
101+
public void shouldSendPostRequestWithEmptyBody() throws Exception {
105102
final String expectedResponseBody = "response body for test shouldSendPostRequest";
106-
final String expectedRequestBody = "request body";
103+
final String expectedRequestBody = "";
107104

108105
final MockWebServer server = new MockWebServer();
109106
server.enqueue(new MockResponse().setBody(expectedResponseBody));
@@ -112,22 +109,22 @@ public void shouldSendPostRequestWithStringBody() throws Exception {
112109
final HttpUrl baseUrl = server.url("/testUrl");
113110

114111
final OAuthRequest request = new OAuthRequest(Verb.POST, baseUrl.toString());
115-
request.setPayload(expectedRequestBody);
116112
try (Response response = oAuthService.execute(request, null).get(30, TimeUnit.SECONDS)) {
117113
assertEquals(expectedResponseBody, response.getBody());
118114
}
119115

120116
final RecordedRequest recordedRequest = server.takeRequest();
121117
assertEquals("POST", recordedRequest.getMethod());
122118
assertEquals(expectedRequestBody, recordedRequest.getBody().readUtf8());
119+
assertEquals(HttpClient.DEFAULT_CONTENT_TYPE, recordedRequest.getHeader(HttpClient.CONTENT_TYPE));
123120

124121
server.shutdown();
125122
}
126123

127124
@Test
128-
public void shouldSendPostRequestWithStringBodyWithSpecialChars() throws Exception {
125+
public void shouldSendPostRequestWithStringBody() throws Exception {
129126
final String expectedResponseBody = "response body for test shouldSendPostRequest";
130-
final String expectedRequestBody = "~/!@#$%^&*()_+//\r\n%2F&";
127+
final String expectedRequestBody = "request body";
131128

132129
final MockWebServer server = new MockWebServer();
133130
server.enqueue(new MockResponse().setBody(expectedResponseBody));
@@ -144,6 +141,9 @@ public void shouldSendPostRequestWithStringBodyWithSpecialChars() throws Excepti
144141
final RecordedRequest recordedRequest = server.takeRequest();
145142
assertEquals("POST", recordedRequest.getMethod());
146143
assertEquals(expectedRequestBody, recordedRequest.getBody().readUtf8());
144+
final String contentTypeHeader = recordedRequest.getHeader(HttpClient.CONTENT_TYPE);
145+
assertNotNull(contentTypeHeader);
146+
assertTrue(contentTypeHeader.startsWith(HttpClient.DEFAULT_CONTENT_TYPE));
147147

148148
server.shutdown();
149149
}
@@ -168,65 +168,16 @@ public void shouldSendPostRequestWithByteBodyBody() throws Exception {
168168
final RecordedRequest recordedRequest = server.takeRequest();
169169
assertEquals("POST", recordedRequest.getMethod());
170170
assertEquals(expectedRequestBody, recordedRequest.getBody().readUtf8());
171-
172-
server.shutdown();
173-
}
174-
175-
@Test
176-
public void shouldSendPostRequestWithByteBodyWithSpecialChars() throws Exception {
177-
final String expectedResponseBody = "response body for test shouldSendPostRequest";
178-
final String expectedRequestBody = "~/!@#$%^&*()_+//\r\n%2F&";
179-
180-
final MockWebServer server = new MockWebServer();
181-
server.enqueue(new MockResponse().setBody(expectedResponseBody));
182-
server.start();
183-
184-
final HttpUrl baseUrl = server.url("/testUrl");
185-
186-
final OAuthRequest request = new OAuthRequest(Verb.POST, baseUrl.toString());
187-
request.setPayload(expectedRequestBody.getBytes());
188-
try (Response response = oAuthService.execute(request, null).get(30, TimeUnit.SECONDS)) {
189-
assertEquals(expectedResponseBody, response.getBody());
190-
}
191-
192-
final RecordedRequest recordedRequest = server.takeRequest();
193-
assertEquals("POST", recordedRequest.getMethod());
194-
assertEquals(expectedRequestBody, recordedRequest.getBody().readUtf8());
171+
assertEquals(HttpClient.DEFAULT_CONTENT_TYPE, recordedRequest.getHeader(HttpClient.CONTENT_TYPE));
195172

196173
server.shutdown();
197174
}
198175

199176
@Test
200177
public void shouldSendPostRequestWithBodyParamsBody() throws Exception {
201178
final String expectedResponseBody = "response body for test shouldSendPostRequest";
202-
final String expectedRequestBodyParamName = "request body param name";
203-
final String expectedRequestBodyParamValue = "request body param value";
204-
final String expectedRequestBody = expectedRequestBodyParamName + '=' + expectedRequestBodyParamValue;
205-
206-
final MockWebServer server = new MockWebServer();
207-
server.enqueue(new MockResponse().setBody(expectedResponseBody));
208-
server.start();
209-
210-
final HttpUrl baseUrl = server.url("/testUrl");
211-
212-
final OAuthRequest request = new OAuthRequest(Verb.POST, baseUrl.toString());
213-
request.addBodyParameter(expectedRequestBodyParamName, expectedRequestBodyParamValue);
214-
try (Response response = oAuthService.execute(request, null).get(30, TimeUnit.SECONDS)) {
215-
assertEquals(expectedResponseBody, response.getBody());
216-
}
217-
218-
final RecordedRequest recordedRequest = server.takeRequest();
219-
assertEquals("POST", recordedRequest.getMethod());
220-
assertEquals(expectedRequestBody, recordedRequest.getBody().readUtf8());
221-
222-
server.shutdown();
223-
}
224-
225-
@Test
226-
public void shouldSendPostRequestWithBodyParamsBodyWithSpecialChars() throws Exception {
227-
final String expectedResponseBody = "response body for test shouldSendPostRequest";
228-
final String expectedRequestBodyParamName = "~/!@#$%^&*()_+//\r\n%2F&name";
229-
final String expectedRequestBodyParamValue = "~/!@#$%^&*()_+//\r\n%2F&value";
179+
final String expectedRequestBodyParamName = "request_body_param_name";
180+
final String expectedRequestBodyParamValue = "request_body_param_value";
230181
final String expectedRequestBody = expectedRequestBodyParamName + '=' + expectedRequestBodyParamValue;
231182

232183
final MockWebServer server = new MockWebServer();
@@ -244,6 +195,7 @@ public void shouldSendPostRequestWithBodyParamsBodyWithSpecialChars() throws Exc
244195
final RecordedRequest recordedRequest = server.takeRequest();
245196
assertEquals("POST", recordedRequest.getMethod());
246197
assertEquals(expectedRequestBody, recordedRequest.getBody().readUtf8());
198+
assertEquals(HttpClient.DEFAULT_CONTENT_TYPE, recordedRequest.getHeader(HttpClient.CONTENT_TYPE));
247199

248200
server.shutdown();
249201
}
Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,43 @@
11
package com.github.scribejava.core.model;
22

3-
import static org.junit.Assert.assertEquals;
43
import static org.junit.Assert.assertTrue;
5-
import org.junit.Before;
64
import org.junit.Test;
7-
import java.net.MalformedURLException;
5+
import static org.junit.Assert.assertEquals;
6+
import static org.junit.Assert.assertArrayEquals;
87

98
public class RequestTest {
109

11-
private OAuthRequest getRequest;
12-
private OAuthRequest postRequest;
13-
14-
@Before
15-
public void setUp() throws MalformedURLException {
16-
postRequest = new OAuthRequest(Verb.POST, "http://example.com");
10+
@Test
11+
public void shouldGetQueryStringParameters() {
12+
final OAuthRequest postRequest = new OAuthRequest(Verb.POST, "http://example.com");
1713
postRequest.addBodyParameter("param", "value");
1814
postRequest.addBodyParameter("param with spaces", "value with spaces");
1915

20-
getRequest = new OAuthRequest(Verb.GET, "http://example.com?qsparam=value&other+param=value+with+spaces");
21-
}
16+
final OAuthRequest getRequest
17+
= new OAuthRequest(Verb.GET, "http://example.com?qsparam=value&other+param=value+with+spaces");
2218

23-
@Test
24-
public void shouldGetQueryStringParameters() {
2519
assertEquals(2, getRequest.getQueryStringParams().size());
2620
assertEquals(0, postRequest.getQueryStringParams().size());
2721
assertTrue(getRequest.getQueryStringParams().contains(new Parameter("qsparam", "value")));
2822
}
2923

3024
@Test
3125
public void shouldSetBodyParamsAndAddContentLength() {
32-
assertEquals("param=value&param with spaces=value with spaces", new String(postRequest.getByteArrayPayload()));
26+
final OAuthRequest postRequest = new OAuthRequest(Verb.POST, "http://example.com");
27+
postRequest.addBodyParameter("param", "value");
28+
postRequest.addBodyParameter("param with spaces", "value with spaces");
29+
30+
assertEquals("param=value&param%20with%20spaces=value%20with%20spaces",
31+
new String(postRequest.getByteArrayPayload()));
3332
}
3433

3534
@Test
3635
public void shouldSetPayloadAndHeaders() {
36+
final OAuthRequest postRequest = new OAuthRequest(Verb.POST, "http://example.com");
37+
postRequest.addBodyParameter("param", "value");
38+
postRequest.addBodyParameter("param with spaces", "value with spaces");
3739
postRequest.setPayload("PAYLOAD");
40+
3841
assertEquals("PAYLOAD", postRequest.getStringPayload());
3942
}
4043

@@ -56,6 +59,41 @@ public void shouldReturnTheCompleteUrl() {
5659

5760
@Test
5861
public void shouldHandleQueryStringSpaceEncodingProperly() {
62+
final OAuthRequest getRequest
63+
= new OAuthRequest(Verb.GET, "http://example.com?qsparam=value&other+param=value+with+spaces");
64+
5965
assertTrue(getRequest.getQueryStringParams().contains(new Parameter("other param", "value with spaces")));
6066
}
67+
68+
@Test
69+
public void shouldNotEncodeInStringPayload() throws Exception {
70+
final String requestBody = "~/!@#$%^&*( )_+//\r\n%2F&";
71+
72+
final OAuthRequest postRequest = new OAuthRequest(Verb.POST, "http://example.com");
73+
postRequest.setPayload(requestBody);
74+
75+
assertEquals(requestBody, postRequest.getStringPayload());
76+
}
77+
78+
@Test
79+
public void shouldNotEncodeInByteBodyPayload() throws Exception {
80+
final byte[] requestBody = "~/!@#$%^&*( )_+//\r\n%2F&".getBytes();
81+
82+
final OAuthRequest postRequest = new OAuthRequest(Verb.POST, "http://example.com");
83+
postRequest.setPayload(requestBody);
84+
85+
assertArrayEquals(requestBody, postRequest.getByteArrayPayload());
86+
}
87+
88+
@Test
89+
public void shouldEncodeInBodyParamsPayload() throws Exception {
90+
final String expectedRequestBodyParamName = "~/!@#$%^&*( )_+//\r\n%2F&name";
91+
final String expectedRequestBodyParamValue = "~/!@#$%^&*( )_+//\r\n%2F&value";
92+
final String expectedRequestBody = "~%2F%21%40%23%24%25%5E%26%2A%28%20%29_%2B%2F%2F%0D%0A%252F%26amp%3Bname="
93+
+ "~%2F%21%40%23%24%25%5E%26%2A%28%20%29_%2B%2F%2F%0D%0A%252F%26amp%3Bvalue";
94+
95+
final OAuthRequest postRequest = new OAuthRequest(Verb.POST, "http://example.com");
96+
postRequest.addBodyParameter(expectedRequestBodyParamName, expectedRequestBodyParamValue);
97+
assertArrayEquals(expectedRequestBody.getBytes(), postRequest.getByteArrayPayload());
98+
}
6199
}

0 commit comments

Comments
 (0)
X Tutup