Add query parameters validation
This commit is contained in:
parent
c97ce11837
commit
e9eb6b55e1
6 changed files with 45 additions and 6 deletions
|
@ -17,6 +17,12 @@ class ContextExecutor {
|
||||||
httpServerWraper.createContext(path, {
|
httpServerWraper.createContext(path, {
|
||||||
HttpExchange ex ->
|
HttpExchange ex ->
|
||||||
String input = ex.requestBody.text
|
String input = ex.requestBody.text
|
||||||
|
Map<String, String> queryParams = ex.requestURI.query?.split('&')?.collectEntries {
|
||||||
|
String[] keyValue = it.split('='); [(keyValue[0]): keyValue[1]]
|
||||||
|
} ?: [:]
|
||||||
|
Map<String, String> headers = ex.requestHeaders.collectEntries {
|
||||||
|
[it.key.toLowerCase(), it.value.join(',')]
|
||||||
|
}
|
||||||
println "Mock received input"
|
println "Mock received input"
|
||||||
for (Mock mock : mocks) {
|
for (Mock mock : mocks) {
|
||||||
try {
|
try {
|
||||||
|
@ -28,10 +34,10 @@ class ContextExecutor {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Map<String,String> headers = ex.requestHeaders.collectEntries { [it.key.toLowerCase(), it.value.join(',')] }
|
|
||||||
if (ex.requestMethod == mock.method &&
|
if (ex.requestMethod == mock.method &&
|
||||||
mock.predicate(xml) &&
|
mock.predicate(xml) &&
|
||||||
mock.requestHeaders(headers)) {
|
mock.requestHeaders(headers) &&
|
||||||
|
mock.queryParams(queryParams)) {
|
||||||
println "Mock ${mock.name} invoked"
|
println "Mock ${mock.name} invoked"
|
||||||
++mock.counter
|
++mock.counter
|
||||||
String response = mock.responseOk(xml)
|
String response = mock.responseOk(xml)
|
||||||
|
|
|
@ -69,6 +69,10 @@ class HttpMockServer {
|
||||||
if(requestHeaders){
|
if(requestHeaders){
|
||||||
mock.requestHeaders = Eval.me(requestHeaders) as Closure
|
mock.requestHeaders = Eval.me(requestHeaders) as Closure
|
||||||
}
|
}
|
||||||
|
String queryParams = request.queryParams
|
||||||
|
if(queryParams){
|
||||||
|
mock.queryParams = Eval.me(queryParams) 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)
|
||||||
|
|
|
@ -14,8 +14,8 @@ class Mock {
|
||||||
String method = 'POST'
|
String method = 'POST'
|
||||||
Closure requestHeaders = {hs -> true}
|
Closure requestHeaders = {hs -> true}
|
||||||
Closure responseHeaders = {xml -> [:]}
|
Closure responseHeaders = {xml -> [:]}
|
||||||
|
Closure queryParams = {qs -> true}
|
||||||
int counter = 0
|
int counter = 0
|
||||||
//TODO add historical invocations
|
|
||||||
|
|
||||||
Mock(String name, String path, int port) {
|
Mock(String name, String path, int port) {
|
||||||
this.name = name
|
this.name = name
|
||||||
|
|
|
@ -13,6 +13,7 @@ class AddMockRequestData {
|
||||||
Method method
|
Method method
|
||||||
String responseHeaders
|
String responseHeaders
|
||||||
String requestHeaders
|
String requestHeaders
|
||||||
|
String queryParams
|
||||||
|
|
||||||
void setPredicate(String predicate) {
|
void setPredicate(String predicate) {
|
||||||
this.predicate = StringEscapeUtils.escapeXml11(predicate)
|
this.predicate = StringEscapeUtils.escapeXml11(predicate)
|
||||||
|
@ -30,6 +31,10 @@ class AddMockRequestData {
|
||||||
this.requestHeaders = StringEscapeUtils.escapeXml11(requestHeaders)
|
this.requestHeaders = StringEscapeUtils.escapeXml11(requestHeaders)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setQueryParams(String queryParams) {
|
||||||
|
this.queryParams = StringEscapeUtils.escapeXml11(queryParams)
|
||||||
|
}
|
||||||
|
|
||||||
enum Method {
|
enum Method {
|
||||||
POST,
|
POST,
|
||||||
GET,
|
GET,
|
||||||
|
|
|
@ -62,6 +62,7 @@ class ControlServerClient {
|
||||||
${data.method ? "<method>${data.method}</method>" : ''}
|
${data.method ? "<method>${data.method}</method>" : ''}
|
||||||
${data.responseHeaders ? "<responseHeaders>${data.responseHeaders}</responseHeaders>" : ''}
|
${data.responseHeaders ? "<responseHeaders>${data.responseHeaders}</responseHeaders>" : ''}
|
||||||
${data.requestHeaders ? "<requestHeaders>${data.requestHeaders}</requestHeaders>" : ''}
|
${data.requestHeaders ? "<requestHeaders>${data.requestHeaders}</requestHeaders>" : ''}
|
||||||
|
${data.queryParams ? "<queryParams>${data.queryParams}</queryParams>" : ''}
|
||||||
</addMock>
|
</addMock>
|
||||||
""", ContentType.create("text/xml", "UTF-8"))
|
""", ContentType.create("text/xml", "UTF-8"))
|
||||||
}
|
}
|
||||||
|
|
|
@ -453,7 +453,7 @@ class MockServerIntegrationTest extends Specification {
|
||||||
restPostResponse.name() == 'goodResponse'
|
restPostResponse.name() == 'goodResponse'
|
||||||
}
|
}
|
||||||
|
|
||||||
def "should add mock that accepts only when certain headers exists"() {
|
def "should add mock that accepts only when certain request headers exists"() {
|
||||||
given:
|
given:
|
||||||
controlServerClient.addMock(new AddMockRequestData(
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
name: 'testRest',
|
name: 'testRest',
|
||||||
|
@ -482,8 +482,31 @@ class MockServerIntegrationTest extends Specification {
|
||||||
restPostResponse.name() == 'goodResponse'
|
restPostResponse.name() == 'goodResponse'
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO def "should dispatch rest mock with get method and query params"(){}
|
def "should add mock that accepts only when certain query params exists"() {
|
||||||
//TODO def "should dispatch rest mock with get method and parameters"(){}
|
given:
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest',
|
||||||
|
path: '/testEndpoint',
|
||||||
|
port: 9999,
|
||||||
|
response: '''{xml -> "<goodResponse/>"}''',
|
||||||
|
queryParams: '''{ qp -> qp['q'] == '15' &&
|
||||||
|
qp.id == '1'}'''
|
||||||
|
))
|
||||||
|
HttpPost restPost = new HttpPost('http://localhost:9999/testEndpoint?q=15&id=1')
|
||||||
|
HttpPost badRestPost = new HttpPost('http://localhost:9999/testEndpoint?q=15&id=2')
|
||||||
|
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 post method and parameters"(){}
|
||||||
//TODO def "should dispatch rest mock with post method, response headers and request 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"(){}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue