Add mock list

This commit is contained in:
Dominik Adam Przybysz 2014-12-13 21:12:56 +01:00
parent 88bb4f3067
commit ba63d36fa0
7 changed files with 120 additions and 14 deletions

View file

@ -69,6 +69,7 @@ class ContextExecutor {
mocks << mock
}
void stop(){
List<Mock> getMocks(){
return mocks
}
}

View file

@ -20,20 +20,38 @@ class HttpMockServer {
httpServerWraper.createContext('/serverControl', {
HttpExchange ex ->
try {
GPathResult request = new XmlSlurper().parse(ex.requestBody)
if (ex.requestMethod == 'POST' && request.name() == 'addMock') {
addMock(request, ex)
} else if (ex.requestMethod == 'POST' && request.name() == 'removeMock') {
removeMock(request, ex)
if (ex.requestMethod == 'GET') {
listMocks(ex)
} else if (ex.requestMethod == 'POST') {
GPathResult request = new XmlSlurper().parse(ex.requestBody)
if (request.name() == 'addMock') {
addMock(request, ex)
} else if (request.name() == 'removeMock') {
removeMock(request, ex)
} else {
throw new RuntimeException('Unknown request')
}
} else {
throw new RuntimeException('Unknown request')
}
//TODO add get mock report
//TODO add list mock
} catch (Exception e) {
createErrorResponse(ex, e)
}
})
}
void listMocks(HttpExchange ex) {
String response = '<mocks>' + listMocks().join('') + '</mocks>'
ex.sendResponseHeaders(200, 0)
ex.responseBody << response
ex.responseBody.close()
}
Set<Mock> listMocks() {
return childServers.collect { it.mocks }.flatten() as TreeSet
}
private void addMock(GPathResult request, HttpExchange ex) {
String name = request.name
if (name in mockNames) {

View file

@ -43,6 +43,10 @@ class HttpServerWraper {
}
int removeMock(String name) {
executors.inject(0) { int res, ContextExecutor e -> e.removeMock(name) + res }
return executors.inject(0) { int res, ContextExecutor e -> e.removeMock(name) + res }
}
List<Mock> getMocks(){
return executors.collect {it.mocks}.flatten()
}
}

View file

@ -5,7 +5,7 @@ import groovy.transform.PackageScope
@PackageScope
@EqualsAndHashCode(excludes = ["counter"])
class Mock {
class Mock implements Comparable<Mock>{
final String name
final String path
final int port
@ -81,4 +81,14 @@ class Mock {
this.responseHeaders = Eval.me(responseHeaders) as Closure
}
}
@Override
public String toString() {
return """<mock><port>$port</port><path>$path</path><name>$name</name></mock>"""
}
@Override
int compareTo(Mock o) {
return name.compareTo(o.name)
}
}