Skip to content

Commit 9596249

Browse files
committed
Clean up OAuthSignatureCalculator test: override generation methods
1 parent 8fe9993 commit 9596249

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

api/src/main/java/org/asynchttpclient/oauth/OAuthSignatureCalculator.java

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ public class OAuthSignatureCalculator implements SignatureCalculator {
7171

7272
protected final RequestToken userAuth;
7373

74-
protected final Long testTimestamp;
75-
76-
protected final String testNonce;
77-
7874
/**
7975
* @param consumerAuth Consumer key to use for signature calculation
8076
* @param userAuth Request/access token to use for signature calculation
@@ -83,26 +79,13 @@ public OAuthSignatureCalculator(ConsumerKey consumerAuth, RequestToken userAuth)
8379
mac = new ThreadSafeHMAC(consumerAuth, userAuth);
8480
this.consumerAuth = consumerAuth;
8581
this.userAuth = userAuth;
86-
this.testTimestamp = 0L;
87-
this.testNonce = null;
88-
random = new Random(System.identityHashCode(this) + System.currentTimeMillis());
89-
}
90-
91-
OAuthSignatureCalculator(ConsumerKey consumerAuth, RequestToken userAuth, Long testTimestamp, String testNonce) {
92-
mac = new ThreadSafeHMAC(consumerAuth, userAuth);
93-
this.consumerAuth = consumerAuth;
94-
this.userAuth = userAuth;
95-
this.testTimestamp = testTimestamp;
96-
this.testNonce = testNonce;
9782
random = new Random(System.identityHashCode(this) + System.currentTimeMillis());
9883
}
9984

100-
101-
//@Override // silly 1.5; doesn't allow this for interfaces
102-
85+
@Override
10386
public void calculateAndAddSignature(Request request, RequestBuilderBase<?> requestBuilder) {
104-
String nonce = (this.testNonce != null) ? this.testNonce : generateNonce();
105-
long timestamp = (this.testTimestamp > 0L) ? this.testTimestamp : System.currentTimeMillis() / 1000L;
87+
String nonce = generateNonce();
88+
long timestamp = generateTimestamp();
10689
String signature = calculateSignature(request.getMethod(), request.getUri(), timestamp, nonce, request.getFormParams(), request.getQueryParams());
10790
String headerValue = constructAuthHeader(signature, nonce, timestamp);
10891
requestBuilder.setHeader(HEADER_AUTHORIZATION, headerValue);
@@ -202,13 +185,17 @@ public String constructAuthHeader(String signature, String nonce, long oauthTime
202185
return sb.toString();
203186
}
204187

205-
private synchronized String generateNonce() {
188+
protected synchronized String generateNonce() {
206189
random.nextBytes(nonceBuffer);
207190
// let's use base64 encoding over hex, slightly more compact than hex or decimals
208191
return Base64.encode(nonceBuffer);
209192
// return String.valueOf(Math.abs(random.nextLong()));
210193
}
211194

195+
protected long generateTimestamp() {
196+
return System.currentTimeMillis() / 1000L;
197+
}
198+
212199
/**
213200
* Container for parameters used for calculating OAuth signature.
214201
* About the only confusing aspect is that of whether entries are to be sorted
@@ -265,8 +252,7 @@ public String value() {
265252
return value;
266253
}
267254

268-
//@Override // silly 1.5; doesn't allow this for interfaces
269-
255+
@Override
270256
public int compareTo(Parameter other) {
271257
int diff = key.compareTo(other.key);
272258
if (diff == 0) {

api/src/test/java/org/asynchttpclient/oauth/TestSignatureCalculator.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.asynchttpclient.oauth;
1717

1818
import static org.testng.Assert.assertEquals;
19-
import static org.testng.Assert.assertNotNull;
2019
import static org.testng.Assert.fail;
2120

2221
import java.io.UnsupportedEncodingException;
@@ -26,7 +25,6 @@
2625
import java.util.regex.Matcher;
2726
import java.util.regex.Pattern;
2827

29-
import org.asynchttpclient.FluentCaseInsensitiveStringsMap;
3028
import org.asynchttpclient.Param;
3129
import org.asynchttpclient.Request;
3230
import org.asynchttpclient.RequestBuilder;
@@ -53,6 +51,28 @@ public class TestSignatureCalculator {
5351

5452
final static long TIMESTAMP = 1191242096;
5553

54+
private static class StaticOAuthSignatureCalculator extends OAuthSignatureCalculator {
55+
56+
private final long timestamp;
57+
private final String nonce;
58+
59+
public StaticOAuthSignatureCalculator(ConsumerKey consumerAuth, RequestToken userAuth, long timestamp, String nonce) {
60+
super(consumerAuth, userAuth);
61+
this.timestamp = timestamp;
62+
this.nonce = nonce;
63+
}
64+
65+
@Override
66+
protected long generateTimestamp() {
67+
return timestamp;
68+
}
69+
70+
@Override
71+
protected String generateNonce() {
72+
return nonce;
73+
}
74+
}
75+
5676
// based on the reference test case from
5777
// http://oauth.pbwiki.com/TestCases
5878
@Test(groups = "fast")
@@ -73,7 +93,7 @@ public void testGetCalculateSignature() {
7393
public void testPostCalculateSignature() {
7494
ConsumerKey consumer = new ConsumerKey(CONSUMER_KEY, CONSUMER_SECRET);
7595
RequestToken user = new RequestToken(TOKEN_KEY, TOKEN_SECRET);
76-
OAuthSignatureCalculator calc = new OAuthSignatureCalculator(consumer, user, TIMESTAMP, NONCE);
96+
OAuthSignatureCalculator calc = new StaticOAuthSignatureCalculator(consumer, user, TIMESTAMP, NONCE);
7797

7898
List<Param> formParams = new ArrayList<Param>();
7999
formParams.add(new Param("file", "vacation.jpg"));
@@ -108,7 +128,7 @@ public void testPostCalculateSignature() {
108128
public void testGetWithRequestBuilder() {
109129
ConsumerKey consumer = new ConsumerKey(CONSUMER_KEY, CONSUMER_SECRET);
110130
RequestToken user = new RequestToken(TOKEN_KEY, TOKEN_SECRET);
111-
OAuthSignatureCalculator calc = new OAuthSignatureCalculator(consumer, user, TIMESTAMP, NONCE);
131+
OAuthSignatureCalculator calc = new StaticOAuthSignatureCalculator(consumer, user, TIMESTAMP, NONCE);
112132

113133
List<Param> queryParams = new ArrayList<Param>();
114134
queryParams.add(new Param("file", "vacation.jpg"));

0 commit comments

Comments
 (0)