Fixed relative URL computation in wIthoutBaseUrl (#1785)

* Fixed relative URL computation in wIthoutBaseUrl

Current behaviour :
* base URL = http://foo.bar
* url = http://foo.bar/my/api
gives => my/api

Fixed behaviour : 
* base URL = http://foo.bar
* url = http://foo.bar/my/api
gives => /my/api

* Fixed & Added urlPathMatching related tests

Fixed false positive test getWithUrlPathMatching() (now fails without commit f9c9527f89e36ba5b4f5b00fc5895c8df78062e2 and pass after), and added 2 tests to ensure leading slash in urlPathPattern or trailing slash in base URL works fine.

Co-authored-by: Rudy Nappee <rudy.nappee@externe.maif.fr>
This commit is contained in:
devdufutur
2022-05-18 09:37:58 +02:00
committed by GitHub
parent b77ae2ca3e
commit e59725326a
5 changed files with 40 additions and 2 deletions

View File

@@ -277,7 +277,7 @@ public final class WireMockRestServiceServer {
if (indexOfBaseUrl == -1) {
return url;
}
return url.substring(indexOfBaseUrl + this.baseUrl.length() + 1);
return url.substring(indexOfBaseUrl + this.baseUrl.length());
}
private Matcher<String> requestMatcher(RequestPattern request) {

View File

@@ -411,6 +411,24 @@ public class WiremockMockServerApplicationTests {
server.verify();
}
@Test
public void getWithSimpleUrlPathMatchingWithTrailingSlashInBaseUrl() throws Exception {
MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate).baseUrl("https://example.org/")
.stubs("classpath:/mappings/url-simple-path-pattern-without-leading-slash.json").build();
assertThat(this.restTemplate.getForObject("https://example.org/my/api", String.class))
.isEqualTo("Hello Url Path Matcher");
server.verify();
}
@Test
public void getWithSimpleUrlPathMatchingWithLeadingSlashInPattern() throws Exception {
MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate).baseUrl("https://example.org")
.stubs("classpath:/mappings/url-simple-path-pattern-with-leading-slash.json").build();
assertThat(this.restTemplate.getForObject("https://example.org/my/api", String.class))
.isEqualTo("Hello Url Path Matcher");
server.verify();
}
@Test
public void getWithUrlMatching() throws Exception {
MockRestServiceServer server = WireMockRestServiceServer.with(this.restTemplate) //

View File

@@ -1,7 +1,7 @@
{
"request": {
"method": "GET",
"urlPathPattern": "([a-zA-Z0-9/-]*)/url-path-pattern/"
"urlPathPattern": "/([0-9]+)/url-path-pattern/"
},
"response": {
"status": 200,

View File

@@ -0,0 +1,10 @@
{
"request": {
"method": "GET",
"urlPathPattern": "/my/api"
},
"response": {
"status": 200,
"body": "Hello Url Path Matcher"
}
}

View File

@@ -0,0 +1,10 @@
{
"request": {
"method": "GET",
"urlPathPattern": "my/api"
},
"response": {
"status": 200,
"body": "Hello Url Path Matcher"
}
}