Don't parse the stream content type to generate stubs

without this change we're parsing the stream's output
with this change we treat it as it is

fixes gh-1535
This commit is contained in:
Marcin Grzejszczak
2020-12-10 13:50:16 +01:00
parent 919f4373db
commit 2b0ef2f2fc
2 changed files with 74 additions and 1 deletions

View File

@@ -46,11 +46,13 @@ class WireMockResponseStubStrategy extends BaseWireMockStubStrategy {
private final Response response
private final ContentType contentType
private final SingleContractMetadata contractMetadata
WireMockResponseStubStrategy(Contract groovyDsl, SingleContractMetadata singleContractMetadata) {
super(groovyDsl)
this.response = groovyDsl.response
this.contentType = contentType(singleContractMetadata)
this.contractMetadata = singleContractMetadata
}
protected ContentType contentType(SingleContractMetadata singleContractMetadata) {
@@ -94,7 +96,8 @@ class WireMockResponseStubStrategy extends BaseWireMockStubStrategy {
private void appendBody(ResponseDefinitionBuilder builder) {
if (response.body) {
Object body = MapConverter.getStubSideValues(response.body)
Closure parsingClosure = parsingClosureForContentType()
Object body = MapConverter.getStubSideValues(response.body, parsingClosure)
if (body instanceof byte[]) {
builder.withBody(body)
}
@@ -107,6 +110,10 @@ class WireMockResponseStubStrategy extends BaseWireMockStubStrategy {
}
}
private Closure parsingClosureForContentType() {
return contractMetadata.getDefinedOutputTestContentType().contains("/stream") ? Closure.IDENTITY : MapConverter.JSON_PARSING_CLOSURE
}
private void appendResponseDelayTime(ResponseDefinitionBuilder builder) {
// TODO: Add a missing test for this
if (response.delay) {

View File

@@ -2902,6 +2902,66 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
}
@Issue("#1535")
def "should work with streams"() {
given:
Contract contractDsl = Contract.make {
description "should return all entities"
request{
method GET()
url("/api/v1/entities")
}
response {
headers {
header 'Content-Type': 'application/stream+json'
}
body(
'''
{
"id" : "1",
"name" : "Entity1",
"nested_data" : {
"key1" : "value1"
}
},
{
"id" : "2",
"name" : "Entity2",
"nested_data" : {
"key1" : "value1"
}
}
'''
)
status 200
}
}
when:
String wireMockStub = new WireMockStubStrategy("Test",
new ContractMetadata(null, false, 0, null, contractDsl), contractDsl)
.toWireMockClientStub()
then:
wireMockStub.contains('Entity1')
wireMockStub.contains('Entity2')
stubMappingIsValidWireMockStub(wireMockStub)
and:
int port = SocketUtils.findAvailableTcpPort()
WireMockServer server = new WireMockServer(config().port(port))
server.start()
and:
stubMappingIsValidWireMockStub(wireMockStub)
server.addStubMapping(WireMockStubMapping.buildFrom(wireMockStub))
when:
ResponseEntity<String> entity = callForStream(port)
then:
entity.statusCodeValue == 200
entity.body.contains("Entity1")
entity.body.contains("Entity2")
cleanup:
server?.shutdown()
}
@Issue("#1125")
def "should not generate assertions for [*] when all manual entries were passed"() {
given:
@@ -3028,6 +3088,12 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
.body("{\"foo\":null,\"name\":\"\"}"), String.class)
}
ResponseEntity<String> callForStream(int port) {
return new TestRestTemplate().exchange(
RequestEntity.get(URI.create("http://localhost:" + port + "/api/v1/entities"))
.header("Content-Type", "application/stream+json").build(), String.class)
}
ResponseEntity<byte[]> callBytes(int port, File request) {
return new TestRestTemplate().exchange(
RequestEntity.put(URI.create("http://localhost:" + port + "/1"))