Skip to content

Commit bc4a6ff

Browse files
committed
Add support for top level assertions
1 parent 5c8cb48 commit bc4a6ff

File tree

2 files changed

+77
-20
lines changed

2 files changed

+77
-20
lines changed

src/test/java/org/primeframework/mvc/GlobalTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,39 @@ public void get_redirects_nested() throws Exception {
13681368
.assertBodyIsEmpty())
13691369
)
13701370
);
1371+
1372+
// Perform assertions on top-level
1373+
test.simulate(() -> simulator
1374+
.test("/router/meta-refresh")
1375+
.get()
1376+
.assertStatusCode(200)
1377+
.assertHTML(html -> html.assertElementExists("meta[http-equiv=Refresh]"))
1378+
1379+
// top-level
1380+
.followMetaRefresh()
1381+
.assertStatusCode(302)
1382+
.assertRedirect("/router/redirect/1")
1383+
1384+
// nested
1385+
.followRedirect(r2 -> r2
1386+
.assertStatusCode(302)
1387+
.assertRedirect("/router/redirect/2")
1388+
.followRedirect()
1389+
.assertStatusCode(302)
1390+
.assertRedirect("/router/redirect/3"))
1391+
.followRedirect(r4 -> r4
1392+
.assertStatusCode(302)
1393+
.assertRedirect("/router/redirect/4")
1394+
.followRedirect(r5 -> r5
1395+
.assertStatusCode(302)
1396+
.assertRedirect("/router/redirect/5")))
1397+
1398+
// top-level
1399+
.followRedirect()
1400+
.assertStatusCode(200)
1401+
.assertBodyIsEmpty()
1402+
1403+
);
13711404
}
13721405

13731406
@Test

