When using file(...) we can guess the content type; fixes gh-880
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
[
|
||||
{
|
||||
"array": [
|
||||
"a",
|
||||
"b"
|
||||
]
|
||||
},
|
||||
{
|
||||
"array": [
|
||||
"a",
|
||||
"b"
|
||||
]
|
||||
},
|
||||
{
|
||||
"array": []
|
||||
}
|
||||
]
|
||||
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user