Merge pull request #2 from smaragda/feature/patern_matching_to_request_body

added possibility to generate wiremock stubs with regexp matching
This commit is contained in:
Marcin Grzejszczak
2015-05-06 17:51:57 +02:00
2 changed files with 73 additions and 10 deletions

View File

@@ -39,11 +39,23 @@ class WiremockRequestStubStrategy extends BaseWiremockStubStrategy {
private Map<String, Object> appendBody(ClientRequest clientRequest) {
Object body = clientRequest?.body?.clientValue
return body != null ? [bodyPatterns: [equalTo: parseBody(body)]] : [:]
if (body == null) {
return [:]
}
if (containsRegex(body)) {
return [bodyPatterns: [matches: parseBody(body)]]
}
return [bodyPatterns: [equalTo: parseBody(body)]]
}
private String parseBody(Object responseBodyObject) {
String responseBody = responseBodyObject as String
boolean containsRegex(Object bodyObject) {
String bodyString = bodyObject as String
return (bodyString =~ /\^.*\$/).find()
}
private String parseBody(Object bodyObject) {
String responseBody = bodyObject as String
try {
def json = new JsonSlurper().parseText(responseBody)
return escapeJava(JsonOutput.toJson(responseBody))

View File

@@ -111,13 +111,12 @@ class WiremockGroovyDslSpec extends Specification {
GroovyDsl groovyDsl = GroovyDsl.make {
request {
method('GET')
url $(client(~/\/[0-9]{2}/), server('/12'))
body("""\
{
"name": "Jan"
}
"""
)
url $(client(regex('/[0-9]{2}')), server('/12'))
body """
{
"name": "Jan"
}
"""
}
response {
status 200
@@ -157,6 +156,58 @@ class WiremockGroovyDslSpec extends Specification {
''')
}
def 'should convert groovy dsl stub with regexp Body as String to wiremock stub for the client side'() {
given:
GroovyDsl groovyDsl = GroovyDsl.make {
request {
method('GET')
url $(client(regex('/[0-9]{2}')), server('/12'))
body """
{
"personalId": "${value(client(regex('^[0-9]{11}$')), server('57593728525'))}"
}
"""
}
response {
status 200
body("""\
{
"name": "Jan"
}
"""
)
headers {
header 'Content-Type': 'text/plain'
}
}
}
when:
String wiremockStub = new WiremockStubStrategy(groovyDsl).toWiremockClientStub()
then:
new JsonSlurper().parseText(wiremockStub) == new JsonSlurper().parseText('''
{
"request": {
"method": "GET",
"urlPattern": "/[0-9]{2}",
"bodyPatterns": {
"matches":"{\\"personalId\\":\\"^[0-9]{11}$\\"}"
}
},
"response": {
"status": 200,
"body": {
"name": "Jan"
},
"headers": {
"Content-Type": "text/plain"
}
}
}
''')
}
def "should generate stub with GET"() {
given:
GroovyDsl groovyDsl = GroovyDsl.make {