Content verifiers
Default content verification is based on contentType
attribute:
Content-Type | ContentVerifier |
---|---|
"application/json", "application/javascript", "text/javascript", "text/json" |
JsonVerifier |
"application/xml", "text/xml", "application/xhtml+xml" |
XmlVerifier |
"text/plain" |
ContentVerifier.Default |
This mapping may be configured by additionalContentTypeConfigs
parameter of WsPlugin
constructor:
public class Specs extends AbstractSpecs {
@Override
protected ExamExtension init() {
return new ExamExtension(
new WsPlugin(
8080,
Map.of(
ContentType.JSON,
new ContentTypeConfig(new JsonResolver(), new JsonVerifier(), new JsonPrinter())
)
)
);
}
...
}
To override content verifier for specific case case.expected.verifyAs
attribute may be used.
Built-in implementations :
verifyAs | ContentVerifier | desc |
---|---|---|
json |
JsonVerifier |
Uses JsonAssert.assertJsonEquals for verification. |
xml |
XmlVerifier |
Uses DiffBuilder.compare for verification. |
text |
ContentVerifier.Default |
Uses jUnit Assert.assertEquals for verification. |
This list may be augmented by ExamExtension.withContentVerifiers
method:
public class Specs extends AbstractSpecs {
@Override
protected ExamExtension init() {
return new ExamExtension(
...
).withContentVerifiers(
Map.of("customFormat", new ContentVerifier.Default())
);
}
}
Examples
Given
Configured additional content verifiers:
.withContentTypeConfigs(
mapOf(
"jsonIgnoreExtraFields" to JsonContentTypeConfig(
verifier = JsonVerifier { it.withOptions(IGNORING_EXTRA_FIELDS) }
),
"jsonIgnorePaths" to JsonContentTypeConfig(
verifier = JsonVerifier { it.whenIgnoringPaths("param2", "arr[*].param4") }
),
)
)
Then
Use command:
The following markup:
<e:get url="ignoreJson">
<e:case desc="verify using jsonIgnoreExtraFields">
<e:expected verifyAs="jsonIgnoreExtraFields"> { "param1":"value1", "arr":[ {"param3":"value3"}, {"param3":"value3"} ] } </e:expected>
</e:case>
<e:case desc="verify using jsonIgnorePaths">
<e:expected verifyAs="jsonIgnorePaths"> { "param1":"value1", "arr":[ {"param3":"value3"}, {"param3":"value3"} ] } </e:expected>
</e:case>
</e:get>
will be rendered as:Use cases: | |
---|---|
1) verify using jsonIgnoreExtraFields | |
| 20012ms |
2) verify using jsonIgnorePaths | |
| 20011ms |