Add setting status code in mock
This commit is contained in:
parent
a7c6050e09
commit
b3e3dfb3f3
7 changed files with 118 additions and 35 deletions
|
@ -16,7 +16,6 @@ class ContextExecutor {
|
|||
this.mocks = new CopyOnWriteArrayList<>([initialMock])
|
||||
httpServerWraper.createContext(path,{
|
||||
HttpExchange ex ->
|
||||
ex.sendResponseHeaders(200, 0)
|
||||
String input = ex.requestBody.text
|
||||
println "Mock received input"
|
||||
GPathResult inputXml = new XmlSlurper().parseText(input)
|
||||
|
@ -31,6 +30,7 @@ class ContextExecutor {
|
|||
}
|
||||
}
|
||||
if (xml != null && mock.predicate(xml)) {
|
||||
ex.sendResponseHeaders(mock.statusCode, 0)
|
||||
println "Mock ${mock.name} invoked"
|
||||
++mock.counter
|
||||
String response = mock.responseOk(xml)
|
||||
|
@ -42,6 +42,7 @@ class ContextExecutor {
|
|||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
ex.sendResponseHeaders(404, 0)
|
||||
ex.responseBody << "<invalidInput/>"
|
||||
ex.responseBody.close()
|
||||
})
|
||||
|
|
|
@ -17,7 +17,6 @@ class HttpMockServer {
|
|||
|
||||
httpServerWraper.createContext('/serverControl', {
|
||||
HttpExchange ex ->
|
||||
ex.sendResponseHeaders(200, 0)
|
||||
try{
|
||||
GPathResult request = new XmlSlurper().parse(ex.requestBody)
|
||||
if(ex.requestMethod== 'POST' && request.name() == 'addMock'){
|
||||
|
@ -41,10 +40,23 @@ class HttpMockServer {
|
|||
println "Adding $name"
|
||||
String mockPath = request.path
|
||||
int mockPort = Integer.valueOf(request.port as String)
|
||||
Closure predicate = Eval.me(request.predicate as String) as Closure
|
||||
Closure okResponse = Eval.me(request.response as String) as Closure
|
||||
boolean soap = Boolean.valueOf(request.soap as String)
|
||||
Mock mock = new Mock(name, mockPath, mockPort, predicate, okResponse, soap)
|
||||
Mock mock = new Mock(name, mockPath, mockPort)
|
||||
String predicate = request.predicate
|
||||
if(predicate){
|
||||
mock.predicate = Eval.me(predicate) as Closure
|
||||
}
|
||||
String okResponse = request.response
|
||||
if(okResponse){
|
||||
mock.responseOk = Eval.me(okResponse) as Closure
|
||||
}
|
||||
String soap = request.soap
|
||||
if(soap){
|
||||
mock.soap = Boolean.valueOf(soap)
|
||||
}
|
||||
String statusCode = request.statusCode
|
||||
if(statusCode){
|
||||
mock.statusCode = Integer.valueOf(statusCode)
|
||||
}
|
||||
HttpServerWraper child = childServers.find { it.port == mockPort }
|
||||
if (!child) {
|
||||
child = new HttpServerWraper(mockPort)
|
||||
|
@ -52,6 +64,7 @@ class HttpMockServer {
|
|||
}
|
||||
child.addMock(mockPath, mock)
|
||||
mockNames << name
|
||||
ex.sendResponseHeaders(200, 0)
|
||||
ex.responseBody << '<mockAdded/>'
|
||||
ex.responseBody.close()
|
||||
}
|
||||
|
@ -64,11 +77,13 @@ class HttpMockServer {
|
|||
println "Removing $name"
|
||||
int used = childServers.inject(0) { int res, HttpServerWraper server-> server.removeMock(name) + res}
|
||||
mockNames.remove(name)
|
||||
ex.sendResponseHeaders(200, 0)
|
||||
ex.responseBody << "<mockRemoved>$used</mockRemoved>"
|
||||
ex.responseBody.close()
|
||||
}
|
||||
|
||||
private static void createErrorResponse(HttpExchange ex, Exception e) {
|
||||
ex.sendResponseHeaders(400, 0)
|
||||
ex.responseBody << """<exceptionOccured>${e.message}</exceptionOccured>"""
|
||||
ex.responseBody.close()
|
||||
}
|
||||
|
|
|
@ -7,21 +7,18 @@ class Mock {
|
|||
final String name
|
||||
final String path
|
||||
final int port
|
||||
final Closure predicate
|
||||
final Closure responseOk
|
||||
final boolean soap
|
||||
//TODO add http method
|
||||
//TODO add http code
|
||||
//TODO add request headers
|
||||
//TODO add response headers
|
||||
Closure predicate = { xml -> true }
|
||||
Closure responseOk = { xml -> '' }
|
||||
boolean soap = false
|
||||
int statusCode = 200
|
||||
//TODO add http method - default POST
|
||||
//TODO add request headers - default [:]
|
||||
//TODO add response headers - default [:]
|
||||
int counter = 0
|
||||
//TODO add historical invocations
|
||||
|
||||
Mock(String name, String path, int port, Closure predicate, Closure responseOk, boolean soap) {
|
||||
Mock(String name, String path, int port) {
|
||||
this.name = name
|
||||
this.predicate = predicate
|
||||
this.responseOk = responseOk
|
||||
this.soap = soap
|
||||
this.path = path
|
||||
this.port = port
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue