Skip to content

Commit d6b8bbb

Browse files
ft/receivedRequestsOrderAssertion (#149)
* ft: Ability to assert a request has been received on given paths in a specified order * Feat/check-order-calls: Now we can assert about the order of several request on different endpoint --------- Co-authored-by: laujacq <[email protected]>
1 parent 15801f7 commit d6b8bbb

File tree

3 files changed

+109
-2
lines changed

3 files changed

+109
-2
lines changed

tzatziki-http/src/main/java/com/decathlon/tzatziki/steps/HttpSteps.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,30 @@ private void mockserver_has_received(Guard guard, Comparison comparison, String
511511
});
512512
}
513513

514+
@Then(THAT + GUARD + "(?:the )?recorded interactions were" + COMPARING_WITH + ":$")
515+
public void we_received_a_request_on_paths(Guard guard, Comparison comparison, Object content) {
516+
guard.in(objects, () -> {
517+
List<Map<String, Object>> expectedRequests = read(objects.resolve(content));
518+
expectedRequests.forEach(expectedRequestMap -> {
519+
String path = (String) expectedRequestMap.get("path");
520+
Matcher withoutFlagInUriMatch = match(mocked(path.replaceFirst("^\\?e ", "")));
521+
String uriGroup = withoutFlagInUriMatch.group(4);
522+
expectedRequestMap.put("path", (path.startsWith("?e ") ? "?e " : "") + uriGroup);
523+
expectedRequestMap.put("queryParameters", toParameters(withoutFlagInUriMatch.group(5)));
524+
});
525+
526+
List<Map<String, Object>> actualRequests = retrieveRecordedRequests(new HttpRequest()).stream()
527+
.map(httpRequest -> {
528+
Map<String, Object> actualRequest = Mapper.read(Mapper.toJson(Interaction.Request.fromHttpRequest(httpRequest)));
529+
actualRequest.put("queryParameters", httpRequest.getQueryStringParameterList());
530+
return actualRequest;
531+
})
532+
.toList();
533+
534+
comparison.compare(actualRequests, expectedRequests);
535+
});
536+
}
537+
514538
@And(THAT + GUARD + QUOTED_CONTENT + " has not been called$")
515539
public void mockserver_has_not_been_called_on(Guard guard, String path) {
516540
guard.in(objects, () -> {

tzatziki-http/src/main/java/com/decathlon/tzatziki/utils/Interaction.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public static String wrapAsInteractionJson(String request) {
4545
@Builder(toBuilder = true)
4646
public static class Request {
4747

48+
public String path;
4849
@Builder.Default
4950
public Map<String, String> headers = new LinkedHashMap<>();
5051
@Builder.Default
@@ -54,6 +55,7 @@ public static class Request {
5455

5556
public static Request fromHttpRequest(HttpRequest httpRequest) {
5657
return Request.builder()
58+
.path(httpRequest.getPath().toString())
5759
.method(Method.of(httpRequest.getMethod().getValue()))
5860
.headers(MockFaster.asMap(httpRequest.getHeaderList()))
5961
.body(Body.builder().payload(httpRequest.getBodyAsString()).build())

tzatziki-http/src/test/resources/com/decathlon/tzatziki/steps/http.feature

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,7 +1361,7 @@ Feature: to interact with an http service and setup mocks
13611361
| {"body":{"payload":{"my-body":{"field":"a bad value"}}}} |
13621362

13631363
Scenario: Requests count assertion should also work for digit
1364-
And that getting on "http://backend/pipe/([a-z]*)/([0-9]*)/(\d+)" will return a status OK_200 and:
1364+
Given that getting on "http://backend/pipe/([a-z]*)/([0-9]*)/(\d+)" will return a status OK_200 and:
13651365
"""
13661366
$1|$2|$3
13671367
"""
@@ -1376,4 +1376,85 @@ Feature: to interact with an http service and setup mocks
13761376
c|3|4
13771377
"""
13781378
And "http://backend/pipe/[a-b]*/1/\d+" has received 1 GET
1379-
And "http://backend/pipe/.*/\d*/\d+" has received 2 GETs
1379+
And "http://backend/pipe/.*/\d*/\d+" has received 2 GETs
1380+
1381+
Scenario: We can assert the order in which the requests were received
1382+
Given that getting on "http://backend/firstEndpoint" will return a status OK_200
1383+
And that posting on "http://backend/secondEndpoint?aParam=1&anotherParam=2" will return a status OK_200
1384+
And that patching on "http://backend/thirdEndpoint" will return a status OK_200
1385+
When we get on "http://backend/firstEndpoint"
1386+
And that we post on "http://backend/secondEndpoint?aParam=1&anotherParam=2" a Request:
1387+
"""
1388+
headers.some-header: some-header-value
1389+
body.payload.message: Hello little you!
1390+
"""
1391+
And that we patch on "http://backend/thirdEndpoint"
1392+
Then the recorded interactions were in order:
1393+
"""
1394+
- method: GET
1395+
path: http://backend/firstEndpoint
1396+
- method: POST
1397+
path: http://backend/secondEndpoint?aParam=1&anotherParam=2
1398+
headers.some-header: some-header-value
1399+
body:
1400+
payload:
1401+
message: Hello little you!
1402+
- method: PATCH
1403+
path: ?e http://backend/third.*
1404+
"""
1405+
And the recorded interactions were:
1406+
"""
1407+
- method: POST
1408+
path: http://backend/secondEndpoint?anotherParam=2&aParam=1
1409+
headers.some-header: ?notNull
1410+
body:
1411+
payload:
1412+
message: Hello little you!
1413+
- method: PATCH
1414+
path: ?e http://backend/third.*
1415+
"""
1416+
But it is not true that the recorded interactions were:
1417+
"""
1418+
- method: POST
1419+
path: http://backend/secondEndpoint?anotherParam=2&aParam=1
1420+
headers.some-header: null
1421+
body:
1422+
payload:
1423+
message: Hello little you!
1424+
- method: PATCH
1425+
path: ?e http://backend/third.*
1426+
"""
1427+
And it is not true that recorded interactions were in order:
1428+
"""
1429+
- method: POST
1430+
path: http://backend/secondEndpoint?aParam=1&anotherParam=2
1431+
body:
1432+
payload:
1433+
message: Hello little you!
1434+
- method: GET
1435+
path: http://backend/firstEndpoint
1436+
- method: PATCH
1437+
path: ?e http://backend/third.*
1438+
"""
1439+
And it is not true that the recorded interactions were:
1440+
"""
1441+
- method: POST
1442+
path: http://backend/secondEndpoint?aParam=1&anotherParam=2
1443+
body:
1444+
payload:
1445+
message: Hello BIG you!
1446+
- method: GET
1447+
path: http://backend/firstEndpoint
1448+
- method: PATCH
1449+
path: ?e http://backend/third.*
1450+
"""
1451+
And it is not true that the recorded interactions were only:
1452+
"""
1453+
- method: GET
1454+
path: http://backend/firstEndpoint
1455+
- method: POST
1456+
path: http://backend/secondEndpoint?aParam=1&anotherParam=2
1457+
body:
1458+
payload:
1459+
message: Hello little you!
1460+
"""

0 commit comments

Comments
 (0)