Add deleting mocks

This commit is contained in:
Dominik Adam Przybysz 2014-12-07 21:03:25 +01:00
parent e5a782ffcc
commit f3d63cf3ef

View file

@ -3,6 +3,7 @@ package com.blogspot.przybyszd.mockserver
import com.sun.net.httpserver.HttpExchange
import com.sun.net.httpserver.HttpHandler
import com.sun.net.httpserver.HttpServer
import groovy.transform.EqualsAndHashCode
import groovy.util.slurpersupport.GPathResult
import java.util.concurrent.CopyOnWriteArrayList
@ -24,16 +25,28 @@ class HttpMockServer {
try{
GPathResult request = new XmlSlurper().parse(ex.requestBody)
if(ex.requestMethod== 'POST' && request.name() == 'addMock'){
addMockAction(request, ex)
}else if(ex.requestMethod == 'DELETE' && request.name() == 'removeMock'){
removeMockAction(request, ex)
}
//TODO add list mock
}catch(Exception e){
createErrorResponse(ex, e)
}
})
}
private void addMockAction(GPathResult request, HttpExchange ex) {
String name = request.name
if (name in actionsNames) {
throw new RuntimeException('action already registered')
throw new RuntimeException('mock already registered')
}
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
Action action = new Action(name, predicate, okResponse)
MockAction action = new MockAction(name, predicate, okResponse)
HttpServerWraper child = childServers.find { it.port == mockPort }
if (!child) {
child = new HttpServerWraper(mockPort)
@ -41,17 +54,29 @@ class HttpMockServer {
}
child.addAction(mockPath, action)
actionsNames << name
ex.responseBody << '<addedAction/>'
ex.responseBody << '<mockAdded/>'
ex.responseBody.close()
}
}catch(Exception e){
private void removeMockAction(GPathResult request, HttpExchange ex) {
String name = request.name
if (! (name in actionsNames)) {
throw new RuntimeException('mock not registered')
}
childServers.each { for (ContextExecutor e : it.executors){
MockAction action = e.actions.find {it.name == name}
if(action){
e.actions.remove(action)
}
}}
ex.responseBody << '<mockRemoved/>'
ex.responseBody.close()
}
private static void createErrorResponse(HttpExchange ex, Exception e) {
ex.responseBody << """<exceptionOccured>${e.message}</exceptionOccured>"""
ex.responseBody.close()
}
//TODO add delete mock
//TODO add list mock
})
}
void stop(){
childServers.each {it.stop()}
@ -78,7 +103,7 @@ class HttpMockServer {
httpServer.createContext(context, handler)
}
void addAction(String path, Action action){
void addAction(String path, MockAction action){
ContextExecutor executor = executors.find {it.path == path}
if(executor){
executor.actions << action
@ -100,9 +125,9 @@ class HttpMockServer {
private static final class ContextExecutor{
final HttpServerWraper httpServerWraper
final String path
List<Action> actions
List<MockAction> actions
ContextExecutor(HttpServerWraper httpServerWraper, String path, Action initialAction) {
ContextExecutor(HttpServerWraper httpServerWraper, String path, MockAction initialAction) {
this.httpServerWraper = httpServerWraper
this.path = path
this.actions = new CopyOnWriteArrayList<>([initialAction])
@ -112,7 +137,7 @@ class HttpMockServer {
String input = ex.requestBody.text
println "Mock received input"
GPathResult xml = new XmlSlurper().parseText(input)
for (Action action : actions){
for (MockAction action : actions){
if(action.predicate(xml)){
ex.responseBody << action.responseOk(xml)
ex.responseBody.close()
@ -125,14 +150,15 @@ class HttpMockServer {
}
}
private static final class Action{
@EqualsAndHashCode
private static final class MockAction {
final String name
final Closure predicate
final Closure responseOk
//TODO add http method
//TODO add is soap method
Action(String name, Closure predicate, Closure responseOk) {
MockAction(String name, Closure predicate, Closure responseOk) {
this.name = name
this.predicate = predicate
this.responseOk = responseOk