Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow modifying BoxAPIRequest URL #1236

Merged
merged 2 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/main/java/com/box/sdk/BoxAPIRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class BoxAPIRequest {
private final BoxAPIConnection api;
private final List<RequestHeader> headers;
private final String method;
private final URL url;
private URL url;
private BackoffCounter backoffCounter;
private int connectTimeout;
private int readTimeout;
Expand Down Expand Up @@ -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.
*
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/com/box/sdk/BoxAPIRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
lukaszsocha2 marked this conversation as resolved.
Show resolved Hide resolved
verify(0, getRequestedFor(urlEqualTo(initialPath)));
}

@Test
public void addsAuthenticationHeaderByDefault() {
stubFor(get(urlEqualTo("/"))
Expand Down Expand Up @@ -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);
}
}
}
Loading