| 1 |
package oauth.signpost.signature; |
| 2 |
|
| 3 |
import static org.junit.Assert.assertEquals; |
| 4 |
import static org.junit.Assert.assertNotNull; |
| 5 |
import static org.junit.Assert.assertTrue; |
| 6 |
import static org.mockito.Mockito.mock; |
| 7 |
import static org.mockito.Mockito.when; |
| 8 |
import oauth.signpost.SignpostTestBase; |
| 9 |
import oauth.signpost.http.HttpRequest; |
| 10 |
import oauth.signpost.http.HttpParameters; |
| 11 |
|
| 12 |
import org.junit.Test; |
| 13 |
import org.junit.runner.RunWith; |
| 14 |
import org.mockito.runners.MockitoJUnit44Runner; |
| 15 |
|
| 16 |
@RunWith(MockitoJUnit44Runner.class) |
| 17 |
public class SignatureBaseStringTest extends SignpostTestBase { |
| 18 |
|
| 19 |
@Test |
| 20 |
public void shouldConsistOf3NonEmptyPartsConcatenatedWithAmpersand() |
| 21 |
throws Exception { |
| 22 |
SignatureBaseString sbs = new SignatureBaseString(httpPostMock, OAUTH_PARAMS); |
| 23 |
String result = sbs.generate(); |
| 24 |
|
| 25 |
String[] parts = result.split("&"); |
| 26 |
|
| 27 |
assertEquals(3, parts.length); |
| 28 |
assertNotNull(parts[0]); |
| 29 |
assertNotNull(parts[1]); |
| 30 |
assertNotNull(parts[2]); |
| 31 |
} |
| 32 |
|
| 33 |
@Test |
| 34 |
public void shouldStartWithUppercaseHttpMethod() throws Exception { |
| 35 |
assertTrue(new SignatureBaseString(httpPostMock, EMPTY_PARAMS).generate().split("&")[0] |
| 36 |
.equals("POST")); |
| 37 |
|
| 38 |
assertTrue(new SignatureBaseString(httpGetMock, EMPTY_PARAMS).generate().split("&")[0] |
| 39 |
.equals("GET")); |
| 40 |
} |
| 41 |
|
| 42 |
@Test |
| 43 |
public void shouldNormalizeRequestUrl() throws Exception { |
| 44 |
// must include scheme and authority in lowercase letters, |
| 45 |
// plus non HTTP(S) port, plus path, |
| 46 |
// but must ignore query params and fragment |
| 47 |
when(httpGetMock.getRequestUrl()) |
| 48 |
.thenReturn("HTTP://www.Example.Com:123/test?q=1#fragment"); |
| 49 |
assertEquals("http://www.example.com:123/test", new SignatureBaseString(httpGetMock, |
| 50 |
OAUTH_PARAMS).normalizeRequestUrl()); |
| 51 |
|
| 52 |
// must exclude HTTP(S) default ports |
| 53 |
when(httpGetMock.getRequestUrl()).thenReturn("http://example.com:80"); |
| 54 |
assertEquals("http://example.com/", new SignatureBaseString(httpGetMock, EMPTY_PARAMS) |
| 55 |
.normalizeRequestUrl()); |
| 56 |
when(httpGetMock.getRequestUrl()).thenReturn("https://example.com:443"); |
| 57 |
assertEquals("https://example.com/", new SignatureBaseString(httpGetMock, EMPTY_PARAMS) |
| 58 |
.normalizeRequestUrl()); |
| 59 |
} |
| 60 |
|
| 61 |
@Test |
| 62 |
public void shouldNormalizeParameters() throws Exception { |
| 63 |
|
| 64 |
// should ignore signature, callback, and realm params |
| 65 |
HttpParameters params = new HttpParameters(); |
| 66 |
params.put("a", "1", true); |
| 67 |
params.put("realm", "www.example.com", true); |
| 68 |
params.put("oauth_signature", "12345", true); |
| 69 |
String result = new SignatureBaseString(httpGetMock, params).normalizeRequestParameters(); |
| 70 |
assertEquals("a=1", result); |
| 71 |
|
| 72 |
// example from OAuth spec |
| 73 |
params = new HttpParameters(); |
| 74 |
params.put("a", "1", true); |
| 75 |
params.put("c", "hi there", true); |
| 76 |
params.put("f", "25", true); |
| 77 |
params.put("f", "50", true); |
| 78 |
params.put("f", "a", true); |
| 79 |
params.put("z", "p", true); |
| 80 |
params.put("z", "t", true); |
| 81 |
String expected = "a=1&c=hi%20there&f=25&f=50&f=a&z=p&z=t"; |
| 82 |
result = new SignatureBaseString(httpGetMock, params).normalizeRequestParameters(); |
| 83 |
assertEquals(expected, result); |
| 84 |
|
| 85 |
// examples from the official test cases on |
| 86 |
// http://oauth.pbwiki.com/TestCases |
| 87 |
params = new HttpParameters(); |
| 88 |
params.put("a", "x!y", true); |
| 89 |
params.put("a", "x y", true); |
| 90 |
expected = "a=x%20y&a=x%21y"; |
| 91 |
result = new SignatureBaseString(httpGetMock, params).normalizeRequestParameters(); |
| 92 |
assertEquals(expected, result); |
| 93 |
|
| 94 |
params = new HttpParameters(); |
| 95 |
params.put("name", "", true); |
| 96 |
assertEquals("name=", new SignatureBaseString(httpGetMock, params) |
| 97 |
.normalizeRequestParameters()); |
| 98 |
params.putNull("name", null); |
| 99 |
assertEquals("name=", new SignatureBaseString(httpGetMock, params) |
| 100 |
.normalizeRequestParameters()); |
| 101 |
} |
| 102 |
|
| 103 |
@Test |
| 104 |
public void shouldEncodeAndConcatenateAllSignatureParts() throws Exception { |
| 105 |
HttpRequest request = mock(HttpRequest.class); |
| 106 |
when(request.getMethod()).thenReturn("GET"); |
| 107 |
when(request.getRequestUrl()).thenReturn("http://example.com"); |
| 108 |
|
| 109 |
HttpParameters params = new HttpParameters(); |
| 110 |
params.put("a", "1"); |
| 111 |
|
| 112 |
SignatureBaseString sbs = new SignatureBaseString(request, params); |
| 113 |
|
| 114 |
//TODO: Is it correct that a trailing slash is always added to the |
| 115 |
//request URL authority if the path is empty? |
| 116 |
assertEquals("GET&http%3A%2F%2Fexample.com%2F&a%3D1", sbs.generate()); |
| 117 |
} |
| 118 |
|
| 119 |
@Test |
| 120 |
public void shouldWorkWithBracketsInParameterName() throws Exception { |
| 121 |
HttpRequest request = mock(HttpRequest.class); |
| 122 |
when(request.getMethod()).thenReturn("GET"); |
| 123 |
when(request.getRequestUrl()).thenReturn("http://examplebrackets.com"); |
| 124 |
|
| 125 |
HttpParameters params = new HttpParameters(); |
| 126 |
params.put("a[]", "1", true); |
| 127 |
|
| 128 |
SignatureBaseString sbs = new SignatureBaseString(request, params); |
| 129 |
|
| 130 |
assertEquals("GET&http%3A%2F%2Fexamplebrackets.com%2F&a%255B%255D%3D1", sbs.generate()); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
@Test |
| 135 |
public void shouldWorkWithMultipleParametersWithBracketsOfSameName() throws Exception { |
| 136 |
HttpRequest request = mock(HttpRequest.class); |
| 137 |
when(request.getMethod()).thenReturn("GET"); |
| 138 |
when(request.getRequestUrl()).thenReturn("http://examplemultiple.com"); |
| 139 |
|
| 140 |
HttpParameters params = new HttpParameters(); |
| 141 |
params.put("a[]", "1", true); |
| 142 |
params.put("a[]", "2", true); |
| 143 |
|
| 144 |
SignatureBaseString sbs = new SignatureBaseString(request, params); |
| 145 |
|
| 146 |
assertEquals("GET&http%3A%2F%2Fexamplemultiple.com%2F&a%255B%255D%3D1%26a%255B%255D%3D2", sbs.generate()); |
| 147 |
} |
| 148 |
} |