Skip to content

Commit

Permalink
feat: Accept multiple custom filters in request
Browse files Browse the repository at this point in the history
  • Loading branch information
shinu-ynap committed Jul 20, 2020
1 parent 5ed1914 commit f299167
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,17 @@ public RequestSpecification filter(Filter filter){
return this;
}

/**
* Add list of filters that will be used in the request
* @param filters Filter list to add
* @return the decorated request specification
*/
@Override
public RequestSpecification filters(final List<Filter> filters) {
core.filters(filters);
return this;
}

@Override
public List<Filter> getDefinedFilters() {
return core.getDefinedFilters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,38 @@ class WhenRecordGetRequestsWithSerenityRest extends Specification {
result.statusCode(200)
}

def "Should record RestAssured get() method calls with parameter provided as a list and using list of filters"() {
given:
def JsonObject json = new JsonObject()
json.addProperty("Weather", "rain")
json.addProperty("temperature", "+2")
def body = gson.toJson(json)
json.addProperty("SomeValue","value")
def requestBody = gson.toJson(json)
def base = "http://localhost:${wire.port()}"
def path = "/test/weather"
def url = "$base$path"

stubFor(WireMock.get(urlPathMatching("$path.*"))
.withRequestBody(matching(".*"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "$APPLICATION_JSON")
.withBody(body)));
when:
def result = SerenityRest.given().filters(Arrays.asList(new MyFilter(), new SecondFilter())).get("$url?status={status}", "available").then()
then: "The JSON request should be recorded in the test steps"
1 * test.firstListener().recordRestQuery(*_) >> { RestQuery query ->
assert "$query" == "GET $url?status=available"
assert query.method == GET
assert query.statusCode == 200
assert formatted(query.responseBody) == formatted(body)
}
and:
result.statusCode(200)
}


class MyFilter implements Filter {

Expand All @@ -186,4 +218,12 @@ class WhenRecordGetRequestsWithSerenityRest extends Specification {
return filterContext.next(filterableRequestSpecification,filterableResponseSpecification);
}
}

class SecondFilter implements Filter {

@Override
public Response filter(FilterableRequestSpecification filterableRequestSpecification, FilterableResponseSpecification filterableResponseSpecification, FilterContext filterContext) {
return filterContext.next(filterableRequestSpecification,filterableResponseSpecification);
}
}
}

0 comments on commit f299167

Please sign in to comment.