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,14 +19,27 @@ 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)){
println "Mock ${mock.name} invoked"
++mock.counter
ex.responseBody << mock.responseOk(xml)
ex.responseBody.close()
return
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
String response = mock.responseOk(xml)
ex.responseBody << (mock.soap ? wrapSoap(response) : response)
ex.responseBody.close()
return
}
}catch (Exception e){
e.printStackTrace()
}
}
ex.responseBody << "<invalidInput/>"
@ -34,6 +47,10 @@ class ContextExecutor {
})
}
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>"""
}
}