forked from scribejava/scribejava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestTest.java
More file actions
99 lines (78 loc) · 4.27 KB
/
Copy pathRequestTest.java
File metadata and controls
99 lines (78 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.github.scribejava.core.model;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;
public class RequestTest {
@Test
public void shouldGetQueryStringParameters() {
final OAuthRequest postRequest = new OAuthRequest(Verb.POST, "http://example.com");
postRequest.addBodyParameter("param", "value");
postRequest.addBodyParameter("param with spaces", "value with spaces");
final OAuthRequest getRequest
= new OAuthRequest(Verb.GET, "http://example.com?qsparam=value&other+param=value+with+spaces");
assertEquals(2, getRequest.getQueryStringParams().size());
assertEquals(0, postRequest.getQueryStringParams().size());
assertTrue(getRequest.getQueryStringParams().contains(new Parameter("qsparam", "value")));
}
@Test
public void shouldSetBodyParamsAndAddContentLength() {
final OAuthRequest postRequest = new OAuthRequest(Verb.POST, "http://example.com");
postRequest.addBodyParameter("param", "value");
postRequest.addBodyParameter("param with spaces", "value with spaces");
assertEquals("param=value¶m%20with%20spaces=value%20with%20spaces",
new String(postRequest.getByteArrayPayload()));
}
@Test
public void shouldSetPayloadAndHeaders() {
final OAuthRequest postRequest = new OAuthRequest(Verb.POST, "http://example.com");
postRequest.addBodyParameter("param", "value");
postRequest.addBodyParameter("param with spaces", "value with spaces");
postRequest.setPayload("PAYLOAD");
assertEquals("PAYLOAD", postRequest.getStringPayload());
}
@Test
public void shouldAllowAddingQuerystringParametersAfterCreation() {
final OAuthRequest request = new OAuthRequest(Verb.GET, "http://example.com?one=val");
request.addQuerystringParameter("two", "other val");
request.addQuerystringParameter("more", "params");
assertEquals(3, request.getQueryStringParams().size());
}
@Test
public void shouldReturnTheCompleteUrl() {
final OAuthRequest request = new OAuthRequest(Verb.GET, "http://example.com?one=val");
request.addQuerystringParameter("two", "other val");
request.addQuerystringParameter("more", "params");
assertEquals("http://example.com?one=val&two=other%20val&more=params", request.getCompleteUrl());
}
@Test
public void shouldHandleQueryStringSpaceEncodingProperly() {
final OAuthRequest getRequest
= new OAuthRequest(Verb.GET, "http://example.com?qsparam=value&other+param=value+with+spaces");
assertTrue(getRequest.getQueryStringParams().contains(new Parameter("other param", "value with spaces")));
}
@Test
public void shouldNotEncodeInStringPayload() throws Exception {
final String requestBody = "~/!@#$%^&*( )_+//\r\n%2F&";
final OAuthRequest postRequest = new OAuthRequest(Verb.POST, "http://example.com");
postRequest.setPayload(requestBody);
assertEquals(requestBody, postRequest.getStringPayload());
}
@Test
public void shouldNotEncodeInByteBodyPayload() throws Exception {
final byte[] requestBody = "~/!@#$%^&*( )_+//\r\n%2F&".getBytes();
final OAuthRequest postRequest = new OAuthRequest(Verb.POST, "http://example.com");
postRequest.setPayload(requestBody);
assertArrayEquals(requestBody, postRequest.getByteArrayPayload());
}
@Test
public void shouldEncodeInBodyParamsPayload() throws Exception {
final String expectedRequestBodyParamName = "~/!@#$%^&*( )_+//\r\n%2F&name";
final String expectedRequestBodyParamValue = "~/!@#$%^&*( )_+//\r\n%2F&value";
final String expectedRequestBody = "~%2F%21%40%23%24%25%5E%26%2A%28%20%29_%2B%2F%2F%0D%0A%252F%26amp%3Bname="
+ "~%2F%21%40%23%24%25%5E%26%2A%28%20%29_%2B%2F%2F%0D%0A%252F%26amp%3Bvalue";
final OAuthRequest postRequest = new OAuthRequest(Verb.POST, "http://example.com");
postRequest.addBodyParameter(expectedRequestBodyParamName, expectedRequestBodyParamValue);
assertArrayEquals(expectedRequestBody.getBytes(), postRequest.getByteArrayPayload());
}
}