diff --git a/src/main/groovy/pl/touk/mockserver/server/ContextExecutor.groovy b/src/main/groovy/pl/touk/mockserver/server/ContextExecutor.groovy index 1297ae4..c62981e 100644 --- a/src/main/groovy/pl/touk/mockserver/server/ContextExecutor.groovy +++ b/src/main/groovy/pl/touk/mockserver/server/ContextExecutor.groovy @@ -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(){ + } } diff --git a/src/main/groovy/pl/touk/mockserver/server/HttpServerWraper.groovy b/src/main/groovy/pl/touk/mockserver/server/HttpServerWraper.groovy index 684825e..c685e15 100644 --- a/src/main/groovy/pl/touk/mockserver/server/HttpServerWraper.groovy +++ b/src/main/groovy/pl/touk/mockserver/server/HttpServerWraper.groovy @@ -38,7 +38,7 @@ class HttpServerWraper { } void stop() { - executors.each { httpServer.removeContext(it.path) } + executors.each { httpServer.removeContext(it.contextPath) } httpServer.stop(0) } diff --git a/src/main/groovy/pl/touk/mockserver/server/Mock.groovy b/src/main/groovy/pl/touk/mockserver/server/Mock.groovy index e86097b..eb3ea54 100644 --- a/src/main/groovy/pl/touk/mockserver/server/Mock.groovy +++ b/src/main/groovy/pl/touk/mockserver/server/Mock.groovy @@ -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 { """ } - 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 } diff --git a/src/test/groovy/pl/touk/mockserver/client/ControlServerClient.groovy b/src/test/groovy/pl/touk/mockserver/client/ControlServerClient.groovy index 4e6f021..e5c0bb5 100644 --- a/src/test/groovy/pl/touk/mockserver/client/ControlServerClient.groovy +++ b/src/test/groovy/pl/touk/mockserver/client/ControlServerClient.groovy @@ -26,7 +26,7 @@ class ControlServerClient { throw new MockAlreadyExists() } - throw new InvalidMockDefinitionException(responseXml.text()) + throw new InvalidMockDefinition(responseXml.text()) } } diff --git a/src/test/groovy/pl/touk/mockserver/client/InvalidMockDefinitionException.groovy b/src/test/groovy/pl/touk/mockserver/client/InvalidMockDefinition.groovy similarity index 59% rename from src/test/groovy/pl/touk/mockserver/client/InvalidMockDefinitionException.groovy rename to src/test/groovy/pl/touk/mockserver/client/InvalidMockDefinition.groovy index 769a435..f95b584 100644 --- a/src/test/groovy/pl/touk/mockserver/client/InvalidMockDefinitionException.groovy +++ b/src/test/groovy/pl/touk/mockserver/client/InvalidMockDefinition.groovy @@ -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) } } diff --git a/src/test/groovy/pl/touk/mockserver/server/MockServerIntegrationTest.groovy b/src/test/groovy/pl/touk/mockserver/server/MockServerIntegrationTest.groovy index c6ebfac..5773fb0 100644 --- a/src/test/groovy/pl/touk/mockserver/server/MockServerIntegrationTest.groovy +++ b/src/test/groovy/pl/touk/mockserver/server/MockServerIntegrationTest.groovy @@ -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 -> ""}''', @@ -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 -> ""}''', @@ -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 -> ""}''', @@ -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 -> ""}''', @@ -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 -> ""}''', @@ -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 -> ""}''', + 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 -> ""}''', @@ -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 -> ""}''', @@ -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 -> ""}''' )) controlServerClient.addMock(new AddMockRequestData( name: 'testSoap', - path: '/testEndpoint', + path: 'testEndpoint', port: 9999, predicate: '''{req -> req.soap.name() == 'request'}''', response: '''{req -> ""}''', @@ -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 -> ""}''' @@ -194,7 +208,7 @@ class MockServerIntegrationTest extends Specification { )) HttpPost firstRequest = new HttpPost('http://localhost:9999/test1') firstRequest.entity = new StringEntity('', 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('', 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 -> ""}''' @@ -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 -> ""}''', 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: '''{_ -> ""}''' )) controlServerClient.addMock(new AddMockRequestData( name: 'testRest2', - path: '/testEndpoint', + path: 'testEndpoint', port: 9999, response: '''{_ -> ""}''', 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: '''{_ -> ""}''' )) controlServerClient.addMock(new AddMockRequestData( name: 'testRest2', - path: '/testEndpoint', + path: 'testEndpoint', port: 9999, response: '''{_ -> ""}''', 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: '''{_ -> ""}''' )) 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: '''{_ -> ""}''' )) 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: '''{_ -> ""}''' )) controlServerClient.addMock(new AddMockRequestData( name: 'testRest2', - path: '/test1', + path: 'test1', port: 9999, predicate: '''{req -> req.xml.name() == 'request1'}''', response: '''{_ -> ""}''', @@ -389,13 +403,13 @@ class MockServerIntegrationTest extends Specification { given: controlServerClient.addMock(new AddMockRequestData( name: 'testRest', - path: '/test1', + path: 'test1', port: 9999, response: '''{_ -> ""}''' )) controlServerClient.addMock(new AddMockRequestData( name: 'testRest2', - path: '/test1', + path: 'test1', port: 9999, response: '''{_ -> ""}''', 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: '''{_ -> ""}''' )) controlServerClient.addMock(new AddMockRequestData( name: 'testRest2', - path: '/test1', + path: 'test1', port: 9999, predicate: '''{req -> req.xml.name() == 'request1'}''', response: '''{_ -> ""}''', @@ -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: '''{_ -> ""}''', @@ -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: '''{_ -> ""}''' @@ -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" }