Add support for response headers

This commit is contained in:
Dominik Adam Przybysz 2014-12-11 22:52:46 +01:00
parent 1be072f440
commit 3b6e4c8faf
6 changed files with 34 additions and 4 deletions

View file

@ -29,10 +29,11 @@ class ContextExecutor {
}
}
if (ex.requestMethod == mock.method && mock.predicate(xml)) {
ex.sendResponseHeaders(mock.statusCode, 0)
println "Mock ${mock.name} invoked"
++mock.counter
String response = mock.responseOk(xml)
mock.responseHeaders(xml).each { ex.responseHeaders.add(it.key as String, it.value as String)}
ex.sendResponseHeaders(mock.statusCode, 0)
ex.responseBody << (mock.soap ? wrapSoap(response) : response)
ex.responseBody.close()
return

View file

@ -61,6 +61,10 @@ class HttpMockServer {
if(method){
mock.method = method
}
String responseHeaders = request.responseHeaders
if(responseHeaders){
mock.responseHeaders = Eval.me(responseHeaders) as Closure
}
HttpServerWraper child = childServers.find { it.port == mockPort }
if (!child) {
child = new HttpServerWraper(mockPort)

View file

@ -13,7 +13,7 @@ class Mock {
int statusCode = 200
String method = 'POST'
//TODO add request headers - default [:]
//TODO add response headers - default [:]
Closure responseHeaders = {xml -> [:]}
int counter = 0
//TODO add historical invocations

View file

@ -11,6 +11,7 @@ class AddMockRequestData {
Boolean soap
Integer statusCode
Method method
String responseHeaders
void setPredicate(String predicate) {
this.predicate = StringEscapeUtils.escapeXml11(predicate)
@ -20,6 +21,10 @@ class AddMockRequestData {
this.response = StringEscapeUtils.escapeXml11(response)
}
void setResponseHeaders(String responseHeaders) {
this.responseHeaders = StringEscapeUtils.escapeXml11(responseHeaders)
}
enum Method {
POST,
GET,

View file

@ -60,6 +60,7 @@ class ControlServerClient {
${data.soap != null ? "<soap>${data.soap}</soap>" : ''}
${data.statusCode ? "<statusCode>${data.statusCode}</statusCode>" : ''}
${data.method ? "<method>${data.method}</method>" : ''}
${data.responseHeaders ? "<responseHeaders>${data.responseHeaders}</responseHeaders>" : ''}
</addMock>
""", ContentType.create("text/xml", "UTF-8"))
}

View file

@ -336,7 +336,6 @@ class MockServerIntegrationTest extends Specification {
then:
response.statusLine.statusCode == 200
EntityUtils.consumeQuietly(response.entity)
//TODO check headers
}
def "should dispatch rest mock with options method"() {
@ -434,8 +433,28 @@ class MockServerIntegrationTest extends Specification {
secondXmlResponse.name() == 'goodResponseRest1'
}
def "should add mock that return headers"() {
given:
controlServerClient.addMock(new AddMockRequestData(
name: 'testRest',
path: '/testEndpoint',
port: 9999,
predicate: '''{xml -> xml.name() == 'request'}''',
response: '''{xml -> "<goodResponse/>"}''',
responseHeaders: '''{ xml -> [input:"${xml.name()}"]}'''
))
HttpPost restPost = new HttpPost('http://localhost:9999/testEndpoint')
restPost.entity = new StringEntity('<request/>', ContentType.create("text/xml", "UTF-8"))
when:
CloseableHttpResponse response = client.execute(restPost)
then:
response.allHeaders.findAll { it.name == 'Input' && it.value == 'request' }
GPathResult restPostResponse = Util.extractXmlResponse(response)
restPostResponse.name() == 'goodResponse'
}
//TODO def "should dispatch rest mock with post method and request headers"(){}
//TODO def "should dispatch rest mock with post method and response headers"(){}
//TODO def "should dispatch rest mock with post method, response headers and request headers"(){}
//TODO def "should get mock report"(){}
//TODO def "should get list mocks"(){}