Add mock validation
This commit is contained in:
parent
fda5b6ca5c
commit
88bb4f3067
6 changed files with 78 additions and 51 deletions
|
@ -13,7 +13,7 @@ class ContextExecutor {
|
|||
|
||||
ContextExecutor(HttpServerWraper httpServerWraper, Mock initialMock) {
|
||||
this.httpServerWraper = httpServerWraper
|
||||
this.path = initialMock.path
|
||||
this.path = '/' + initialMock.path
|
||||
this.mocks = new CopyOnWriteArrayList<>([initialMock])
|
||||
httpServerWraper.createContext(path, {
|
||||
HttpExchange ex ->
|
||||
|
@ -36,6 +36,14 @@ class ContextExecutor {
|
|||
})
|
||||
}
|
||||
|
||||
String getPath() {
|
||||
return path.substring(1)
|
||||
}
|
||||
|
||||
String getContextPath() {
|
||||
return path
|
||||
}
|
||||
|
||||
private static void fillExchange(HttpExchange httpExchange, MockResponse response) {
|
||||
response.headers.each {
|
||||
httpExchange.responseHeaders.add(it.key, it.value)
|
||||
|
@ -60,4 +68,7 @@ class ContextExecutor {
|
|||
void addMock(Mock mock) {
|
||||
mocks << mock
|
||||
}
|
||||
|
||||
void stop(){
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ class HttpServerWraper {
|
|||
}
|
||||
|
||||
void stop() {
|
||||
executors.each { httpServer.removeContext(it.path) }
|
||||
executors.each { httpServer.removeContext(it.contextPath) }
|
||||
httpServer.stop(0)
|
||||
}
|
||||
|
||||
|
|
|
@ -18,6 +18,9 @@ class Mock {
|
|||
int counter = 0
|
||||
|
||||
Mock(String name, String path, int port) {
|
||||
if (!(name)) {
|
||||
throw new RuntimeException("Mock name must be given")
|
||||
}
|
||||
this.name = name
|
||||
this.path = path
|
||||
this.port = port
|
||||
|
@ -43,37 +46,37 @@ class Mock {
|
|||
</soap-env:Envelope>"""
|
||||
}
|
||||
|
||||
void setPredicate(String predicate){
|
||||
void setPredicate(String predicate) {
|
||||
if (predicate) {
|
||||
this.predicate = Eval.me(predicate) as Closure
|
||||
}
|
||||
}
|
||||
|
||||
void setResponse(String response){
|
||||
void setResponse(String response) {
|
||||
if (response) {
|
||||
this.response = Eval.me(response) as Closure
|
||||
}
|
||||
}
|
||||
|
||||
void setSoap(String soap){
|
||||
void setSoap(String soap) {
|
||||
if (soap) {
|
||||
this.soap = Boolean.valueOf(soap)
|
||||
}
|
||||
}
|
||||
|
||||
void setStatusCode(String statusCode){
|
||||
void setStatusCode(String statusCode) {
|
||||
if (statusCode) {
|
||||
this.statusCode = Integer.valueOf(statusCode)
|
||||
}
|
||||
}
|
||||
|
||||
void setMethod(String method){
|
||||
if(method){
|
||||
void setMethod(String method) {
|
||||
if (method) {
|
||||
this.method = method
|
||||
}
|
||||
}
|
||||
|
||||
void setResponseHeaders(String responseHeaders){
|
||||
void setResponseHeaders(String responseHeaders) {
|
||||
if (responseHeaders) {
|
||||
this.responseHeaders = Eval.me(responseHeaders) as Closure
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ class ControlServerClient {
|
|||
throw new MockAlreadyExists()
|
||||
|
||||
}
|
||||
throw new InvalidMockDefinitionException(responseXml.text())
|
||||
throw new InvalidMockDefinition(responseXml.text())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5,8 +5,8 @@ import groovy.transform.TypeChecked
|
|||
|
||||
@CompileStatic
|
||||
@TypeChecked
|
||||
class InvalidMockDefinitionException extends RuntimeException {
|
||||
InvalidMockDefinitionException(String s) {
|
||||
class InvalidMockDefinition extends RuntimeException {
|
||||
InvalidMockDefinition(String s) {
|
||||
super(s)
|
||||
}
|
||||
}
|
|
@ -34,7 +34,7 @@ class MockServerIntegrationTest extends Specification {
|
|||
expect:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest',
|
||||
path: '/testEndpoint',
|
||||
path: 'testEndpoint',
|
||||
port: 9999,
|
||||
predicate: '''{req -> req.xml.name() == 'request'}''',
|
||||
response: '''{req -> "<goodResponseRest-${req.xml.name()}/>"}''',
|
||||
|
@ -55,7 +55,7 @@ class MockServerIntegrationTest extends Specification {
|
|||
expect:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testSoap',
|
||||
path: '/testEndpoint',
|
||||
path: 'testEndpoint',
|
||||
port: 9999,
|
||||
predicate: '''{req -> req.soap.name() == 'request'}''',
|
||||
response: '''{req -> "<goodResponseSoap-${req.soap.name()}/>"}''',
|
||||
|
@ -81,7 +81,7 @@ class MockServerIntegrationTest extends Specification {
|
|||
expect:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testSoap',
|
||||
path: '/testEndpoint',
|
||||
path: 'testEndpoint',
|
||||
port: 9999,
|
||||
predicate: '''{req -> req.xml.name() == 'request'}''',
|
||||
response: '''{req -> "<goodResponseSoap-${req.xml.name()}/>"}''',
|
||||
|
@ -99,7 +99,7 @@ class MockServerIntegrationTest extends Specification {
|
|||
expect:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testSoap',
|
||||
path: '/testEndpoint',
|
||||
path: 'testEndpoint',
|
||||
port: 9999,
|
||||
predicate: '''{req -> req.xml.name() == 'request'}''',
|
||||
response: '''{req -> "<goodResponseSoap-${req.xml.name()}/>"}''',
|
||||
|
@ -108,7 +108,7 @@ class MockServerIntegrationTest extends Specification {
|
|||
when:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testSoap',
|
||||
path: '/testEndpoint2',
|
||||
path: 'testEndpoint2',
|
||||
port: 9998,
|
||||
predicate: '''{req -> req.xml.name() == 'request'}''',
|
||||
response: '''{req -> "<goodResponseSoap-${req.xml.name()}/>"}''',
|
||||
|
@ -118,11 +118,25 @@ class MockServerIntegrationTest extends Specification {
|
|||
thrown(MockAlreadyExists)
|
||||
}
|
||||
|
||||
def "should not add mock with empty name"() {
|
||||
when:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: '',
|
||||
path: 'testEndpoint2',
|
||||
port: 9998,
|
||||
predicate: '''{req -> req.xml.name() == 'request'}''',
|
||||
response: '''{req -> "<goodResponseSoap-${req.xml.name()}/>"}''',
|
||||
soap: true
|
||||
))
|
||||
then:
|
||||
thrown(InvalidMockDefinition)
|
||||
}
|
||||
|
||||
def "should add mock after deleting old mock with the same name"() {
|
||||
expect:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testSoap',
|
||||
path: '/testEndpoint',
|
||||
path: 'testEndpoint',
|
||||
port: 9999,
|
||||
predicate: '''{req -> req.xml.name() == 'request'}''',
|
||||
response: '''{req -> "<goodResponseSoap-${req.name()}/>"}''',
|
||||
|
@ -133,7 +147,7 @@ class MockServerIntegrationTest extends Specification {
|
|||
and:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testSoap',
|
||||
path: '/testEndpoint',
|
||||
path: 'testEndpoint',
|
||||
port: 9999,
|
||||
predicate: '''{req -> req.xml.name() == 'request2'}''',
|
||||
response: '''{req -> "<goodResponseSoap2-${req.xml.name()}/>"}''',
|
||||
|
@ -145,14 +159,14 @@ class MockServerIntegrationTest extends Specification {
|
|||
given:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest',
|
||||
path: '/testEndpoint',
|
||||
path: 'testEndpoint',
|
||||
port: 9999,
|
||||
predicate: '''{req -> req.xml.name() == 'request'}''',
|
||||
response: '''{req -> "<goodResponseRest-${req.xml.name()}/>"}'''
|
||||
))
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testSoap',
|
||||
path: '/testEndpoint',
|
||||
path: 'testEndpoint',
|
||||
port: 9999,
|
||||
predicate: '''{req -> req.soap.name() == 'request'}''',
|
||||
response: '''{req -> "<goodResponseSoap-${req.soap.name()}/>"}''',
|
||||
|
@ -180,7 +194,7 @@ class MockServerIntegrationTest extends Specification {
|
|||
given:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest1',
|
||||
path: '/test1',
|
||||
path: 'test1',
|
||||
port: 9999,
|
||||
predicate: '''{req -> req.xml.name() == 'request1'}''',
|
||||
response: '''{req -> "<goodResponseRest1/>"}'''
|
||||
|
@ -194,7 +208,7 @@ class MockServerIntegrationTest extends Specification {
|
|||
))
|
||||
HttpPost firstRequest = new HttpPost('http://localhost:9999/test1')
|
||||
firstRequest.entity = new StringEntity('<request1/>', ContentType.create("text/xml", "UTF-8"))
|
||||
HttpPost secondRequest = new HttpPost("http://localhost:${secondPort}${secondPath}")
|
||||
HttpPost secondRequest = new HttpPost("http://localhost:${secondPort}/${secondPath}")
|
||||
secondRequest.entity = new StringEntity('<request2/>', ContentType.create("text/xml", "UTF-8"))
|
||||
when:
|
||||
CloseableHttpResponse firstResponse = client.execute(firstRequest)
|
||||
|
@ -208,10 +222,10 @@ class MockServerIntegrationTest extends Specification {
|
|||
secondXmlResponse.name() == 'goodResponseRest2'
|
||||
where:
|
||||
secondPort | secondPath | name
|
||||
9999 | '/test1' | 'the same port and path'
|
||||
9998 | '/test1' | 'the same path and another port'
|
||||
9999 | '/test2' | 'the same port and another path'
|
||||
9998 | '/test2' | 'another port and path'
|
||||
9999 | 'test1' | 'the same port and path'
|
||||
9998 | 'test1' | 'the same path and another port'
|
||||
9999 | 'test2' | 'the same port and another path'
|
||||
9998 | 'test2' | 'another port and path'
|
||||
}
|
||||
|
||||
@Unroll
|
||||
|
@ -219,7 +233,7 @@ class MockServerIntegrationTest extends Specification {
|
|||
given:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest1',
|
||||
path: '/test1',
|
||||
path: 'test1',
|
||||
port: 9999,
|
||||
statusCode: statusCode
|
||||
))
|
||||
|
@ -241,7 +255,7 @@ class MockServerIntegrationTest extends Specification {
|
|||
given:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest1',
|
||||
path: '/test1',
|
||||
path: 'test1',
|
||||
port: 9999,
|
||||
predicate: '''{req -> req.xml.name() == 'request2'}''',
|
||||
response: '''{req -> "<goodResponseRest2/>"}'''
|
||||
|
@ -260,27 +274,27 @@ class MockServerIntegrationTest extends Specification {
|
|||
when:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testSoap',
|
||||
path: '/testEndpoint2',
|
||||
path: 'testEndpoint2',
|
||||
port: -1,
|
||||
predicate: '''{_ -> true}''',
|
||||
response: '''{req -> "<goodResponseSoap-${req.xml.name()}/>"}''',
|
||||
soap: true
|
||||
))
|
||||
then:
|
||||
thrown(InvalidMockDefinitionException)
|
||||
thrown(InvalidMockDefinition)
|
||||
}
|
||||
|
||||
def "should dispatch rest mock with get method"() {
|
||||
given:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest',
|
||||
path: '/testEndpoint',
|
||||
path: 'testEndpoint',
|
||||
port: 9999,
|
||||
response: '''{_ -> "<defaultResponse/>"}'''
|
||||
))
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest2',
|
||||
path: '/testEndpoint',
|
||||
path: 'testEndpoint',
|
||||
port: 9999,
|
||||
response: '''{_ -> "<getResponse/>"}''',
|
||||
method: AddMockRequestData.Method.GET
|
||||
|
@ -297,13 +311,13 @@ class MockServerIntegrationTest extends Specification {
|
|||
given:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest',
|
||||
path: '/testEndpoint',
|
||||
path: 'testEndpoint',
|
||||
port: 9999,
|
||||
response: '''{_ -> "<defaultResponse/>"}'''
|
||||
))
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest2',
|
||||
path: '/testEndpoint',
|
||||
path: 'testEndpoint',
|
||||
port: 9999,
|
||||
response: '''{_ -> "<traceResponse/>"}''',
|
||||
method: AddMockRequestData.Method.TRACE
|
||||
|
@ -320,13 +334,13 @@ class MockServerIntegrationTest extends Specification {
|
|||
given:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest',
|
||||
path: '/testEndpoint',
|
||||
path: 'testEndpoint',
|
||||
port: 9999,
|
||||
response: '''{_ -> "<defaultResponse/>"}'''
|
||||
))
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest2',
|
||||
path: '/testEndpoint',
|
||||
path: 'testEndpoint',
|
||||
port: 9999,
|
||||
method: AddMockRequestData.Method.HEAD
|
||||
))
|
||||
|
@ -342,13 +356,13 @@ class MockServerIntegrationTest extends Specification {
|
|||
given:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest',
|
||||
path: '/testEndpoint',
|
||||
path: 'testEndpoint',
|
||||
port: 9999,
|
||||
response: '''{_ -> "<defaultResponse/>"}'''
|
||||
))
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest2',
|
||||
path: '/testEndpoint',
|
||||
path: 'testEndpoint',
|
||||
port: 9999,
|
||||
method: AddMockRequestData.Method.OPTIONS
|
||||
))
|
||||
|
@ -364,13 +378,13 @@ class MockServerIntegrationTest extends Specification {
|
|||
given:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest',
|
||||
path: '/test1',
|
||||
path: 'test1',
|
||||
port: 9999,
|
||||
response: '''{_ -> "<defaultResponse/>"}'''
|
||||
))
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest2',
|
||||
path: '/test1',
|
||||
path: 'test1',
|
||||
port: 9999,
|
||||
predicate: '''{req -> req.xml.name() == 'request1'}''',
|
||||
response: '''{_ -> "<goodResponseRest1/>"}''',
|
||||
|
@ -389,13 +403,13 @@ class MockServerIntegrationTest extends Specification {
|
|||
given:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest',
|
||||
path: '/test1',
|
||||
path: 'test1',
|
||||
port: 9999,
|
||||
response: '''{_ -> "<defaultResponse/>"}'''
|
||||
))
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest2',
|
||||
path: '/test1',
|
||||
path: 'test1',
|
||||
port: 9999,
|
||||
response: '''{_ -> "<goodResponseRest1/>"}''',
|
||||
method: AddMockRequestData.Method.DELETE
|
||||
|
@ -412,13 +426,13 @@ class MockServerIntegrationTest extends Specification {
|
|||
given:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest',
|
||||
path: '/test1',
|
||||
path: 'test1',
|
||||
port: 9999,
|
||||
response: '''{_ -> "<defaultResponse/>"}'''
|
||||
))
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest2',
|
||||
path: '/test1',
|
||||
path: 'test1',
|
||||
port: 9999,
|
||||
predicate: '''{req -> req.xml.name() == 'request1'}''',
|
||||
response: '''{_ -> "<goodResponseRest1/>"}''',
|
||||
|
@ -437,7 +451,7 @@ class MockServerIntegrationTest extends Specification {
|
|||
given:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest',
|
||||
path: '/testEndpoint',
|
||||
path: 'testEndpoint',
|
||||
port: 9999,
|
||||
predicate: '''{req -> req.xml.name() == 'request'}''',
|
||||
response: '''{_ -> "<goodResponse/>"}''',
|
||||
|
@ -457,7 +471,7 @@ class MockServerIntegrationTest extends Specification {
|
|||
given:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest',
|
||||
path: '/testEndpoint',
|
||||
path: 'testEndpoint',
|
||||
port: 9999,
|
||||
predicate: '''{ req -> req.headers['user-agent']?.startsWith('Mozilla') &&
|
||||
req.headers.pragma == 'no-cache'}''',
|
||||
|
@ -486,7 +500,7 @@ class MockServerIntegrationTest extends Specification {
|
|||
given:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest',
|
||||
path: '/testEndpoint',
|
||||
path: 'testEndpoint',
|
||||
port: 9999,
|
||||
predicate: '''{ req -> req.query['q'] == '15' &&
|
||||
req.query.id == '1'}''',
|
||||
|
@ -510,7 +524,7 @@ class MockServerIntegrationTest extends Specification {
|
|||
given:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest',
|
||||
path: '/testEndpoint',
|
||||
path: 'testEndpoint',
|
||||
port: 9999,
|
||||
predicate: '''{req -> req.text == 'hello=world&id=3'}''',
|
||||
response: '''{_ -> "<goodResponse/>"}'''
|
||||
|
@ -535,7 +549,7 @@ class MockServerIntegrationTest extends Specification {
|
|||
given:
|
||||
controlServerClient.addMock(new AddMockRequestData(
|
||||
name: 'testRest',
|
||||
path: '/testEndpoint',
|
||||
path: 'testEndpoint',
|
||||
port: 9999,
|
||||
predicate: '''{req -> req.json.id == 1 && req.json.ar == ["a", true]}''',
|
||||
response: '''{req -> """{"name":"goodResponse-${req.json.id}"}"""}'''
|
||||
|
@ -558,5 +572,4 @@ class MockServerIntegrationTest extends Specification {
|
|||
|
||||
//TODO def "should get mock report"(){}
|
||||
//TODO def "should get list mocks"(){}
|
||||
//TODO def "should validate mock when creating"
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue