Add request headers validation

This commit is contained in:
Dominik Adam Przybysz 2014-12-12 19:16:09 +01:00
parent 9670cd6d7e
commit c97ce11837
6 changed files with 48 additions and 5 deletions

View file

@ -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,

View file

@ -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"))
}

View file

@ -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"(){}