Add support for response headers
This commit is contained in:
parent
1be072f440
commit
3b6e4c8faf
6 changed files with 34 additions and 4 deletions
|
@ -29,10 +29,11 @@ class ContextExecutor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ex.requestMethod == mock.method && mock.predicate(xml)) {
|
if (ex.requestMethod == mock.method && mock.predicate(xml)) {
|
||||||
ex.sendResponseHeaders(mock.statusCode, 0)
|
|
||||||
println "Mock ${mock.name} invoked"
|
println "Mock ${mock.name} invoked"
|
||||||
++mock.counter
|
++mock.counter
|
||||||
String response = mock.responseOk(xml)
|
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 << (mock.soap ? wrapSoap(response) : response)
|
||||||
ex.responseBody.close()
|
ex.responseBody.close()
|
||||||
return
|
return
|
||||||
|
|
|
@ -61,6 +61,10 @@ class HttpMockServer {
|
||||||
if(method){
|
if(method){
|
||||||
mock.method = method
|
mock.method = method
|
||||||
}
|
}
|
||||||
|
String responseHeaders = request.responseHeaders
|
||||||
|
if(responseHeaders){
|
||||||
|
mock.responseHeaders = Eval.me(responseHeaders) as Closure
|
||||||
|
}
|
||||||
HttpServerWraper child = childServers.find { it.port == mockPort }
|
HttpServerWraper child = childServers.find { it.port == mockPort }
|
||||||
if (!child) {
|
if (!child) {
|
||||||
child = new HttpServerWraper(mockPort)
|
child = new HttpServerWraper(mockPort)
|
||||||
|
|
|
@ -13,7 +13,7 @@ class Mock {
|
||||||
int statusCode = 200
|
int statusCode = 200
|
||||||
String method = 'POST'
|
String method = 'POST'
|
||||||
//TODO add request headers - default [:]
|
//TODO add request headers - default [:]
|
||||||
//TODO add response headers - default [:]
|
Closure responseHeaders = {xml -> [:]}
|
||||||
int counter = 0
|
int counter = 0
|
||||||
//TODO add historical invocations
|
//TODO add historical invocations
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ class AddMockRequestData {
|
||||||
Boolean soap
|
Boolean soap
|
||||||
Integer statusCode
|
Integer statusCode
|
||||||
Method method
|
Method method
|
||||||
|
String responseHeaders
|
||||||
|
|
||||||
void setPredicate(String predicate) {
|
void setPredicate(String predicate) {
|
||||||
this.predicate = StringEscapeUtils.escapeXml11(predicate)
|
this.predicate = StringEscapeUtils.escapeXml11(predicate)
|
||||||
|
@ -20,6 +21,10 @@ class AddMockRequestData {
|
||||||
this.response = StringEscapeUtils.escapeXml11(response)
|
this.response = StringEscapeUtils.escapeXml11(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setResponseHeaders(String responseHeaders) {
|
||||||
|
this.responseHeaders = StringEscapeUtils.escapeXml11(responseHeaders)
|
||||||
|
}
|
||||||
|
|
||||||
enum Method {
|
enum Method {
|
||||||
POST,
|
POST,
|
||||||
GET,
|
GET,
|
||||||
|
|
|
@ -60,6 +60,7 @@ class ControlServerClient {
|
||||||
${data.soap != null ? "<soap>${data.soap}</soap>" : ''}
|
${data.soap != null ? "<soap>${data.soap}</soap>" : ''}
|
||||||
${data.statusCode ? "<statusCode>${data.statusCode}</statusCode>" : ''}
|
${data.statusCode ? "<statusCode>${data.statusCode}</statusCode>" : ''}
|
||||||
${data.method ? "<method>${data.method}</method>" : ''}
|
${data.method ? "<method>${data.method}</method>" : ''}
|
||||||
|
${data.responseHeaders ? "<responseHeaders>${data.responseHeaders}</responseHeaders>" : ''}
|
||||||
</addMock>
|
</addMock>
|
||||||
""", ContentType.create("text/xml", "UTF-8"))
|
""", ContentType.create("text/xml", "UTF-8"))
|
||||||
}
|
}
|
||||||
|
|
|
@ -336,7 +336,6 @@ class MockServerIntegrationTest extends Specification {
|
||||||
then:
|
then:
|
||||||
response.statusLine.statusCode == 200
|
response.statusLine.statusCode == 200
|
||||||
EntityUtils.consumeQuietly(response.entity)
|
EntityUtils.consumeQuietly(response.entity)
|
||||||
//TODO check headers
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def "should dispatch rest mock with options method"() {
|
def "should dispatch rest mock with options method"() {
|
||||||
|
@ -434,8 +433,28 @@ class MockServerIntegrationTest extends Specification {
|
||||||
secondXmlResponse.name() == 'goodResponseRest1'
|
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 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 mock report"(){}
|
||||||
//TODO def "should get list mocks"(){}
|
//TODO def "should get list mocks"(){}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue