Skip to content

Commit eaea019

Browse files
authored
feat: allow modifying BoxAPIRequest URL (#1236)
Closes: SDK-3638
1 parent 5545a34 commit eaea019

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/main/java/com/box/sdk/BoxAPIRequest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class BoxAPIRequest {
4949
private final BoxAPIConnection api;
5050
private final List<RequestHeader> headers;
5151
private final String method;
52-
private final URL url;
52+
private URL url;
5353
private BackoffCounter backoffCounter;
5454
private int connectTimeout;
5555
private int readTimeout;
@@ -291,6 +291,14 @@ public URL getUrl() {
291291
return this.url;
292292
}
293293

294+
/**
295+
* Sets the URL to the request.
296+
*
297+
*/
298+
public void setUrl(URL url) {
299+
this.url = url;
300+
}
301+
294302
/**
295303
* Gets the http method from the request.
296304
*

src/test/java/com/box/sdk/BoxAPIRequestTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,27 @@ public void interceptorCanModifyRequest() {
287287
verify(1, getRequestedFor(urlEqualTo("/")));
288288
}
289289

290+
@Test
291+
public void interceptorCanModifyRequestURL() {
292+
String initialPath = "/";
293+
String modifiedPath = "/2";
294+
295+
stubFor(get(urlEqualTo(initialPath))
296+
.willReturn(aResponse().withStatus(404)));
297+
stubFor(get(urlEqualTo(modifiedPath))
298+
.willReturn(aResponse().withStatus(200).withBody("{\"foo\":\"bar\"}")));
299+
300+
BoxAPIConnection api = createConnectionWith(boxMockUrl().toString());
301+
api.setRequestInterceptor(request -> {
302+
request.setUrl(boxMockUrl(modifiedPath));
303+
return null;
304+
});
305+
306+
new BoxAPIRequest(api, boxMockUrl(), "GET").send();
307+
verify(1, getRequestedFor(urlEqualTo(modifiedPath)));
308+
verify(0, getRequestedFor(urlEqualTo(initialPath)));
309+
}
310+
290311
@Test
291312
public void addsAuthenticationHeaderByDefault() {
292313
stubFor(get(urlEqualTo("/"))
@@ -334,4 +355,12 @@ private URL boxMockUrl() {
334355
throw new RuntimeException(e);
335356
}
336357
}
358+
359+
private URL boxMockUrl(String path) {
360+
try {
361+
return new URL(format("https://localhost:%d%s", wireMockRule.httpsPort(), path));
362+
} catch (MalformedURLException e) {
363+
throw new RuntimeException(e);
364+
}
365+
}
337366
}

0 commit comments

Comments
 (0)