Add soap support

This commit is contained in:
Dominik Adam Przybysz 2014-12-08 20:13:07 +01:00
parent 54bbfd7fdf
commit 5db690c434
3 changed files with 36 additions and 10 deletions

View file

@ -19,21 +19,38 @@ class ContextExecutor {
ex.sendResponseHeaders(200, 0)
String input = ex.requestBody.text
println "Mock received input"
GPathResult xml = new XmlSlurper().parseText(input)
GPathResult inputXml = new XmlSlurper().parseText(input)
for (Mock mock : mocks){
if(mock.predicate(xml)){
GPathResult xml = inputXml
try {
if (mock.soap) {
if(xml.name() == 'Envelope' && xml.Body.size() > 0){
xml = getSoapBodyContent(xml)
}else{
continue
}
}
if (xml != null && mock.predicate(xml)) {
println "Mock ${mock.name} invoked"
++mock.counter
ex.responseBody << mock.responseOk(xml)
String response = mock.responseOk(xml)
ex.responseBody << (mock.soap ? wrapSoap(response) : response)
ex.responseBody.close()
return
}
}catch (Exception e){
e.printStackTrace()
}
}
ex.responseBody << "<invalidInput/>"
ex.responseBody.close()
})
}
private static GPathResult getSoapBodyContent(GPathResult xml) {
return xml.Body.'**'[1]
}
int removeMock(String name) {
Mock mock = mocks.find {it.name == name}
if(mock){
@ -41,4 +58,11 @@ class ContextExecutor {
}
return mock.counter
}
private static String wrapSoap(String request) {
"""<?xml version='1.0' encoding='UTF-8'?>
<soap-env:Envelope xmlns:soap-env='http://schemas.xmlsoap.org/soap/envelope/' xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
<soap-env:Body>${request}</soap-env:Body>
</soap-env:Envelope>"""
}
}

View file

@ -43,7 +43,8 @@ class HttpMockServer {
int mockPort = Integer.valueOf(request.port as String)
Closure predicate = Eval.me(request.predicate as String) as Closure
Closure okResponse = Eval.me(request.response as String) as Closure
Mock mock = new Mock(name, predicate, okResponse)
boolean soap = Boolean.valueOf(request.soap as String)
Mock mock = new Mock(name, predicate, okResponse, soap)
HttpServerWraper child = childServers.find { it.port == mockPort }
if (!child) {
child = new HttpServerWraper(mockPort)

View file

@ -7,13 +7,14 @@ class Mock {
final String name
final Closure predicate
final Closure responseOk
final boolean soap
//TODO add http method
//TODO add is soap method
int counter = 0
Mock(String name, Closure predicate, Closure responseOk) {
Mock(String name, Closure predicate, Closure responseOk, boolean soap) {
this.name = name
this.predicate = predicate
this.responseOk = responseOk
this.soap = soap
}
}