PathNotFoundException when request body wraps into multiline string ; fixes #458

This commit is contained in:
Marcin Grzejszczak
2017-11-27 13:59:21 +01:00
parent 8b69916754
commit 3bbdda83a9
2 changed files with 40 additions and 2 deletions

View File

@@ -6,8 +6,8 @@ import com.github.tomakehurst.wiremock.extension.responsetemplating.RequestTempl
import com.jayway.jsonpath.DocumentContext
import com.jayway.jsonpath.JsonPath
import groovy.transform.CompileStatic
import org.springframework.cloud.contract.verifier.builder.TestSideRequestTemplateModel
import org.springframework.cloud.contract.verifier.builder.TestSideRequestTemplateModel
/**
* A Handlebars helper for the {@code jsonpath} helper function.
*
@@ -38,8 +38,16 @@ class HandlebarsJsonPathHelper implements Helper<Map<String, Object>> {
}
private Object returnObjectForTest(Object model, String jsonPath) {
DocumentContext documentContext = JsonPath.parse(((TestSideRequestTemplateModel) model).rawBody)
String body = removeSurroundingQuotes(((TestSideRequestTemplateModel) model).rawBody).replace('\\"', '"')
DocumentContext documentContext = JsonPath.parse(body)
return documentContext.read(jsonPath)
}
private String removeSurroundingQuotes(String body) {
if (body.startsWith('"') && body.endsWith('"')) {
return body.substring(1, body.length() - 1)
}
return body
}
}

View File

@@ -348,6 +348,36 @@ DocumentContext parsedJson = JsonPath.parse(json);
"JaxRsClientJUnitMethodBodyBuilder" | { Contract dsl -> new JaxRsClientJUnitMethodBodyBuilder(dsl, properties) }
}
@Issue("#458")
def "should reference request from body whtn body is a string [#methodBuilderName]"() {
given:
Contract contractDsl = Contract.make {
request {
method 'GET'
url '/mytest'
body("""{ "name": "My name" }""")
}
response {
status 200
body fromRequest().body('$.name')
}
}
MethodBodyBuilder builder = methodBuilder(contractDsl)
BlockBuilder blockBuilder = new BlockBuilder(" ")
when:
builder.appendTo(blockBuilder)
then:
SyntaxChecker.tryToCompileWithoutCompileStatic(methodBuilderName, blockBuilder.toString())
and:
stubMappingIsValidWireMockStub(contractDsl)
where:
methodBuilderName | methodBuilder | responseAsserter
"MockMvcSpockMethodBuilder" | { Contract dsl -> new MockMvcSpockMethodRequestProcessingBodyBuilder(dsl, properties) } | { String string -> assert string.contains('responseBody == "My name"') }
"MockMvcJUnitMethodBuilder" | { Contract dsl -> new MockMvcJUnitMethodBodyBuilder(dsl, properties) } | { String string -> assert string.contains('assertThat(responseBody).isEqualTo("My name");') }
"JaxRsClientSpockMethodRequestProcessingBodyBuilder" | { Contract dsl -> new JaxRsClientSpockMethodRequestProcessingBodyBuilder(dsl, properties) } | { String string -> assert string.contains('responseBody == "My name"') }
"JaxRsClientJUnitMethodBodyBuilder" | { Contract dsl -> new JaxRsClientJUnitMethodBodyBuilder(dsl, properties) } | { String string -> assert string.contains('assertThat(responseBody).isEqualTo("My name");') }
}
def "should use fixed delay milliseconds in the generated test [#methodBuilderName]"() {
given:
Contract contractDsl = Contract.make {