Add request headers validation
This commit is contained in:
parent
9670cd6d7e
commit
c97ce11837
6 changed files with 48 additions and 5 deletions
|
@ -28,7 +28,10 @@ class ContextExecutor {
|
|||
continue
|
||||
}
|
||||
}
|
||||
if (ex.requestMethod == mock.method && mock.predicate(xml)) {
|
||||
Map<String,String> headers = ex.requestHeaders.collectEntries { [it.key.toLowerCase(), it.value.join(',')] }
|
||||
if (ex.requestMethod == mock.method &&
|
||||
mock.predicate(xml) &&
|
||||
mock.requestHeaders(headers)) {
|
||||
println "Mock ${mock.name} invoked"
|
||||
++mock.counter
|
||||
String response = mock.responseOk(xml)
|
||||
|
|
|
@ -65,6 +65,10 @@ class HttpMockServer {
|
|||
if(responseHeaders){
|
||||
mock.responseHeaders = Eval.me(responseHeaders) as Closure
|
||||
}
|
||||
String requestHeaders = request.requestHeaders
|
||||
if(requestHeaders){
|
||||
mock.requestHeaders = Eval.me(requestHeaders) as Closure
|
||||
}
|
||||
HttpServerWraper child = childServers.find { it.port == mockPort }
|
||||
if (!child) {
|
||||
child = new HttpServerWraper(mockPort)
|
||||
|
|
|
@ -12,7 +12,7 @@ class Mock {
|
|||
boolean soap = false
|
||||
int statusCode = 200
|
||||
String method = 'POST'
|
||||
//TODO add request headers - default [:]
|
||||
Closure requestHeaders = {hs -> true}
|
||||
Closure responseHeaders = {xml -> [:]}
|
||||
int counter = 0
|
||||
//TODO add historical invocations
|
||||
|
|
|
@ -12,6 +12,7 @@ class AddMockRequestData {
|
|||
Integer statusCode
|
||||
Method method
|
||||
String responseHeaders
|
||||
String requestHeaders
|
||||
|
||||
void setPredicate(String predicate) {
|
||||
this.predicate = StringEscapeUtils.escapeXml11(predicate)
|
||||
|
@ -25,6 +26,10 @@ class AddMockRequestData {
|
|||
this.responseHeaders = StringEscapeUtils.escapeXml11(responseHeaders)
|
||||
}
|
||||
|
||||
void setRequestHeaders(String requestHeaders) {
|
||||
this.requestHeaders = StringEscapeUtils.escapeXml11(requestHeaders)
|
||||
}
|
||||
|
||||
enum Method {
|
||||
POST,
|
||||
GET,
|
||||
|
|
|
@ -61,6 +61,7 @@ class ControlServerClient {
|
|||
${data.statusCode ? "<statusCode>${data.statusCode}</statusCode>" : ''}
|
||||
${data.method ? "<method>${data.method}</method>" : ''}
|
||||
${data.responseHeaders ? "<responseHeaders>${data.responseHeaders}</responseHeaders>" : ''}
|
||||
${data.requestHeaders ? "<requestHeaders>${data.requestHeaders}</requestHeaders>" : ''}
|
||||
</addMock>
|
||||
""", ContentType.create("text/xml", "UTF-8"))
|
||||
}
|
||||
|
|
|
@ -441,19 +441,49 @@ class MockServerIntegrationTest extends Specification {
|
|||
port: 9999,
|
||||
predicate: '''{xml -> xml.name() == 'request'}''',
|
||||
response: '''{xml -> "<goodResponse/>"}''',
|
||||
responseHeaders: '''{ xml -> [input:"${xml.name()}"]}'''
|
||||
responseHeaders: '''{ xml -> ['Input-Name':"${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' }
|
||||
response.allHeaders.findAll { it.name.toLowerCase() == 'input-name' && it.value == 'request' }
|
||||
GPathResult restPostResponse = Util.extractXmlResponse(response)
|
||||
restPostResponse.name() == 'goodResponse'
|
||||
}
|
||||
|
||||
//TODO def "should dispatch rest mock with post method and request headers"(){}
|
||||
def "should add mock that accepts only when certain headers exists"() {
|
||||
given:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest',
|
||||
path: '/testEndpoint',
|
||||
port: 9999,
|
||||
response: '''{xml -> "<goodResponse/>"}''',
|
||||
requestHeaders: '''{ hs -> hs['user-agent']?.startsWith('Mozilla') &&
|
||||
hs.pragma == 'no-cache'}'''
|
||||
))
|
||||
HttpPost restPost = new HttpPost('http://localhost:9999/testEndpoint')
|
||||
restPost.entity = new StringEntity('<request/>', ContentType.create("text/xml", "UTF-8"))
|
||||
restPost.addHeader('User-Agent', 'Mozilla/5.0')
|
||||
restPost.addHeader('Pragma', 'no-cache')
|
||||
HttpPost badRestPost = new HttpPost('http://localhost:9999/testEndpoint')
|
||||
badRestPost.entity = new StringEntity('<request/>', ContentType.create("text/xml", "UTF-8"))
|
||||
badRestPost.addHeader('Pragma', 'no-cache')
|
||||
when:
|
||||
CloseableHttpResponse badResponse = client.execute(badRestPost)
|
||||
then:
|
||||
GPathResult badRestPostResponse = Util.extractXmlResponse(badResponse)
|
||||
badRestPostResponse.name() == 'invalidInput'
|
||||
when:
|
||||
CloseableHttpResponse response = client.execute(restPost)
|
||||
then:
|
||||
GPathResult restPostResponse = Util.extractXmlResponse(response)
|
||||
restPostResponse.name() == 'goodResponse'
|
||||
}
|
||||
|
||||
//TODO def "should dispatch rest mock with get method and query params"(){}
|
||||
//TODO def "should dispatch rest mock with get method and parameters"(){}
|
||||
//TODO def "should dispatch rest mock with post method, response headers and request headers"(){}
|
||||
|
||||
//TODO def "should get mock report"(){}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue