diff --git a/src/main/java/com/box/sdk/BoxAPIRequest.java b/src/main/java/com/box/sdk/BoxAPIRequest.java index c5aaae13d..924572d7c 100644 --- a/src/main/java/com/box/sdk/BoxAPIRequest.java +++ b/src/main/java/com/box/sdk/BoxAPIRequest.java @@ -49,7 +49,7 @@ public class BoxAPIRequest { private final BoxAPIConnection api; private final List headers; private final String method; - private final URL url; + private URL url; private BackoffCounter backoffCounter; private int connectTimeout; private int readTimeout; @@ -291,6 +291,14 @@ public URL getUrl() { return this.url; } + /** + * Sets the URL to the request. + * + */ + public void setUrl(URL url) { + this.url = url; + } + /** * Gets the http method from the request. * diff --git a/src/test/java/com/box/sdk/BoxAPIRequestTest.java b/src/test/java/com/box/sdk/BoxAPIRequestTest.java index 30c424612..3d77f9fde 100644 --- a/src/test/java/com/box/sdk/BoxAPIRequestTest.java +++ b/src/test/java/com/box/sdk/BoxAPIRequestTest.java @@ -287,6 +287,27 @@ public void interceptorCanModifyRequest() { verify(1, getRequestedFor(urlEqualTo("/"))); } + @Test + public void interceptorCanModifyRequestURL() { + String initialPath = "/"; + String modifiedPath = "/2"; + + stubFor(get(urlEqualTo(initialPath)) + .willReturn(aResponse().withStatus(404))); + stubFor(get(urlEqualTo(modifiedPath)) + .willReturn(aResponse().withStatus(200).withBody("{\"foo\":\"bar\"}"))); + + BoxAPIConnection api = createConnectionWith(boxMockUrl().toString()); + api.setRequestInterceptor(request -> { + request.setUrl(boxMockUrl(modifiedPath)); + return null; + }); + + new BoxAPIRequest(api, boxMockUrl(), "GET").send(); + verify(1, getRequestedFor(urlEqualTo(modifiedPath))); + verify(0, getRequestedFor(urlEqualTo(initialPath))); + } + @Test public void addsAuthenticationHeaderByDefault() { stubFor(get(urlEqualTo("/")) @@ -334,4 +355,12 @@ private URL boxMockUrl() { throw new RuntimeException(e); } } + + private URL boxMockUrl(String path) { + try { + return new URL(format("https://localhost:%d%s", wireMockRule.httpsPort(), path)); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } + } }