Skip to content

Commit

Permalink
fix: class cast exception when uploading large file (#1174)
Browse files Browse the repository at this point in the history
Closes #1173
Co-authored-by: Kamil Berdychowski <[email protected]>
  • Loading branch information
mhagmajer authored Jun 13, 2023
1 parent 4d1616d commit e7d28bd
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/main/java/com/box/sdk/BoxAPIResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public class BoxAPIResponse implements Closeable {
private final String contentType;
private final String requestMethod;
private final String requestUrl;

private int responseCode;
private String bodyString;

Expand Down Expand Up @@ -320,4 +319,12 @@ private void logErrorResponse(int responseCode) {
LOGGER.error(this.toString());
}
}

protected String getRequestMethod() {
return requestMethod;
}

protected String getRequestUrl() {
return requestUrl;
}
}
12 changes: 10 additions & 2 deletions src/main/java/com/box/sdk/BoxJSONRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,20 @@ public JsonValue getBodyAsJsonValue() {

@Override
public BoxJSONResponse send() {
return (BoxJSONResponse) super.send();
return convert(super.send());
}

@Override
public BoxJSONResponse send(ProgressListener listener) {
return (BoxJSONResponse) super.send(listener);
return convert(super.send(listener));
}

private BoxJSONResponse convert(BoxAPIResponse response) {
if (response instanceof BoxJSONResponse) {
return (BoxJSONResponse) response;
} else {
return new BoxJSONResponse(response);
}
}

@Override
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/box/sdk/BoxJSONResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ public BoxJSONResponse() {
super();
}

BoxJSONResponse(BoxAPIResponse response) {
this(
response.getResponseCode(),
response.getRequestMethod(),
response.getRequestUrl(),
response.getHeaders(),
new JsonObject()
);
}

/**
* Constructs a BoxAPIResponse with an http response code and response body.
*
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/com/box/sdk/BoxJSONRequestTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.box.sdk;

import static com.box.sdk.TestUtils.createConnectionWith;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static java.lang.String.format;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.mockito.Mockito.mock;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.Rule;
import org.junit.Test;

public class BoxJSONRequestTest {
@Rule
public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicHttpsPort().httpDisabled(true));

@Test
public void shouldHandleApiResponse() {
stubFor(get(urlEqualTo("/")).willReturn(aResponse().withStatus(202)));
Time mockTime = mock(Time.class);
BackoffCounter backoffCounter = new BackoffCounter(mockTime);

BoxAPIConnection api = createConnectionWith(boxMockUrl().toString());
BoxJSONRequest request = new BoxJSONRequest(api, boxMockUrl(), "GET");
request.setBackoffCounter(backoffCounter);

BoxJSONResponse response = request.send();
assertThat(response.getResponseCode(), is(202));
}

private URL boxMockUrl() {
try {
return new URL(format("https://localhost:%d/", wireMockRule.httpsPort()));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit e7d28bd

Please sign in to comment.