Add mock list
This commit is contained in:
parent
88bb4f3067
commit
ba63d36fa0
7 changed files with 120 additions and 14 deletions
|
@ -69,6 +69,7 @@ class ContextExecutor {
|
||||||
mocks << mock
|
mocks << mock
|
||||||
}
|
}
|
||||||
|
|
||||||
void stop(){
|
List<Mock> getMocks(){
|
||||||
|
return mocks
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,20 +20,38 @@ class HttpMockServer {
|
||||||
httpServerWraper.createContext('/serverControl', {
|
httpServerWraper.createContext('/serverControl', {
|
||||||
HttpExchange ex ->
|
HttpExchange ex ->
|
||||||
try {
|
try {
|
||||||
|
if (ex.requestMethod == 'GET') {
|
||||||
|
listMocks(ex)
|
||||||
|
} else if (ex.requestMethod == 'POST') {
|
||||||
GPathResult request = new XmlSlurper().parse(ex.requestBody)
|
GPathResult request = new XmlSlurper().parse(ex.requestBody)
|
||||||
if (ex.requestMethod == 'POST' && request.name() == 'addMock') {
|
if (request.name() == 'addMock') {
|
||||||
addMock(request, ex)
|
addMock(request, ex)
|
||||||
} else if (ex.requestMethod == 'POST' && request.name() == 'removeMock') {
|
} else if (request.name() == 'removeMock') {
|
||||||
removeMock(request, ex)
|
removeMock(request, ex)
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException('Unknown request')
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException('Unknown request')
|
||||||
}
|
}
|
||||||
//TODO add get mock report
|
//TODO add get mock report
|
||||||
//TODO add list mock
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
createErrorResponse(ex, 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) {
|
private void addMock(GPathResult request, HttpExchange ex) {
|
||||||
String name = request.name
|
String name = request.name
|
||||||
if (name in mockNames) {
|
if (name in mockNames) {
|
||||||
|
|
|
@ -43,6 +43,10 @@ class HttpServerWraper {
|
||||||
}
|
}
|
||||||
|
|
||||||
int removeMock(String name) {
|
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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import groovy.transform.PackageScope
|
||||||
|
|
||||||
@PackageScope
|
@PackageScope
|
||||||
@EqualsAndHashCode(excludes = ["counter"])
|
@EqualsAndHashCode(excludes = ["counter"])
|
||||||
class Mock {
|
class Mock implements Comparable<Mock>{
|
||||||
final String name
|
final String name
|
||||||
final String path
|
final String path
|
||||||
final int port
|
final int port
|
||||||
|
@ -81,4 +81,14 @@ class Mock {
|
||||||
this.responseHeaders = Eval.me(responseHeaders) as Closure
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package pl.touk.mockserver.client
|
||||||
|
|
||||||
import groovy.util.slurpersupport.GPathResult
|
import groovy.util.slurpersupport.GPathResult
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse
|
import org.apache.http.client.methods.CloseableHttpResponse
|
||||||
|
import org.apache.http.client.methods.HttpGet
|
||||||
import org.apache.http.client.methods.HttpPost
|
import org.apache.http.client.methods.HttpPost
|
||||||
import org.apache.http.entity.ContentType
|
import org.apache.http.entity.ContentType
|
||||||
import org.apache.http.entity.StringEntity
|
import org.apache.http.entity.StringEntity
|
||||||
|
@ -64,4 +65,14 @@ class ControlServerClient {
|
||||||
</addMock>
|
</addMock>
|
||||||
""", ContentType.create("text/xml", "UTF-8"))
|
""", ContentType.create("text/xml", "UTF-8"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<RegisteredMock> listMocks() {
|
||||||
|
HttpGet get = new HttpGet(address)
|
||||||
|
CloseableHttpResponse response = client.execute(get)
|
||||||
|
GPathResult xml = Util.extractXmlResponse(response)
|
||||||
|
if(xml.name() == 'mocks'){
|
||||||
|
return xml.mock.collect {new RegisteredMock(it.name.text(), it.path.text(), it.port.text() as int)}
|
||||||
|
}
|
||||||
|
return []
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
package pl.touk.mockserver.client
|
||||||
|
|
||||||
|
import groovy.transform.CompileStatic
|
||||||
|
import groovy.transform.EqualsAndHashCode
|
||||||
|
import groovy.transform.TypeChecked
|
||||||
|
|
||||||
|
@CompileStatic
|
||||||
|
@TypeChecked
|
||||||
|
@EqualsAndHashCode
|
||||||
|
class RegisteredMock {
|
||||||
|
final String name
|
||||||
|
final String path
|
||||||
|
final int port
|
||||||
|
|
||||||
|
RegisteredMock(String name, String path, int port) {
|
||||||
|
this.name = name
|
||||||
|
this.path = path
|
||||||
|
this.port = port
|
||||||
|
}
|
||||||
|
}
|
|
@ -570,6 +570,48 @@ class MockServerIntegrationTest extends Specification {
|
||||||
restPostResponse.name == 'goodResponse-1'
|
restPostResponse.name == 'goodResponse-1'
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO def "should get mock report"(){}
|
def "should get list mocks"() {
|
||||||
//TODO def "should get list mocks"(){}
|
given:
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest2',
|
||||||
|
path: 'testEndpoint',
|
||||||
|
port: 9998
|
||||||
|
))
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest4',
|
||||||
|
path: 'testEndpoint',
|
||||||
|
port: 9999
|
||||||
|
))
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest3',
|
||||||
|
path: 'testEndpoint2',
|
||||||
|
port: 9999
|
||||||
|
))
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest5',
|
||||||
|
path: 'testEndpoint',
|
||||||
|
port: 9999
|
||||||
|
))
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest6',
|
||||||
|
path: 'testEndpoint2',
|
||||||
|
port: 9999
|
||||||
|
))
|
||||||
|
controlServerClient.addMock(new AddMockRequestData(
|
||||||
|
name: 'testRest',
|
||||||
|
path: 'testEndpoint',
|
||||||
|
port: 9999
|
||||||
|
))
|
||||||
|
controlServerClient.removeMock('testRest5')
|
||||||
|
expect:
|
||||||
|
controlServerClient.listMocks() == [
|
||||||
|
new RegisteredMock('testRest', 'testEndpoint', 9999),
|
||||||
|
new RegisteredMock('testRest2', 'testEndpoint', 9998),
|
||||||
|
new RegisteredMock('testRest3', 'testEndpoint2', 9999),
|
||||||
|
new RegisteredMock('testRest4', 'testEndpoint', 9999),
|
||||||
|
new RegisteredMock('testRest6', 'testEndpoint2', 9999)
|
||||||
|
]
|
||||||
|
|
||||||
|
}
|
||||||
|
//TODO def "should get mock report"(){}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue