-
Notifications
You must be signed in to change notification settings - Fork 683
/
FunctionalTest.java
650 lines (586 loc) · 22.5 KB
/
FunctionalTest.java
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
package play.test;
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
import com.ning.http.client.multipart.FilePart;
import com.ning.http.client.multipart.MultipartBody;
import com.ning.http.client.multipart.MultipartUtils;
import com.ning.http.client.multipart.Part;
import com.ning.http.client.multipart.StringPart;
import org.junit.Before;
import play.Invoker;
import play.Invoker.InvocationContext;
import play.classloading.enhancers.ControllersEnhancer.ControllerInstrumentation;
import play.exceptions.JavaExecutionException;
import play.exceptions.UnexpectedException;
import play.mvc.ActionInvoker;
import play.mvc.Controller;
import play.mvc.Http;
import play.mvc.Http.Request;
import play.mvc.Http.Response;
import play.mvc.Router.ActionDefinition;
import play.mvc.Scope.RenderArgs;
import play.mvc.results.RenderStatic;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.nio.channels.Channels;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.regex.Pattern;
/**
* Application tests support
*/
public abstract class FunctionalTest extends BaseTest {
public static final String APPLICATION_X_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded";
public static final String MULTIPART_FORM_DATA = "multipart/form-data";
private static Map<String, Http.Cookie> savedCookies; // cookies stored
// between calls
private static final Map<String, Object> renderArgs = new HashMap<>();
@Before
public void clearCookies() {
savedCookies = null;
}
// Requests
public static Response GET(Object url) {
return GET(newRequest(), url);
}
/**
* sends a GET request to the application under tests.
*
* @param url
* relative url such as <em>"/products/1234"</em>
* @param followRedirect
* indicates if request have to follow redirection (status 302)
* @return the response
*/
public static Response GET(Object url, boolean followRedirect) {
Response response = GET(url);
if (Http.StatusCode.FOUND == response.status && followRedirect) {
Http.Header redirectedTo = response.headers.get("Location");
String location = redirectedTo.value();
if (location.contains("http")) {
java.net.URL redirectedUrl;
try {
redirectedUrl = new java.net.URL(redirectedTo.value());
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
response = GET(redirectedUrl.getPath());
} else {
response = GET(location);
}
}
return response;
}
/**
* Sends a GET request to the application under tests.
*
* @param request
* The given request
* @param url
* relative url such as <em>"/products/1234"</em>
* @return the response
*/
public static Response GET(Request request, Object url) {
String path;
String queryString = "";
String turl = url.toString();
int questionIndex = turl.indexOf('?');
if (questionIndex >= 0) {
path = turl.substring(0, questionIndex);
queryString = turl.substring(questionIndex + 1);
} else {
path = turl;
}
request.method = "GET";
request.url = turl;
request.path = path;
request.querystring = queryString;
request.body = new ByteArrayInputStream(new byte[0]);
if (savedCookies != null)
request.cookies = savedCookies;
return makeRequest(request);
}
// convenience methods
public static Response POST(Object url) {
return POST(url, APPLICATION_X_WWW_FORM_URLENCODED, "");
}
public static Response POST(Request request, Object url) {
return POST(request, url, APPLICATION_X_WWW_FORM_URLENCODED, "");
}
public static Response POST(Object url, String contenttype, String body) {
return POST(newRequest(), url, contenttype, body);
}
public static Response POST(Request request, Object url, String contenttype, String body) {
return POST(request, url, contenttype, new ByteArrayInputStream(body.getBytes()));
}
public static Response POST(Object url, String contenttype, InputStream body) {
return POST(newRequest(), url, contenttype, body);
}
/**
* Sends a POST request to the application under tests.
*
* @param request
* The given request
* @param url
* relative url such as <em>"/products/1234"</em>
* @param contenttype
* content-type of the request
* @param body
* posted data
* @return the response
*/
public static Response POST(Request request, Object url, String contenttype, InputStream body) {
String path;
String queryString = "";
String turl = url.toString();
int questionIndex = turl.indexOf('?');
if (questionIndex >= 0) {
path = turl.substring(0, questionIndex);
queryString = turl.substring(questionIndex + 1);
} else {
path = turl;
}
request.method = "POST";
request.contentType = contenttype;
request.url = turl;
request.path = path;
request.querystring = queryString;
request.body = body;
if (savedCookies != null)
request.cookies = savedCookies;
return makeRequest(request);
}
/**
* Sends a POST request to the application under tests as a multipart form. Designed for file upload testing.
*
* @param url
* relative url such as <em>"/products/1234"</em>
* @param parameters
* map of parameters to be posted
* @param files
* map containing files to be uploaded
* @return the response
*/
public static Response POST(Object url, Map<String, String> parameters, Map<String, File> files) {
return POST(newRequest(), url, parameters, files);
}
public static Response POST(Object url, Map<String, String> parameters) {
return POST(newRequest(), url, parameters, new HashMap<>());
}
public static Response POST(Request request, Object url, Map<String, String> parameters, Map<String, File> files) {
List<Part> parts = new ArrayList<>();
for (String key : parameters.keySet()) {
StringPart stringPart = new StringPart(key, parameters.get(key), request.contentType, Charset.forName(request.encoding));
parts.add(stringPart);
}
for (Map.Entry<String, File> entry : files.entrySet()) {
File file = entry.getValue();
if (file != null) {
Part filePart = new FilePart(entry.getKey(), entry.getValue());
parts.add(filePart);
}
}
MultipartBody requestEntity;
/*
* ^1 MultipartBody::read is not working (if parts.isEmpty() == true) byte[] array = null;
**/
_ByteArrayOutputStream baos;
try {
requestEntity = MultipartUtils.newMultipartBody(parts, new FluentCaseInsensitiveStringsMap());
request.headers.put("content-type", new Http.Header("content-type", requestEntity.getContentType()));
long contentLength = requestEntity.getContentLength();
if (contentLength < Integer.MIN_VALUE || contentLength > Integer.MAX_VALUE) {
throw new IllegalArgumentException(contentLength + " cannot be cast to int without changing its value.");
}
// array = new byte[(int) contentLength]; // ^1
// requestEntity.read(ByteBuffer.wrap(array)); // ^1
baos = new _ByteArrayOutputStream((int) contentLength);
requestEntity.transferTo(0, Channels.newChannel(baos));
} catch (IOException e) {
throw new RuntimeException(e);
}
// InputStream body = new ByteArrayInputStream(array != null ? array :
// new byte[0]); // ^1
InputStream body = new ByteArrayInputStream(baos.getByteArray());
return POST(request, url, MULTIPART_FORM_DATA, body);
}
public static Response PUT(Object url, String contenttype, String body) {
return PUT(newRequest(), url, contenttype, body);
}
/**
* Sends a PUT request to the application under tests.
*
* @param request
* The given request
* @param url
* relative url such as <em>"/products/1234"</em>
* @param contenttype
* content-type of the request
* @param body
* data to send
* @return the response
*/
public static Response PUT(Request request, Object url, String contenttype, String body) {
String path;
String queryString = "";
String turl = url.toString();
int questionIndex = turl.indexOf('?');
if (questionIndex >= 0) {
path = turl.substring(0, questionIndex);
queryString = turl.substring(questionIndex + 1);
} else {
path = turl;
}
request.method = "PUT";
request.contentType = contenttype;
request.url = turl;
request.path = path;
request.querystring = queryString;
request.body = new ByteArrayInputStream(body.getBytes());
if (savedCookies != null)
request.cookies = savedCookies;
return makeRequest(request);
}
public static Response DELETE(String url) {
return DELETE(newRequest(), url);
}
/**
* Sends a DELETE request to the application under tests.
*
* @param request
* The given request
* @param url
* relative url eg. <em>"/products/1234"</em>
* @return the response
*/
public static Response DELETE(Request request, Object url) {
String path;
String queryString = "";
String turl = url.toString();
int questionIndex = turl.indexOf('?');
if (questionIndex >= 0) {
path = turl.substring(0, questionIndex);
queryString = turl.substring(questionIndex + 1);
} else {
path = turl;
}
request.method = "DELETE";
request.url = turl;
request.path = path;
request.querystring = queryString;
if (savedCookies != null)
request.cookies = savedCookies;
request.body = new ByteArrayInputStream(new byte[0]);
return makeRequest(request);
}
public static void makeRequest(final Request request, final Response response) {
final CountDownLatch actionCompleted = new CountDownLatch(1);
final Future<?> invocationResult = TestEngine.functionalTestsExecutor.submit(new Invoker.Invocation() {
@Override
public void execute() {
renderArgs.clear();
ActionInvoker.invoke(request, response);
if (RenderArgs.current().data != null) {
renderArgs.putAll(RenderArgs.current().data);
}
}
@Override
public void onSuccess() throws Exception {
try {
super.onSuccess();
} finally {
onActionCompleted();
}
}
@Override
public void onException(Throwable e) {
try {
super.onException(e);
} finally {
onActionCompleted();
}
}
private void onActionCompleted() {
actionCompleted.countDown();
}
@Override
public InvocationContext getInvocationContext() {
ActionInvoker.resolve(request);
return new InvocationContext(Http.invocationType, request.invokedMethod.getAnnotations(),
request.invokedMethod.getDeclaringClass().getAnnotations());
}
});
try {
// We can not simply wait on the future result because of how continuations
// are implemented. Only when the latch is counted down the action is really
// completed. Therefore, wait on the latch.
if (!actionCompleted.await(30, TimeUnit.SECONDS)) {
throw new TimeoutException("Request did not complete in time");
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
try {
// We still call this to raise any exception that might have
// occurred during execution of the invocation.
invocationResult.get();
}
catch (ExecutionException e) {
RuntimeException originalException = unwrapOriginalException(e);
if (originalException instanceof RenderStatic) {
response.status = 200;
response.direct = ((RenderStatic) originalException).file;
}
else {
throw originalException;
}
}
catch (Exception e) {
throw new RuntimeException(e);
}
try {
if (savedCookies == null) {
savedCookies = new HashMap<>();
}
for (Map.Entry<String, Http.Cookie> e : response.cookies.entrySet()) {
// If Max-Age is unset, browsers discard on exit; if
// 0, they discard immediately.
if (e.getValue().maxAge == null || e.getValue().maxAge > 0) {
savedCookies.put(e.getKey(), e.getValue());
} else {
// cookies with maxAge zero still remove a previously
// existing cookie,
// like PLAY_FLASH.
savedCookies.remove(e.getKey());
}
}
response.out.flush();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
/**
* Check if the original exceptions fits the usual patterns.
* If yes, return the very original runtime exception.
*/
private static RuntimeException unwrapOriginalException(final ExecutionException e) {
Throwable executionCause = e.getCause();
if (executionCause instanceof JavaExecutionException || executionCause instanceof UnexpectedException) {
Throwable originalCause = executionCause.getCause();
if (originalCause instanceof RuntimeException) {
return (RuntimeException) originalCause;
}
}
// As a last fallback, just wrap everything up
return new RuntimeException(e);
}
public static Response makeRequest(Request request) {
Response response = newResponse();
makeRequest(request, response);
if (response.status == 302) { // redirect
// if Location-header is pressent, fix it to "look like" a
// functional-test-url
Http.Header locationHeader = response.headers.get("Location");
if (locationHeader != null) {
String locationUrl = locationHeader.value();
if (locationUrl.startsWith("http://localhost/")) {
locationHeader.values.clear();
locationHeader.values.add(locationUrl.substring(16));// skip
// 'http://localhost'
}
}
}
return response;
}
public static Response newResponse() {
Response response = new Response();
response.out = new ByteArrayOutputStream();
// Register an onWriteChunk action so that Response.writeChunk() won't throw
// an unhandled exception if the controller action calls it.
response.onWriteChunk(
chunk -> {
// Mimic the behavior of PlayHandler$LazyChunkedInput.writeChunk()
if (chunk != null) {
try {
byte[] bytes;
if (chunk instanceof byte[]) {
bytes = (byte[]) chunk;
} else {
bytes = chunk.toString().getBytes(response.encoding);
}
response.out.write(bytes);
} catch (Exception exception) {
// Something is wrong with the chunk.
throw new RuntimeException(exception);
}
}
});
return response;
}
public static Request newRequest() {
return Request.createRequest(null, "GET", "/", "", null, null, null, null, false, 80, "localhost", false, null, null);
}
// Assertions
/**
* Asserts a <em>2OO Success</em> response
*
* @param response
* server response
*/
public static void assertIsOk(Response response) {
assertStatus(200, response);
}
/**
* Asserts a <em>404 (not found)</em> response
*
* @param response
* server response
*/
public static void assertIsNotFound(Response response) {
assertStatus(404, response);
}
/**
* Asserts response status code
*
* @param status
* expected HTTP response code
* @param response
* server response
*/
public static void assertStatus(int status, Response response) {
assertEquals("Response status ", (Object) status, response.status);
}
public static void assertIsStaticFile(Response response, String filePath) {
assertIsOk(response);
String file = (String) response.direct;
assertEquals(filePath, file);
}
/**
* Exact equality assertion on response body
*
* @param content
* expected body content
* @param response
* server response
*/
public static void assertContentEquals(String content, Response response) {
assertEquals(content, getContent(response));
}
/**
* Asserts response body matched a pattern or contains some text.
*
* @param pattern
* a regular expression pattern or a regular text, ( which must be escaped using Pattern.quote)
* @param response
* server response
*/
public static void assertContentMatch(String pattern, Response response) {
Pattern ptn = Pattern.compile(pattern);
boolean ok = ptn.matcher(getContent(response)).find();
assertTrue("Response content does not match '" + pattern + "'", ok);
}
/**
* Verify response charset encoding, as returned by the server in the Content-Type header. Be aware that if no
* charset is returned, assertion will fail.
*
* @param charset
* expected charset encoding such as "utf-8" or "iso8859-1".
* @param response
* server response
*/
public static void assertCharset(String charset, Response response) {
int pos = response.contentType.indexOf("charset=") + 8;
String responseCharset = (pos > 7) ? response.contentType.substring(pos).toLowerCase() : "";
assertEquals("Response charset", charset.toLowerCase(), responseCharset);
}
/**
* Verify the response content-type
*
* @param contentType
* expected content-type without any charset extension, such as "text/html"
* @param response
* server response
*/
public static void assertContentType(String contentType, Response response) {
String responseContentType = response.contentType;
assertNotNull("Response contentType missing", responseContentType);
assertTrue("Response contentType unmatched : '" + contentType + "' !~ '" + responseContentType + "'",
responseContentType.startsWith(contentType));
}
/**
* Exact equality assertion on a response header value
*
* @param headerName
* header to verify. case-insensitive
* @param value
* expected header value
* @param response
* server response
*/
public static void assertHeaderEquals(String headerName, String value, Response response) {
assertNotNull("Response header " + headerName + " missing", response.headers.get(headerName));
assertEquals("Response header " + headerName + " mismatch", value, response.headers.get(headerName).value());
}
/**
* obtains the response body as a string
*
* @param response
* server response
* @return the response body as an <em>utf-8 string</em>
*/
public static String getContent(Response response) {
byte[] data = response.out.toByteArray();
try {
return new String(data, response.encoding);
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex);
}
}
public static Object renderArgs(String name) {
return renderArgs.get(name);
}
// Utils
public void sleep(int seconds) {
try {
Thread.sleep(seconds * 1000);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
protected static URL reverse() {
ControllerInstrumentation.stopActionCall();
ActionDefinition actionDefinition = new ActionDefinition();
Controller._currentReverse.set(actionDefinition);
return new URL(actionDefinition);
}
public static class URL {
final ActionDefinition actionDefinition;
URL(ActionDefinition actionDefinition) {
this.actionDefinition = actionDefinition;
}
@Override
public String toString() {
return actionDefinition.url;
}
}
public static final class _ByteArrayOutputStream extends ByteArrayOutputStream {
public _ByteArrayOutputStream(int size) {
super(size);
}
public byte[] getByteArray() {
return this.buf;
}
}
}