forked from TeleSign/java_telesign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestClientTest.java
More file actions
363 lines (279 loc) · 13.5 KB
/
RestClientTest.java
File metadata and controls
363 lines (279 loc) · 13.5 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package com.telesign;
import junit.framework.TestCase;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
public class RestClientTest extends TestCase {
private MockWebServer mockServer;
private String customerId;
private String apiKey;
private SimpleDateFormat rfc2616 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'");
public void setUp() throws Exception {
super.setUp();
this.customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
this.apiKey = "EXAMPLE----TE8sTgg45yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
this.mockServer = new MockWebServer();
this.mockServer.start();
}
public void tearDown() throws Exception {
super.tearDown();
this.mockServer.shutdown();
}
public void testRestClientConstructors() {
RestClient client = new RestClient(this.customerId, this.apiKey);
}
public void testGenerateTelesignHeadersWithPost() throws GeneralSecurityException {
String methodName = "POST";
String dateRfc2616 = "Wed, 14 Dec 2016 18:20:12 GMT";
String nonce = "A1592C6F-E384-4CDB-BC42-C3AB970369E9";
String resource = "/v1/resource";
String bodyParamsUrlEncoded = "test=param";
String expectedAuthorizationHeader = "TSA FFFFFFFF-EEEE-DDDD-1234-AB1234567890:" +
"2xVlmbrxLjYrrPun3G3WMNG6Jon4yKcTeOoK9DjXJ/Q=";
Map<String, String> actualHeaders = RestClient.generateTelesignHeaders(this.customerId, this.apiKey,
methodName,
resource,
bodyParamsUrlEncoded,
dateRfc2616,
nonce,
"unitTest",
"application/x-www-form-urlencoded"
);
assertEquals("Authorization header is not as expected", expectedAuthorizationHeader,
actualHeaders.get("Authorization"));
}
public void testGenerateTelesignHeadersUnicodeContent() throws GeneralSecurityException {
String methodName = "POST";
String dateRfc2616 = "Wed, 14 Dec 2016 18:20:12 GMT";
String nonce = "A1592C6F-E384-4CDB-BC42-C3AB970369E9";
String resource = "/v1/resource";
String bodyParamsUrlEncoded = "test=%CF%BF";
String expectedAuthorizationHeader = "TSA FFFFFFFF-EEEE-DDDD-1234-AB1234567890:" +
"h8d4I0RTxErbxYXuzCOtNqb/f0w3Ck8e5SEkGNj01+8=";
Map<String, String> actualHeaders = RestClient.generateTelesignHeaders(this.customerId, this.apiKey,
methodName,
resource,
bodyParamsUrlEncoded,
dateRfc2616,
nonce,
"unitTest",
"application/x-www-form-urlencoded");
assertEquals("Authorization header is not as expected", expectedAuthorizationHeader,
actualHeaders.get("Authorization"));
}
public void testGenerateTelesignHeadersWithGet() throws GeneralSecurityException {
String methodName = "GET";
String dateRfc2616 = "Wed, 14 Dec 2016 18:20:12 GMT";
String nonce = "A1592C6F-E384-4CDB-BC42-C3AB970369E9";
String resource = "/v1/resource";
String expectedAuthorizationHeader = "TSA FFFFFFFF-EEEE-DDDD-1234-AB1234567890:" +
"aUm7I+9GKl3ww7PNeeJntCT0iS7b+EmRKEE4LnRzChQ=";
Map<String, String> actualHeaders = RestClient.generateTelesignHeaders(this.customerId, this.apiKey,
methodName,
resource,
"",
dateRfc2616,
nonce,
"unitTest", "application/x-www-form-urlencoded");
assertEquals("Authorization header is not as expected", expectedAuthorizationHeader,
actualHeaders.get("Authorization"));
}
public void testGenerateTelesignHeadersDefaultValues() throws GeneralSecurityException {
String methodName = "GET";
String resource = "/v1/resource";
Map<String, String> actualHeaders = RestClient.generateTelesignHeaders(this.customerId, this.apiKey,
methodName,
resource,
"",
null,
null,
null,
"");
try {
UUID uuid = UUID.fromString(actualHeaders.get("x-ts-nonce"));
} catch (IllegalArgumentException e) {
fail("x-ts-nonce header is not a valid UUID");
}
try {
this.rfc2616.parse(actualHeaders.get("Date"));
} catch (ParseException e) {
fail("Date header is not valid rfc2616 format");
}
}
public void testRestClientPost() throws Exception {
String test_resource = "/test/resource";
Map<String, String> test_params = new HashMap<>();
test_params.put("test", "123_\u03ff_test");
this.mockServer.enqueue(new MockResponse().setBody("{}"));
RestClient client = new RestClient(this.customerId,
this.apiKey,
this.mockServer.url("").toString().replaceAll("/$", ""));
client.post(test_resource, test_params);
RecordedRequest request = this.mockServer.takeRequest(1, TimeUnit.SECONDS);
assertEquals("method is not as expected", "POST", request.getMethod());
assertEquals("path is not as expected", "/test/resource", request.getPath());
assertEquals("body is not as expected", "test=123_%CF%BF_test",
request.getBody().readUtf8());
assertEquals("Content-Type header is not as expected", "application/x-www-form-urlencoded",
request.getHeader("Content-Type"));
assertEquals("x-ts-auth-method header is not as expected", "HMAC-SHA256",
request.getHeader("x-ts-auth-method"));
try {
UUID uuid = UUID.fromString(request.getHeader("x-ts-nonce"));
} catch (IllegalArgumentException e) {
fail("x-ts-nonce header is not a valid UUID");
}
try {
this.rfc2616.parse(request.getHeader("Date"));
} catch (ParseException e) {
fail("Date header is not valid rfc2616 format");
}
assertNotNull(request.getHeader("Authorization"));
}
public void testRestClientGet() throws Exception {
String test_resource = "/test/resource";
Map<String, String> test_params = new HashMap<>();
test_params.put("test", "123_\u03ff_test");
this.mockServer.enqueue(new MockResponse().setBody("{}"));
RestClient client = new RestClient(this.customerId,
this.apiKey,
this.mockServer.url("").toString().replaceAll("/$", ""));
client.get(test_resource, test_params);
RecordedRequest request = this.mockServer.takeRequest(1, TimeUnit.SECONDS);
assertEquals("method is not as expected", "GET", request.getMethod());
assertEquals("path is not as expected", "/test/resource?test=123_%CF%BF_test",
request.getPath());
assertEquals("body is not as expected", "", request.getBody().readUtf8());
assertEquals("Content-Type header is not as expected", "",
request.getHeader("Content-Type"));
assertEquals("x-ts-auth-method header is not as expected", "HMAC-SHA256",
request.getHeader("x-ts-auth-method"));
try {
UUID uuid = UUID.fromString(request.getHeader("x-ts-nonce"));
} catch (IllegalArgumentException e) {
fail("x-ts-nonce header is not a valid UUID");
}
try {
this.rfc2616.parse(request.getHeader("Date"));
} catch (ParseException e) {
fail("Date header is not valid rfc2616 format");
}
assertNotNull(request.getHeader("Authorization"));
}
public void testRestClientPut() throws Exception {
String test_resource = "/test/resource";
Map<String, String> test_params = new HashMap<>();
test_params.put("test", "123_\u03ff_test");
this.mockServer.enqueue(new MockResponse().setBody("{}"));
RestClient client = new RestClient(this.customerId,
this.apiKey,
this.mockServer.url("").toString().replaceAll("/$", ""));
client.put(test_resource, test_params);
RecordedRequest request = this.mockServer.takeRequest(1, TimeUnit.SECONDS);
assertEquals("method is not as expected", "PUT", request.getMethod());
assertEquals("path is not as expected", "/test/resource", request.getPath());
assertEquals("body is not as expected", "test=123_%CF%BF_test",
request.getBody().readUtf8());
assertEquals("Content-Type header is not as expected", "application/x-www-form-urlencoded",
request.getHeader("Content-Type"));
assertEquals("x-ts-auth-method header is not as expected", "HMAC-SHA256",
request.getHeader("x-ts-auth-method"));
try {
UUID uuid = UUID.fromString(request.getHeader("x-ts-nonce"));
} catch (IllegalArgumentException e) {
fail("x-ts-nonce header is not a valid UUID");
}
try {
this.rfc2616.parse(request.getHeader("Date"));
} catch (ParseException e) {
fail("Date header is not valid rfc2616 format");
}
assertNotNull(request.getHeader("Authorization"));
}
public void testRestClientDelete() throws Exception {
String test_resource = "/test/resource";
Map<String, String> test_params = new HashMap<>();
test_params.put("test", "123_\u03ff_test");
this.mockServer.enqueue(new MockResponse().setBody("{}"));
RestClient client = new RestClient(this.customerId,
this.apiKey,
this.mockServer.url("").toString().replaceAll("/$", ""));
client.delete(test_resource, test_params);
RecordedRequest request = this.mockServer.takeRequest(1, TimeUnit.SECONDS);
assertEquals("method is not as expected", "DELETE", request.getMethod());
assertEquals("path is not as expected", "/test/resource?test=123_%CF%BF_test",
request.getPath());
assertEquals("body is not as expected", "", request.getBody().readUtf8());
assertEquals("Content-Type header is not as expected", "",
request.getHeader("Content-Type"));
assertEquals("x-ts-auth-method header is not as expected", "HMAC-SHA256",
request.getHeader("x-ts-auth-method"));
try {
UUID uuid = UUID.fromString(request.getHeader("x-ts-nonce"));
} catch (IllegalArgumentException e) {
fail("x-ts-nonce header is not a valid UUID");
}
try {
this.rfc2616.parse(request.getHeader("Date"));
} catch (ParseException e) {
fail("Date header is not valid rfc2616 format");
}
assertNotNull(request.getHeader("Authorization"));
}
public void testRestClientProxyAuthentication() throws Exception {
String test_resource = "/test/resource";
Map<String, String> test_params = new HashMap<>();
test_params.put("test", "123_\u03ff_test");
this.mockServer.enqueue(new MockResponse().setBody("{}").setResponseCode(407));
this.mockServer.enqueue(new MockResponse().setBody("{}"));
RestClient client = new RestClient(this.customerId,
this.apiKey,
this.mockServer.url("").toString().replaceAll("/$", ""),
1,
1,
1,
this.mockServer.toProxyAddress(),
"proxyUsername",
"proxyPassword");
client.get(test_resource, test_params);
assertEquals("request count does not match expected", 2, this.mockServer.getRequestCount());
RecordedRequest initialRequest = this.mockServer.takeRequest(1, TimeUnit.SECONDS);
RecordedRequest proxyAuthorizationRequest = this.mockServer.takeRequest(1, TimeUnit.SECONDS);
assertNull("Proxy-Authorization header should be absent from initial request",
initialRequest.getHeader("Proxy-Authorization"));
assertNotNull("Proxy-Authorization header should exist in the subsequent request",
proxyAuthorizationRequest.getHeader("Proxy-Authorization"));
}
public void testHttpInternalServerErrorResponse() {
String test_resource = "/resource";
this.mockServer.enqueue(new MockResponse().setBody("").setResponseCode(503));
RestClient client = new RestClient(this.customerId,
this.apiKey,
this.mockServer.url("").toString().replaceAll("/$", "")
);
RestClient.TelesignResponse resp = null;
try {
resp = client.get(test_resource, null);
} catch (GeneralSecurityException e) {
fail("GeneralSecurityException thrown");
} catch (IOException e) {
fail("IOException thrown");
} catch (Exception e) {
fail("Unhandled exception thrown.");
}
assertNotNull(resp);
assertEquals("Expected empty string", "", resp.body);
assertEquals("HTTP Status code mismatch", 503, resp.statusCode);
assertNotNull(resp.headers);
assertFalse(resp.ok);
assertNotNull(resp.json);
}
}