Add deleting mocks
This commit is contained in:
parent
e5a782ffcc
commit
f3d63cf3ef
1 changed files with 57 additions and 31 deletions
|
@ -3,6 +3,7 @@ package com.blogspot.przybyszd.mockserver
|
||||||
import com.sun.net.httpserver.HttpExchange
|
import com.sun.net.httpserver.HttpExchange
|
||||||
import com.sun.net.httpserver.HttpHandler
|
import com.sun.net.httpserver.HttpHandler
|
||||||
import com.sun.net.httpserver.HttpServer
|
import com.sun.net.httpserver.HttpServer
|
||||||
|
import groovy.transform.EqualsAndHashCode
|
||||||
import groovy.util.slurpersupport.GPathResult
|
import groovy.util.slurpersupport.GPathResult
|
||||||
|
|
||||||
import java.util.concurrent.CopyOnWriteArrayList
|
import java.util.concurrent.CopyOnWriteArrayList
|
||||||
|
@ -24,16 +25,28 @@ class HttpMockServer {
|
||||||
try{
|
try{
|
||||||
GPathResult request = new XmlSlurper().parse(ex.requestBody)
|
GPathResult request = new XmlSlurper().parse(ex.requestBody)
|
||||||
if(ex.requestMethod== 'POST' && request.name() == 'addMock'){
|
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
|
String name = request.name
|
||||||
if (name in actionsNames) {
|
if (name in actionsNames) {
|
||||||
throw new RuntimeException('action already registered')
|
throw new RuntimeException('mock already registered')
|
||||||
}
|
}
|
||||||
println "Adding $name"
|
println "Adding $name"
|
||||||
String mockPath = request.path
|
String mockPath = request.path
|
||||||
int mockPort = Integer.valueOf(request.port as String)
|
int mockPort = Integer.valueOf(request.port as String)
|
||||||
Closure predicate = Eval.me(request.predicate as String) as Closure
|
Closure predicate = Eval.me(request.predicate as String) as Closure
|
||||||
Closure okResponse = Eval.me(request.response 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 }
|
HttpServerWraper child = childServers.find { it.port == mockPort }
|
||||||
if (!child) {
|
if (!child) {
|
||||||
child = new HttpServerWraper(mockPort)
|
child = new HttpServerWraper(mockPort)
|
||||||
|
@ -41,17 +54,29 @@ class HttpMockServer {
|
||||||
}
|
}
|
||||||
child.addAction(mockPath, action)
|
child.addAction(mockPath, action)
|
||||||
actionsNames << name
|
actionsNames << name
|
||||||
ex.responseBody << '<addedAction/>'
|
ex.responseBody << '<mockAdded/>'
|
||||||
ex.responseBody.close()
|
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 << """<exceptionOccured>${e.message}</exceptionOccured>"""
|
||||||
ex.responseBody.close()
|
ex.responseBody.close()
|
||||||
}
|
}
|
||||||
//TODO add delete mock
|
|
||||||
//TODO add list mock
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
void stop(){
|
void stop(){
|
||||||
childServers.each {it.stop()}
|
childServers.each {it.stop()}
|
||||||
|
@ -78,7 +103,7 @@ class HttpMockServer {
|
||||||
httpServer.createContext(context, handler)
|
httpServer.createContext(context, handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
void addAction(String path, Action action){
|
void addAction(String path, MockAction action){
|
||||||
ContextExecutor executor = executors.find {it.path == path}
|
ContextExecutor executor = executors.find {it.path == path}
|
||||||
if(executor){
|
if(executor){
|
||||||
executor.actions << action
|
executor.actions << action
|
||||||
|
@ -100,9 +125,9 @@ class HttpMockServer {
|
||||||
private static final class ContextExecutor{
|
private static final class ContextExecutor{
|
||||||
final HttpServerWraper httpServerWraper
|
final HttpServerWraper httpServerWraper
|
||||||
final String path
|
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.httpServerWraper = httpServerWraper
|
||||||
this.path = path
|
this.path = path
|
||||||
this.actions = new CopyOnWriteArrayList<>([initialAction])
|
this.actions = new CopyOnWriteArrayList<>([initialAction])
|
||||||
|
@ -112,7 +137,7 @@ class HttpMockServer {
|
||||||
String input = ex.requestBody.text
|
String input = ex.requestBody.text
|
||||||
println "Mock received input"
|
println "Mock received input"
|
||||||
GPathResult xml = new XmlSlurper().parseText(input)
|
GPathResult xml = new XmlSlurper().parseText(input)
|
||||||
for (Action action : actions){
|
for (MockAction action : actions){
|
||||||
if(action.predicate(xml)){
|
if(action.predicate(xml)){
|
||||||
ex.responseBody << action.responseOk(xml)
|
ex.responseBody << action.responseOk(xml)
|
||||||
ex.responseBody.close()
|
ex.responseBody.close()
|
||||||
|
@ -125,14 +150,15 @@ class HttpMockServer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final class Action{
|
@EqualsAndHashCode
|
||||||
|
private static final class MockAction {
|
||||||
final String name
|
final String name
|
||||||
final Closure predicate
|
final Closure predicate
|
||||||
final Closure responseOk
|
final Closure responseOk
|
||||||
//TODO add http method
|
//TODO add http method
|
||||||
//TODO add is soap method
|
//TODO add is soap method
|
||||||
|
|
||||||
Action(String name, Closure predicate, Closure responseOk) {
|
MockAction(String name, Closure predicate, Closure responseOk) {
|
||||||
this.name = name
|
this.name = name
|
||||||
this.predicate = predicate
|
this.predicate = predicate
|
||||||
this.responseOk = responseOk
|
this.responseOk = responseOk
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue