39
39
*
40
40
*/
41
41
public class OAuthSignatureCalculatorTest {
42
+
42
43
private static final String CONSUMER_KEY = "dpf43f3p2l4k3l03" ;
43
44
44
45
private static final String CONSUMER_SECRET = "kd94hf93k423kf44" ;
@@ -52,16 +53,16 @@ public class OAuthSignatureCalculatorTest {
52
53
final static long TIMESTAMP = 1191242096 ;
53
54
54
55
private static class StaticOAuthSignatureCalculator extends OAuthSignatureCalculator {
55
-
56
+
56
57
private final long timestamp ;
57
58
private final String nonce ;
58
-
59
+
59
60
public StaticOAuthSignatureCalculator (ConsumerKey consumerAuth , RequestToken userAuth , long timestamp , String nonce ) {
60
61
super (consumerAuth , userAuth );
61
- this .timestamp = timestamp ;
62
+ this .timestamp = timestamp ;
62
63
this .nonce = nonce ;
63
64
}
64
-
65
+
65
66
@ Override
66
67
protected long generateTimestamp () {
67
68
return timestamp ;
@@ -72,7 +73,62 @@ protected String generateNonce() {
72
73
return nonce ;
73
74
}
74
75
}
75
-
76
+
77
+ // sample from RFC https://tools.ietf.org/html/rfc5849#section-3.4.1
78
+ private void testSignatureBaseString (Request request ) {
79
+ ConsumerKey consumer = new ConsumerKey ("9djdj82h48djs9d2" , CONSUMER_SECRET );
80
+ RequestToken user = new RequestToken ("kkk9d7dh3k39sjv7" , TOKEN_SECRET );
81
+ OAuthSignatureCalculator calc = new OAuthSignatureCalculator (consumer , user );
82
+
83
+ String signatureBaseString = calc .signatureBaseString (//
84
+ request .getMethod (),//
85
+ request .getUri (),//
86
+ 137131201 ,//
87
+ "7d8f3e4a" ,//
88
+ request .getFormParams (),//
89
+ request .getQueryParams ()).toString ();
90
+
91
+ assertEquals (signatureBaseString , "POST&" //
92
+ + "http%3A%2F%2Fexample.com%2Frequest" //
93
+ + "&a2%3Dr%2520b%26" //
94
+ + "a3%3D2%2520q%26" + "a3%3Da%26" //
95
+ + "b5%3D%253D%25253D%26" //
96
+ + "c%2540%3D%26" //
97
+ + "c2%3D%26" //
98
+ + "oauth_consumer_key%3D9djdj82h48djs9d2%26" //
99
+ + "oauth_nonce%3D7d8f3e4a%26" //
100
+ + "oauth_signature_method%3DHMAC-SHA1%26" //
101
+ + "oauth_timestamp%3D137131201%26" //
102
+ + "oauth_token%3Dkkk9d7dh3k39sjv7%26" //
103
+ + "oauth_version%3D1.0" );
104
+ }
105
+
106
+ @ Test (groups = "fast" )
107
+ public void testSignatureBaseStringWithProperlyEncodedUri () {
108
+
109
+ Request request = new RequestBuilder ("POST" )//
110
+ .setUrl ("http://example.com/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b" )//
111
+ .addFormParam ("c2" , "" )//
112
+ .addFormParam ("a3" , "2 q" )//
113
+ .build ();
114
+
115
+ testSignatureBaseString (request );
116
+ }
117
+
118
+ @ Test (groups = "fast" )
119
+ public void testSignatureBaseStringWithRawUri () {
120
+
121
+ // note: @ is legal so don't decode it into %40 because it won't be encoded back
122
+ // note: we don't know how to fix a = that should have been encoded as %3D but who would be stupid enough to do that?
123
+ Request request = new RequestBuilder ("POST" )//
124
+ .setUrl ("http://example.com/request?b5=%3D%253D&a3=a&c%40=&a2=r b" )//
125
+ .addFormParam ("c2" , "" )//
126
+ .addFormParam ("a3" , "2 q" )//
127
+ .build ();
128
+
129
+ testSignatureBaseString (request );
130
+ }
131
+
76
132
// based on the reference test case from
77
133
// http://oauth.pbwiki.com/TestCases
78
134
@ Test (groups = "fast" )
@@ -99,10 +155,11 @@ public void testPostCalculateSignature() {
99
155
formParams .add (new Param ("file" , "vacation.jpg" ));
100
156
formParams .add (new Param ("size" , "original" ));
101
157
String url = "http://photos.example.net/photos" ;
102
- final Request req = new RequestBuilder ("POST" )
103
- .setUri (Uri .create (url ))
104
- .setFormParams (formParams )
105
- .setSignatureCalculator (calc ).build ();
158
+ final Request req = new RequestBuilder ("POST" )//
159
+ .setUri (Uri .create (url ))//
160
+ .setFormParams (formParams )//
161
+ .setSignatureCalculator (calc )//
162
+ .build ();
106
163
107
164
// From the signature tester, POST should look like:
108
165
// normalized parameters: file=vacation.jpg&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_nonce=kllo9940pd9333jh&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1191242096&oauth_token=nnch734d00sl2jdk&oauth_version=1.0&size=original
@@ -135,14 +192,15 @@ public void testGetWithRequestBuilder() {
135
192
queryParams .add (new Param ("size" , "original" ));
136
193
String url = "http://photos.example.net/photos" ;
137
194
138
- final Request req = new RequestBuilder ("GET" )
139
- .setUri (Uri .create (url ))
140
- .setQueryParams (queryParams )
141
- .setSignatureCalculator (calc ).build ();
195
+ final Request req = new RequestBuilder ("GET" )//
196
+ .setUri (Uri .create (url ))//
197
+ .setQueryParams (queryParams )//
198
+ .setSignatureCalculator (calc )//
199
+ .build ();
142
200
143
201
final List <Param > params = req .getQueryParams ();
144
202
assertEquals (params .size (), 2 );
145
-
203
+
146
204
// From the signature tester, the URL should look like:
147
205
//normalized parameters: file=vacation.jpg&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_nonce=kllo9940pd9333jh&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1191242096&oauth_token=nnch734d00sl2jdk&oauth_version=1.0&size=original
148
206
//signature base string: GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal
0 commit comments