Skip to content

Commit e7d28bd

Browse files
mhagmajerantusus
andauthored
fix: class cast exception when uploading large file (#1174)
Closes #1173 Co-authored-by: Kamil Berdychowski <kberdychowski@box.com>
1 parent 4d1616d commit e7d28bd

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public class BoxAPIResponse implements Closeable {
4343
private final String contentType;
4444
private final String requestMethod;
4545
private final String requestUrl;
46-
4746
private int responseCode;
4847
private String bodyString;
4948

@@ -320,4 +319,12 @@ private void logErrorResponse(int responseCode) {
320319
LOGGER.error(this.toString());
321320
}
322321
}
322+
323+
protected String getRequestMethod() {
324+
return requestMethod;
325+
}
326+
327+
protected String getRequestUrl() {
328+
return requestUrl;
329+
}
323330
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,20 @@ public JsonValue getBodyAsJsonValue() {
9898

9999
@Override
100100
public BoxJSONResponse send() {
101-
return (BoxJSONResponse) super.send();
101+
return convert(super.send());
102102
}
103103

104104
@Override
105105
public BoxJSONResponse send(ProgressListener listener) {
106-
return (BoxJSONResponse) super.send(listener);
106+
return convert(super.send(listener));
107+
}
108+
109+
private BoxJSONResponse convert(BoxAPIResponse response) {
110+
if (response instanceof BoxJSONResponse) {
111+
return (BoxJSONResponse) response;
112+
} else {
113+
return new BoxJSONResponse(response);
114+
}
107115
}
108116

109117
@Override

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ public BoxJSONResponse() {
2525
super();
2626
}
2727

28+
BoxJSONResponse(BoxAPIResponse response) {
29+
this(
30+
response.getResponseCode(),
31+
response.getRequestMethod(),
32+
response.getRequestUrl(),
33+
response.getHeaders(),
34+
new JsonObject()
35+
);
36+
}
37+
2838
/**
2939
* Constructs a BoxAPIResponse with an http response code and response body.
3040
*
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.box.sdk;
2+
3+
import static com.box.sdk.TestUtils.createConnectionWith;
4+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
5+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
6+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
7+
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
8+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
9+
import static java.lang.String.format;
10+
import static org.hamcrest.MatcherAssert.assertThat;
11+
import static org.hamcrest.core.Is.is;
12+
import static org.mockito.Mockito.mock;
13+
14+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
15+
import java.net.MalformedURLException;
16+
import java.net.URL;
17+
import org.junit.Rule;
18+
import org.junit.Test;
19+
20+
public class BoxJSONRequestTest {
21+
@Rule
22+
public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicHttpsPort().httpDisabled(true));
23+
24+
@Test
25+
public void shouldHandleApiResponse() {
26+
stubFor(get(urlEqualTo("/")).willReturn(aResponse().withStatus(202)));
27+
Time mockTime = mock(Time.class);
28+
BackoffCounter backoffCounter = new BackoffCounter(mockTime);
29+
30+
BoxAPIConnection api = createConnectionWith(boxMockUrl().toString());
31+
BoxJSONRequest request = new BoxJSONRequest(api, boxMockUrl(), "GET");
32+
request.setBackoffCounter(backoffCounter);
33+
34+
BoxJSONResponse response = request.send();
35+
assertThat(response.getResponseCode(), is(202));
36+
}
37+
38+
private URL boxMockUrl() {
39+
try {
40+
return new URL(format("https://localhost:%d/", wireMockRule.httpsPort()));
41+
} catch (MalformedURLException e) {
42+
throw new RuntimeException(e);
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)