Skip to content

Commit 7f3a902

Browse files
committed
fix url encoding in POST payload, it wasn't needed (thanks to https://github.com/max904-github)
1 parent fdef16c commit 7f3a902

6 files changed

Lines changed: 162 additions & 11 deletions

File tree

changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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)
45

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

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.asFormUrlEncodedString();
363+
final String body = bodyParams.asString();
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ 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+
1923
@Override
2024
public boolean equals(Object other) {
2125
if (other == null) {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ 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+
6880
public void addAll(ParameterList other) {
6981
params.addAll(other.getParams());
7082
}

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

Lines changed: 143 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,37 +78,172 @@ public void shouldSendGetRequest() throws Exception {
7878
}
7979

8080
@Test
81-
public void shouldSendPostRequest() throws Exception {
81+
public void shouldSendPostRequestWithEmptyBody() throws Exception {
82+
final String expectedResponseBody = "response body for test shouldSendPostRequest";
83+
final String expectedRequestBody = "";
84+
85+
final MockWebServer server = new MockWebServer();
86+
server.enqueue(new MockResponse().setBody(expectedResponseBody));
87+
server.start();
88+
89+
final HttpUrl baseUrl = server.url("/testUrl");
90+
91+
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+
}
95+
96+
final RecordedRequest recordedRequest = server.takeRequest();
97+
assertEquals("POST", recordedRequest.getMethod());
98+
assertEquals(expectedRequestBody, recordedRequest.getBody().readUtf8());
99+
100+
server.shutdown();
101+
}
102+
103+
@Test
104+
public void shouldSendPostRequestWithStringBody() throws Exception {
82105
final String expectedResponseBody = "response body for test shouldSendPostRequest";
83106
final String expectedRequestBody = "request body";
84107

85108
final MockWebServer server = new MockWebServer();
86109
server.enqueue(new MockResponse().setBody(expectedResponseBody));
110+
server.start();
111+
112+
final HttpUrl baseUrl = server.url("/testUrl");
113+
114+
final OAuthRequest request = new OAuthRequest(Verb.POST, baseUrl.toString());
115+
request.setPayload(expectedRequestBody);
116+
try (Response response = oAuthService.execute(request, null).get(30, TimeUnit.SECONDS)) {
117+
assertEquals(expectedResponseBody, response.getBody());
118+
}
119+
120+
final RecordedRequest recordedRequest = server.takeRequest();
121+
assertEquals("POST", recordedRequest.getMethod());
122+
assertEquals(expectedRequestBody, recordedRequest.getBody().readUtf8());
123+
124+
server.shutdown();
125+
}
126+
127+
@Test
128+
public void shouldSendPostRequestWithStringBodyWithSpecialChars() throws Exception {
129+
final String expectedResponseBody = "response body for test shouldSendPostRequest";
130+
final String expectedRequestBody = "~/!@#$%^&*()_+//\r\n%2F&";
131+
132+
final MockWebServer server = new MockWebServer();
87133
server.enqueue(new MockResponse().setBody(expectedResponseBody));
88134
server.start();
89135

90136
final HttpUrl baseUrl = server.url("/testUrl");
91137

92-
// request with body
93-
OAuthRequest request = new OAuthRequest(Verb.POST, baseUrl.toString());
138+
final OAuthRequest request = new OAuthRequest(Verb.POST, baseUrl.toString());
94139
request.setPayload(expectedRequestBody);
95140
try (Response response = oAuthService.execute(request, null).get(30, TimeUnit.SECONDS)) {
96141
assertEquals(expectedResponseBody, response.getBody());
97142
}
98143

99-
RecordedRequest recordedRequest = server.takeRequest();
144+
final RecordedRequest recordedRequest = server.takeRequest();
145+
assertEquals("POST", recordedRequest.getMethod());
146+
assertEquals(expectedRequestBody, recordedRequest.getBody().readUtf8());
147+
148+
server.shutdown();
149+
}
150+
151+
@Test
152+
public void shouldSendPostRequestWithByteBodyBody() throws Exception {
153+
final String expectedResponseBody = "response body for test shouldSendPostRequest";
154+
final String expectedRequestBody = "request body";
155+
156+
final MockWebServer server = new MockWebServer();
157+
server.enqueue(new MockResponse().setBody(expectedResponseBody));
158+
server.start();
159+
160+
final HttpUrl baseUrl = server.url("/testUrl");
161+
162+
final OAuthRequest request = new OAuthRequest(Verb.POST, baseUrl.toString());
163+
request.setPayload(expectedRequestBody.getBytes());
164+
try (Response response = oAuthService.execute(request, null).get(30, TimeUnit.SECONDS)) {
165+
assertEquals(expectedResponseBody, response.getBody());
166+
}
167+
168+
final RecordedRequest recordedRequest = server.takeRequest();
169+
assertEquals("POST", recordedRequest.getMethod());
170+
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());
195+
196+
server.shutdown();
197+
}
198+
199+
@Test
200+
public void shouldSendPostRequestWithBodyParamsBody() throws Exception {
201+
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();
100219
assertEquals("POST", recordedRequest.getMethod());
101220
assertEquals(expectedRequestBody, recordedRequest.getBody().readUtf8());
102221

103-
// request with empty body
104-
request = new OAuthRequest(Verb.POST, baseUrl.toString());
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";
230+
final String expectedRequestBody = expectedRequestBodyParamName + '=' + expectedRequestBodyParamValue;
231+
232+
final MockWebServer server = new MockWebServer();
233+
server.enqueue(new MockResponse().setBody(expectedResponseBody));
234+
server.start();
235+
236+
final HttpUrl baseUrl = server.url("/testUrl");
237+
238+
final OAuthRequest request = new OAuthRequest(Verb.POST, baseUrl.toString());
239+
request.addBodyParameter(expectedRequestBodyParamName, expectedRequestBodyParamValue);
105240
try (Response response = oAuthService.execute(request, null).get(30, TimeUnit.SECONDS)) {
106241
assertEquals(expectedResponseBody, response.getBody());
107242
}
108243

109-
recordedRequest = server.takeRequest();
244+
final RecordedRequest recordedRequest = server.takeRequest();
110245
assertEquals("POST", recordedRequest.getMethod());
111-
assertEquals("", recordedRequest.getBody().readUtf8());
246+
assertEquals(expectedRequestBody, recordedRequest.getBody().readUtf8());
112247

113248
server.shutdown();
114249
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ public void shouldGetQueryStringParameters() {
2929

3030
@Test
3131
public void shouldSetBodyParamsAndAddContentLength() {
32-
assertEquals("param=value&param%20with%20spaces=value%20with%20spaces",
33-
new String(postRequest.getByteArrayPayload()));
32+
assertEquals("param=value&param with spaces=value with spaces", new String(postRequest.getByteArrayPayload()));
3433
}
3534

3635
@Test

0 commit comments

Comments
 (0)