When using file(...) we can guess the content type; fixes gh-880

This commit is contained in:
Marcin Grzejszczak
2019-12-31 10:41:06 +01:00
parent bb85b353ed
commit e3d5f9bfff
5 changed files with 65 additions and 3 deletions

View File

@@ -66,6 +66,14 @@ class FromFileProperty implements Serializable {
return this.file.bytes
}
boolean isJson() {
return this.fileName().endsWith(".json")
}
boolean isXml() {
return this.fileName().endsWith(".xml")
}
@Override
String toString() {
return asString()

View File

@@ -423,6 +423,8 @@ abstract class MethodBodyBuilder implements ClassVerifier {
byteResponseBodyCheck(bb, convertedResponseBody)
return
}
contentType = convertedResponseBody.isJson() ? JSON :
convertedResponseBody.isXml() ? XML : contentType
convertedResponseBody = convertedResponseBody.asString()
}
if (convertedResponseBody instanceof GString) {

View File

@@ -661,4 +661,39 @@ class MockMvcMethodBodyBuilderWithMatchersSpec extends Specification implements
WebTestClientJUnitMethodBodyBuilder.simpleName | { Contract dsl -> new WebTestClientJUnitMethodBodyBuilder(dsl, properties, generatedClassDataForMethod) }
}
@Issue('#880')
def 'should not generate a null statement when there is no content type in the response [#methodBuilderName]'() {
given:
Contract contractDsl = Contract.make {
description 'Should return 200'
request {
method POST()
url("/get")
headers {
contentType("application/json;charset=UTF-8")
}
}
response {
status OK()
body(file("getBody.json"))
}
}
MethodBodyBuilder builder = methodBuilder(contractDsl)
BlockBuilder blockBuilder = new BlockBuilder(" ")
and:
builder.appendTo(blockBuilder)
String test = blockBuilder.toString()
when:
SyntaxChecker.tryToCompileWithoutCompileStatic(methodBuilderName, test)
then:
!test.contains('null')
where:
methodBuilderName | methodBuilder
HttpSpockMethodRequestProcessingBodyBuilder.simpleName | { Contract dsl -> new HttpSpockMethodRequestProcessingBodyBuilder(dsl, properties, generatedClassDataForMethod) }
MockMvcJUnitMethodBodyBuilder.simpleName | { Contract dsl -> new MockMvcJUnitMethodBodyBuilder(dsl, properties, generatedClassDataForMethod) }
JaxRsClientSpockMethodRequestProcessingBodyBuilder.simpleName | { Contract dsl -> new JaxRsClientSpockMethodRequestProcessingBodyBuilder(dsl, properties, generatedClassDataForMethod) }
JaxRsClientJUnitMethodBodyBuilder.simpleName | { Contract dsl -> new JaxRsClientJUnitMethodBodyBuilder(dsl, properties, generatedClassDataForMethod) }
WebTestClientJUnitMethodBodyBuilder.simpleName | { Contract dsl -> new WebTestClientJUnitMethodBodyBuilder(dsl, properties, generatedClassDataForMethod) }
}
}

View File

@@ -0,0 +1,17 @@
[
{
"array": [
"a",
"b"
]
},
{
"array": [
"a",
"b"
]
},
{
"array": []
}
]

View File

@@ -59,10 +59,10 @@ public abstract class WireMockVerifyHelper<T, S extends WireMockVerifyHelper<T,
Map<String, Object> configuration = getConfiguration(result);
byte[] requestBodyContent = getRequestBodyContent(result);
if (requestBodyContent != null) {
String actual = new String(requestBodyContent,
Charset.forName("UTF-8"));
String actual = new String(requestBodyContent, Charset.forName("UTF-8"));
for (JsonPath jsonPath : this.jsonPaths.values()) {
new JsonPathValue(jsonPath, actual).assertHasValue(Object.class, "an object");
new JsonPathValue(jsonPath, actual).assertHasValue(Object.class,
"an object");
}
}
configuration.put("contract.jsonPaths", this.jsonPaths.keySet());