From f8e0cc44f99e9b93d6823f55ea4a79b46d9c751f Mon Sep 17 00:00:00 2001 From: piotrekfus91 Date: Sun, 28 Jan 2018 18:30:50 +0100 Subject: [PATCH] Handle leading slash (#1) * Handle leading slash --- .gitignore | 2 ++ .../tests/MockServerIntegrationTest.groovy | 29 +++++++++++++++++++ .../pl/touk/mockserver/server/Mock.groovy | 10 ++++++- 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7f995f3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.iml +target/ diff --git a/mockserver-tests/src/test/groovy/pl/touk/mockserver/tests/MockServerIntegrationTest.groovy b/mockserver-tests/src/test/groovy/pl/touk/mockserver/tests/MockServerIntegrationTest.groovy index e8f4d87..ab16248 100644 --- a/mockserver-tests/src/test/groovy/pl/touk/mockserver/tests/MockServerIntegrationTest.groovy +++ b/mockserver-tests/src/test/groovy/pl/touk/mockserver/tests/MockServerIntegrationTest.groovy @@ -1133,4 +1133,33 @@ class MockServerIntegrationTest extends Specification { remoteMockServer.removeMock('testRest')?.size() == 1 } + @Unroll + def 'should handle leading slash'() { + given: + String name = "testRest-${UUID.randomUUID().toString()}" + expect: + remoteMockServer.addMock(new AddMock( + name: name, + path: mockPath, + port: 9999, + statusCode: 201, + soap: false + )) + when: + HttpPost restPost = new HttpPost("http://localhost:9999/$urlPath") + CloseableHttpResponse response = client.execute(restPost) + then: + response.statusLine.statusCode == 201 + Util.consumeResponse(response) + expect: + remoteMockServer.removeMock(name)?.size() == 1 + where: + mockPath | urlPath + '' | '' + '/' | '' + 'test' | 'test' + '/test' | 'test' + 'test/other' | 'test/other' + '/test/other' | 'test/other' + } } diff --git a/mockserver/src/main/groovy/pl/touk/mockserver/server/Mock.groovy b/mockserver/src/main/groovy/pl/touk/mockserver/server/Mock.groovy index 83c3c7e..ed37f89 100644 --- a/mockserver/src/main/groovy/pl/touk/mockserver/server/Mock.groovy +++ b/mockserver/src/main/groovy/pl/touk/mockserver/server/Mock.groovy @@ -41,10 +41,18 @@ class Mock implements Comparable { throw new RuntimeException("Mock name must be given") } this.name = name - this.path = path + this.path = stripLeadingSlash(path) this.port = port } + private static String stripLeadingSlash(String path) { + if (path?.startsWith('/')) { + return path - '/' + } else { + return path + } + } + boolean match(Method method, MockRequest request) { return this.method == method && predicate(request) }