Skip to content

Commit

Permalink
feat: add ability to see response body for responses with HTML conten…
Browse files Browse the repository at this point in the history
…t type in report

Rest-Assured Integration: now it will be available to verify Response Body in report for REST Queries for responses with HTML content type
  • Loading branch information
viktor-klymenko committed Jun 17, 2016
1 parent 3e547d4 commit b5cd76d
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public RestReportingHelper() {
private static boolean shouldRecordResponseBodyFor(Response result) {
final ContentType type = ContentType.fromContentType(result.contentType());
return type != null && (ContentType.JSON == type || ContentType.XML == type
|| ContentType.TEXT == type);
|| ContentType.TEXT == type || ContentType.HTML == type);
}

public RestQuery recordRestSpecificationData(final RestMethod method, final RequestSpecificationDecorated spec,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public Map<LogDetail, String> print(final Response response) {
}
recordingStream.flush();
String recorded = new String(output.toByteArray(), StandardCharsets.UTF_8);
output.reset();
recorded = recorded.replaceAll("^(" +
"(Proxy:)|(Body:)|(Cookies:)|(Headers:)|(Multiparts:)|(Request path:)" +
")\\s*\\n*", "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,112 @@ class WhenRecordPostRequestsWithSerenityRest extends Specification {
and:
result.statusCode(200)
}

def "Should record RestAssured post() method calls with cookies"() {
given:
def JsonObject json = new JsonObject()
json.addProperty("Number", "9999")
json.addProperty("Price", "100")
def body = gson.toJson(json)
json.addProperty("SomeValue","value")
def requestBody = gson.toJson(json)
def base = "http://localhost:${wire.port()}"
def path = "/test/number"
def url = "$base$path"
def header = "Content-Type: $APPLICATION_JSON"
def cookie = "__smToken=Nguv2UFVaztFaBguF9YF7yyF"

stubFor(WireMock.post(urlEqualTo(path))
.withRequestBody(matching(".*"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "$APPLICATION_JSON")
.withHeader("Set-Cookie:", cookie)
.withBody(body)));
when:
def result = given().contentType("$APPLICATION_JSON").body(requestBody).post(url).then()
then: "The JSON request with cookies should be recorded in the test steps"
1 * test.firstListener().recordRestQuery(*_) >> { RestQuery query ->
assert "$query" == "POST $url"
assert query.method == POST
assert "${query.path}" == url
assert query.statusCode == 200
assert query.responseHeaders.contains(header)
assert query.responseCookies == cookie
assert formatted(query.responseBody) == formatted(body)
assert formatted(query.content) == formatted(requestBody)
}
and:
result.statusCode(200)
}

def "Should record RestAssured post() method calls with no cookies"() {
given:
def JsonObject json = new JsonObject()
json.addProperty("Number", "9999")
json.addProperty("Price", "100")
def body = gson.toJson(json)
json.addProperty("SomeValue","value")
def requestBody = gson.toJson(json)
def base = "http://localhost:${wire.port()}"
def path = "/test/number"
def url = "$base$path"
def header = "Content-Type: $APPLICATION_JSON"

stubFor(WireMock.post(urlEqualTo(path))
.withRequestBody(matching(".*"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "$APPLICATION_JSON")
.withBody(body)));
when:
def result = given().contentType("$APPLICATION_JSON").body(requestBody).post(url).then()
then: "The JSON request with empty cookies should be recorded in the test steps"
1 * test.firstListener().recordRestQuery(*_) >> { RestQuery query ->
assert "$query" == "POST $url"
assert query.method == POST
assert "${query.path}" == url
assert query.statusCode == 200
assert query.responseHeaders.contains(header)
assert query.responseCookies == ""
assert formatted(query.responseBody) == formatted(body)
assert formatted(query.content) == formatted(requestBody)
}
and:
result.statusCode(200)
}

def "Should record RestAssured post() method calls with body for HTML content type"() {
given:
def JsonObject json = new JsonObject()
json.addProperty("Number", "9999")
json.addProperty("Price", "100")
def body = gson.toJson(json)
def requestBody = gson.toJson(json)
def base = "http://localhost:${wire.port()}"
def path = "/test/number"
def url = "$base$path"

stubFor(WireMock.post(urlEqualTo(path))
.withRequestBody(matching(".*"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "$HTML")
.withBody(requestBody)));
when:
def result = given().contentType("$HTML").body(requestBody).post(url).then()
then: "The JSON request with body should be recorded in the test steps"
1 * test.firstListener().recordRestQuery(*_) >> { RestQuery query ->
assert "$query" == "POST $url"
assert query.method == POST
assert "${query.path}" == url
assert query.statusCode == 200
assert query.responseBody.contains(body)
}
and:
result.statusCode(200)
}
}

0 comments on commit b5cd76d

Please sign in to comment.