src/test/java/org/primeframework/mvc/test/RequestResult.java

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,7 @@ public RequestResult delegateIfTrue(boolean test, ThrowingFunction<RequestResult
13041304
*
13051305
* @param selector The selector used to find the form in the DOM
13061306
* @param result A consumer for the request result from following the redirect.
1307-
* @return This.
1307+
* @return the request result returned by this action, or the last nested result.
13081308
*/
13091309
public RequestResult executeFormPostInResponseBody(String selector, ThrowingConsumer<RequestResult> result)
13101310
throws Exception {
@@ -1318,7 +1318,7 @@ public RequestResult executeFormPostInResponseBody(String selector, ThrowingCons
13181318
* @param selector The selector used to find the form in the DOM
13191319
* @param domHelper A consumer for the DOM Helper
13201320
* @param result A consumer for the request result from following the redirect.
1321-
* @return This.
1321+
* @return the request result returned by this action, or the last nested result.
13221322
*/
13231323
public RequestResult executeFormPostInResponseBody(String selector, ThrowingConsumer<DOMHelper> domHelper,
13241324
ThrowingConsumer<RequestResult> result)
@@ -1334,7 +1334,7 @@ public RequestResult executeFormPostInResponseBody(String selector, ThrowingCons
13341334
*
13351335
* @param selector The selector used to find the form in the DOM
13361336
* @param result A consumer for the request result from following the redirect.
1337-
* @return This.
1337+
* @return the request result returned by this action, or the last nested result.
13381338
*/
13391339
public RequestResult executeFormPostInResponseBodyReturnPostResult(String selector, ThrowingConsumer<RequestResult> result)
13401340
throws Exception {
@@ -1349,7 +1349,7 @@ public RequestResult executeFormPostInResponseBodyReturnPostResult(String select
13491349
* @param selector The selector used to find the form in the DOM
13501350
* @param domHelper A consumer for the DOM Helper
13511351
* @param result A consumer for the request result from following the redirect.
1352-
* @return This.
1352+
* @return the request result returned by this action, or the last nested result.
13531353
*/
13541354
public RequestResult executeFormPostInResponseBodyReturnPostResult(String selector, ThrowingConsumer<DOMHelper> domHelper,
13551355
ThrowingConsumer<RequestResult> result)
@@ -1363,7 +1363,7 @@ public RequestResult executeFormPostInResponseBodyReturnPostResult(String select
13631363
* Prefer {@link #followMetaRefresh(ThrowingConsumer)}.
13641364
*
13651365
* @param result A consumer for the request result from following the redirect.
1366-
* @return This.
1366+
* @return the request result returned by this action, or the last nested result.
13671367
*/
13681368
public RequestResult executeMetaRefreshReturnResult(ThrowingConsumer<RequestResult> result) throws Exception {
13691369
return handleFollowMetaRefresh(result);
@@ -1375,7 +1375,7 @@ public RequestResult executeMetaRefreshReturnResult(ThrowingConsumer<RequestResu
13751375
* Prefer {@link #followRedirect(ThrowingConsumer)}
13761376
*
13771377
* @param consumer The request result from following the redirect.
1378-
* @return This.
1378+
* @return the request result returned by this action, or the last nested result.
13791379
*/
13801380
public RequestResult executeRedirect(ThrowingConsumer<RequestResult> consumer) throws Exception {
13811381
_followRedirect(consumer);
@@ -1388,7 +1388,7 @@ public RequestResult executeRedirect(ThrowingConsumer<RequestResult> consumer) t
13881388
* Prefer {@link #followRedirect(ThrowingConsumer)}
13891389
*
13901390
* @param consumer The request result from following the redirect.
1391-
* @return This.
1391+
* @return the request result returned by this action, or the last nested result.
13921392
*/
13931393
public RequestResult executeRedirectReturnResult(ThrowingConsumer<RequestResult> consumer) throws Exception {
13941394
return _followRedirect(consumer);
@@ -1398,22 +1398,40 @@ public RequestResult executeRedirectReturnResult(ThrowingConsumer<RequestResult>
13981398
* Attempt to follow a meta-refresh
13991399
*
14001400
* @param result A consumer for the request result from following the redirect.
1401-
* @return This.
1401+
* @return the request result returned by this action, or the last nested result.
14021402
*/
14031403
public RequestResult followMetaRefresh(ThrowingConsumer<RequestResult> result) throws Exception {
14041404
return handleFollowMetaRefresh(result);
14051405
}
14061406

1407+
/**
1408+
* Attempt to follow a meta-refresh
1409+
*
1410+
* @return the request result returned by this action, or the last nested result.
1411+
*/
1412+
public RequestResult followMetaRefresh() throws Exception {
1413+
return handleFollowMetaRefresh(null);
1414+
}
1415+
14071416
/**
14081417
* Follow the redirect and accept a consumer to assert on the response.
14091418
*
14101419
* @param consumer The request result from following the redirect.
1411-
* @return This.
1420+
* @return the request result returned by this action, or the last nested redirect.
14121421
*/
14131422
public RequestResult followRedirect(ThrowingConsumer<RequestResult> consumer) throws Exception {
14141423
return _followRedirect(consumer);
14151424
}
14161425

1426+
/**
1427+
* Follow the redirect and accept a consumer to assert on the response.
1428+
*
1429+
* @return the request result returned by this action, or the last nested redirect.
1430+
*/
1431+
public RequestResult followRedirect() throws Exception {
1432+
return _followRedirect(null);
1433+
}
1434+
14171435
/**
14181436
* Retrieves the instance of the given type from the Guice Injector.
14191437
*
@@ -1613,24 +1631,23 @@ public RequestResult setup(ThrowingRunnable runnable) throws Exception {
16131631
* Attempt to submit the form found in the response body and return the result.
16141632
*
16151633
* @param selector The selector used to find the form in the DOM
1616-
* @param result The request result
1617-
* @return This.
1634+
* @return the request result returned by this action, or the last nested result.
16181635
*/
1619-
public RequestResult submitForm(String selector, ThrowingConsumer<RequestResult> result) throws Exception {
1620-
return _submitForm(selector, null, result);
1636+
public RequestResult submitForm(String selector, ThrowingConsumer<RequestResult> consumer) throws Exception {
1637+
return _submitForm(selector, null, consumer);
16211638
}
16221639

16231640
/**
16241641
* Attempt to submit the form found in the response body and return the result.
16251642
*
16261643
* @param selector The selector used to find the form in the DOM
16271644
* @param domHelper A consumer for the DOM Helper
1628-
* @param result The request result
1629-
* @return This.
1645+
* @param consumer The request result consumer
1646+
* @return the request result returned by this action, or the last nested result.
16301647
*/
1631-
public RequestResult submitForm(String selector, ThrowingConsumer<DOMHelper> domHelper, ThrowingConsumer<RequestResult> result)
1648+
public RequestResult submitForm(String selector, ThrowingConsumer<DOMHelper> domHelper, ThrowingConsumer<RequestResult> consumer)
16321649
throws Exception {
1633-
return _submitForm(selector, domHelper, result);
1650+
return _submitForm(selector, domHelper, consumer);
16341651
}
16351652

16361653
private RequestResult _assertBodyContains(boolean escape, String... strings) {
@@ -1738,7 +1755,9 @@ private RequestResult _followRedirect(ThrowingConsumer<RequestResult> consumer)
17381755

17391756
result.chained = chained;
17401757
chained.add(result);
1741-
consumer.accept(result);
1758+
if (consumer != null) {
1759+
consumer.accept(result);
1760+
}
17421761
return chained.getLast();
17431762
}
17441763

@@ -1808,7 +1827,10 @@ private RequestResult _submitForm(String selector, ThrowingConsumer<DOMHelper> d
18081827

18091828
result.chained = chained;
18101829
result.chained.add(result);
1811-
consumer.accept(result);
1830+
if (consumer != null) {
1831+
consumer.accept(result);
1832+
}
1833+
18121834
return chained.getLast();
18131835
}
18141836

@@ -1942,7 +1964,9 @@ private RequestResult handleFollowMetaRefresh(ThrowingConsumer<RequestResult> co
19421964

19431965
result.chained = chained;
19441966
result.chained.add(result);
1945-
consumer.accept(result);
1967+
if (consumer != null) {
1968+
consumer.accept(result);
1969+
}
19461970
return result.chained.getLast();
19471971
}
19481972
}

0 commit comments

Comments
 (0)