[#49] Added missing request body conversion of Wiremock to DSL
This commit is contained in:
@@ -16,6 +16,7 @@ class WiremockToDslConverter {
|
||||
Object wiremockStub = new JsonSlurper().parseText(wiremockStringStub)
|
||||
def request = wiremockStub.request
|
||||
def response = wiremockStub.response
|
||||
def bodyPatterns = request.bodyPatterns
|
||||
return """\
|
||||
request {
|
||||
${request.method ? "method \"\"\"$request.method\"\"\"" : ""}
|
||||
@@ -23,18 +24,20 @@ class WiremockToDslConverter {
|
||||
${request.urlPattern ? "url \$(client(regex('${escapeJava(request.urlPattern)}')), server(''))" : ""}
|
||||
${request.urlPath ? "url \"\"\"$request.urlPath\"\"\"" : ""}
|
||||
${
|
||||
request.headers ? """headers {
|
||||
${
|
||||
request.headers.collect {
|
||||
def assertion = it.value
|
||||
String headerName = it.key as String
|
||||
def entry = assertion.entrySet().first()
|
||||
"""header(\"\"\"$headerName\"\"\", ${buildHeader(entry.key, entry.value)})\n"""
|
||||
}.join('')
|
||||
}
|
||||
}
|
||||
""" : ""
|
||||
}
|
||||
request.headers ? """headers {
|
||||
${
|
||||
request.headers.collect {
|
||||
def assertion = it.value
|
||||
String headerName = it.key as String
|
||||
def entry = assertion.entrySet().first()
|
||||
"""header(\"\"\"$headerName\"\"\", ${buildHeader(entry.key, entry.value)})\n"""
|
||||
}.join('')
|
||||
}
|
||||
}
|
||||
""" : ""
|
||||
}
|
||||
${bodyPatterns?.equalTo ? "body('''${bodyPatterns.equalTo}''')" : '' }
|
||||
${bodyPatterns?.matches ? "body \$(client(regex('${escapeJava(bodyPatterns.matches)}')), server(''))" : ""}
|
||||
}
|
||||
response {
|
||||
${response.status ? "status $response.status" : ""}
|
||||
|
||||
@@ -71,7 +71,7 @@ class WiremockToDslConverterSpec extends Specification {
|
||||
}
|
||||
|
||||
|
||||
def 'should convert Wiremock stub with body containing simple JSON'() {
|
||||
def 'should convert Wiremock stub with response body containing simple JSON'() {
|
||||
given:
|
||||
String wiremockStub = '''\
|
||||
{
|
||||
@@ -122,7 +122,7 @@ class WiremockToDslConverterSpec extends Specification {
|
||||
}""") == expectedGroovyDsl
|
||||
}
|
||||
|
||||
def 'should convert Wiremock stub with body containing integer'() {
|
||||
def 'should convert Wiremock stub with response body containing integer'() {
|
||||
given:
|
||||
String wiremockStub = '''\
|
||||
{
|
||||
@@ -171,7 +171,7 @@ class WiremockToDslConverterSpec extends Specification {
|
||||
}""") == expectedGroovyDsl
|
||||
}
|
||||
|
||||
def 'should convert Wiremock stub with body as a list'() {
|
||||
def 'should convert Wiremock stub with response body as a list'() {
|
||||
given:
|
||||
String wiremockStub = '''\
|
||||
{
|
||||
@@ -224,7 +224,7 @@ class WiremockToDslConverterSpec extends Specification {
|
||||
}
|
||||
|
||||
|
||||
def 'should convert Wiremock stub with body containing a nested list'() {
|
||||
def 'should convert Wiremock stub with response body containing a nested list'() {
|
||||
given:
|
||||
String wiremockStub = '''\
|
||||
{
|
||||
@@ -287,4 +287,80 @@ class WiremockToDslConverterSpec extends Specification {
|
||||
}""") == expectedGroovyDsl
|
||||
}
|
||||
|
||||
def 'should convert Wiremock stub with request body checking equality to Json'() {
|
||||
given:
|
||||
String wiremockStub = '''\
|
||||
{
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"url": "/test",
|
||||
"bodyPatterns": {
|
||||
"equalTo": "{\\"property1\\":\\"abc\\",\\"property2\\":\\"2017-01\\",\\"property3\\":\\"666\\",\\"property4\\":1428566412}"
|
||||
}
|
||||
},
|
||||
"response": {
|
||||
"status": 200
|
||||
}
|
||||
}
|
||||
'''
|
||||
and:
|
||||
GroovyDsl expectedGroovyDsl = GroovyDsl.make {
|
||||
request {
|
||||
method 'POST'
|
||||
url '/test'
|
||||
body ('''{"property1":"abc","property2":"2017-01","property3":"666","property4":1428566412}''')
|
||||
}
|
||||
response {
|
||||
status 200
|
||||
}
|
||||
}
|
||||
when:
|
||||
String groovyDsl = WiremockToDslConverter.fromWiremockStub(wiremockStub)
|
||||
then:
|
||||
GroovyDsl evaluatedGroovyDsl = new GroovyShell(this.class.classLoader).evaluate(
|
||||
""" io.codearte.accurest.dsl.GroovyDsl.make {
|
||||
$groovyDsl
|
||||
}""")
|
||||
and:
|
||||
evaluatedGroovyDsl == expectedGroovyDsl
|
||||
}
|
||||
|
||||
def 'should convert Wiremock stub with request body checking matching to Json'() {
|
||||
given:
|
||||
String wiremockStub = '''\
|
||||
{
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"url": "/test",
|
||||
"bodyPatterns": {
|
||||
"matches": "[0-9]{5}"
|
||||
}
|
||||
},
|
||||
"response": {
|
||||
"status": 200
|
||||
}
|
||||
}
|
||||
'''
|
||||
and:
|
||||
GroovyDsl expectedGroovyDsl = GroovyDsl.make {
|
||||
request {
|
||||
method 'POST'
|
||||
url '/test'
|
||||
body $(client(~/[0-9]{5}/), server(''))
|
||||
}
|
||||
response {
|
||||
status 200
|
||||
}
|
||||
}
|
||||
when:
|
||||
String groovyDsl = WiremockToDslConverter.fromWiremockStub(wiremockStub)
|
||||
then:
|
||||
GroovyDsl evaluatedGroovyDsl = new GroovyShell(this.class.classLoader).evaluate(
|
||||
""" io.codearte.accurest.dsl.GroovyDsl.make {
|
||||
$groovyDsl
|
||||
}""")
|
||||
and:
|
||||
evaluatedGroovyDsl == expectedGroovyDsl
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import io.codearte.accurest.dsl.internal.Request
|
||||
import io.codearte.accurest.dsl.internal.Response
|
||||
|
||||
@TypeChecked
|
||||
@EqualsAndHashCode(includeFields = true)
|
||||
@EqualsAndHashCode
|
||||
@ToString(includeFields = true, includePackage = false, includeNames = true)
|
||||
class GroovyDsl {
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import groovy.transform.ToString
|
||||
import groovy.transform.TypeChecked
|
||||
|
||||
@TypeChecked
|
||||
@EqualsAndHashCode(includeFields = true)
|
||||
@EqualsAndHashCode
|
||||
@ToString(includePackage = false, includeNames = true)
|
||||
class Request extends Common {
|
||||
|
||||
@@ -64,7 +64,7 @@ class Request extends Common {
|
||||
}
|
||||
|
||||
@CompileStatic
|
||||
@EqualsAndHashCode(includeFields = true)
|
||||
@EqualsAndHashCode
|
||||
@ToString(includePackage = false)
|
||||
class ServerRequest extends Request {
|
||||
ServerRequest(Request request) {
|
||||
@@ -73,7 +73,7 @@ class ServerRequest extends Request {
|
||||
}
|
||||
|
||||
@CompileStatic
|
||||
@EqualsAndHashCode(includeFields = true)
|
||||
@EqualsAndHashCode
|
||||
@ToString(includePackage = false)
|
||||
class ClientRequest extends Request {
|
||||
ClientRequest(Request request) {
|
||||
|
||||
@@ -6,13 +6,13 @@ import groovy.transform.ToString
|
||||
import groovy.transform.TypeChecked
|
||||
|
||||
@TypeChecked
|
||||
@EqualsAndHashCode(includeFields = true)
|
||||
@EqualsAndHashCode
|
||||
@ToString(includePackage = false, includeFields = true)
|
||||
class Response extends Common {
|
||||
|
||||
private DslProperty status
|
||||
private Headers headers
|
||||
private Body body
|
||||
DslProperty status
|
||||
Headers headers
|
||||
Body body
|
||||
|
||||
Response() {
|
||||
}
|
||||
@@ -49,21 +49,10 @@ class Response extends Common {
|
||||
this.body = new Body(bodyAsValue)
|
||||
}
|
||||
|
||||
Body getBody() {
|
||||
return body
|
||||
}
|
||||
|
||||
DslProperty getStatus() {
|
||||
return status
|
||||
}
|
||||
|
||||
Headers getHeaders() {
|
||||
return headers
|
||||
}
|
||||
}
|
||||
|
||||
@CompileStatic
|
||||
@EqualsAndHashCode(includeFields = true)
|
||||
@EqualsAndHashCode
|
||||
@ToString(includePackage = false)
|
||||
class ServerResponse extends Response {
|
||||
ServerResponse(Response request) {
|
||||
@@ -72,7 +61,7 @@ class ServerResponse extends Response {
|
||||
}
|
||||
|
||||
@CompileStatic
|
||||
@EqualsAndHashCode(includeFields = true)
|
||||
@EqualsAndHashCode
|
||||
@ToString(includePackage = false)
|
||||
class ClientResponse extends Response {
|
||||
ClientResponse(Response request) {
|
||||
|
||||
Reference in New Issue
Block a user