Skip to content

Commit dc45304

Browse files
committed
Fixed test and fixed a bug
The test UploadResponseHandlerTest#getUploadSessionOnProgressingUpload mimicked a server response with a 200/OK response code. In the REST reference (https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession?view=graph-rest-1.0#upload-bytes-to-the-upload-session) it is stated that the response code for uploading more bytes to the upload session is 202/Accepted. The bug in #1517 is fixed. Previously the method UploadResponseHandler#handleResponse handled differently the response in these two cases: - response code 200/OK - all other cases Now the same method handles differently the response in these two cases: - response code 200/OK or 201/Created - all other cases Everything works if the REST reference is corrected and: - 200/OK is returned when an item is updated, 201/Created is returned when an item is Created - 202/Accepted is returned when more bytes are requested
1 parent 2c168c7 commit dc45304

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

src/main/java/com/microsoft/graph/core/requests/upload/UploadResponseHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,16 @@ public <T extends Parsable> UploadResult<T> handleResponse(@Nonnull final Respon
5656
}
5757
try(final InputStream in = body.byteStream()){
5858
final String contentType = body.contentType().toString().split(";")[0]; //contentType.toString() returns in format <mediaType>;<charset>, we only want the mediaType.
59+
int responseCode = response.code();
5960
if(!response.isSuccessful()) {
6061
throw new ApiExceptionBuilder()
6162
.withMessage(ErrorConstants.Codes.GENERAL_EXCEPTION)
62-
.withResponseStatusCode(response.code())
63+
.withResponseStatusCode(responseCode)
6364
.withResponseHeaders(HeadersCompatibility.getResponseHeaders(response.headers()))
6465
.build();
6566
}
6667
UploadResult<T> uploadResult = new UploadResult<>();
67-
if (response.code() == HttpURLConnection.HTTP_CREATED) {
68+
if (responseCode == HttpURLConnection.HTTP_CREATED || responseCode == HttpURLConnection.HTTP_OK) {
6869
if (body.contentLength() > 0) {
6970
final ParseNode uploadTypeParseNode = parseNodeFactory.getParseNode(contentType, in);
7071
uploadResult.itemResponse = uploadTypeParseNode.getObjectValue(factory);

src/test/java/com/microsoft/graph/core/requests/upload/UploadResponseHandlerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ void getUploadSessionOnProgressingUpload() {
119119
Response response = new Response.Builder()
120120
.request(mock(Request.class))
121121
.protocol(mock(Protocol.class))
122-
.message("OK")
122+
.message("Accepted")
123123
.body(body)
124-
.code(HttpURLConnection.HTTP_OK)
124+
.code(HttpURLConnection.HTTP_ACCEPTED)
125125
.build();
126126
UploadResult<TestDriveItem> result = responseHandler
127127
.handleResponse(response, TestDriveItem::createFromDiscriminatorValue);
@@ -135,7 +135,7 @@ void getUploadSessionOnProgressingUpload() {
135135
assertEquals("77829-99375", session.getNextExpectedRanges().get(1));
136136
assertEquals(2, session.getNextExpectedRanges().size());
137137
}
138-
138+
139139
@Test
140140
void throwsServiceExceptionOnErrorResponse() {
141141
UploadResponseHandler responseHandler = new UploadResponseHandler(null);

0 commit comments

Comments
 (0)