Skip to content

Commit ecaa5be

Browse files
committed
Fix mock handler headers for resumable upload
1 parent a832dba commit ecaa5be

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

test/fixtures/gcs-fixture/src/main/java/fixture/gcs/GoogleCloudStorageHttpHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public void handle(final HttpExchange exchange) throws IOException {
253253
if (updateResponse.rangeHeader() != null) {
254254
exchange.getResponseHeaders().add("Range", updateResponse.rangeHeader().headerString());
255255
}
256-
exchange.getResponseHeaders().add("Content-Length", "0");
256+
exchange.getResponseHeaders().add("x-goog-stored-content-length", String.valueOf(updateResponse.storedContentLength()));
257257
exchange.sendResponseHeaders(updateResponse.statusCode(), -1);
258258
} else {
259259
exchange.sendResponseHeaders(RestStatus.NOT_FOUND.getStatus(), -1);

test/fixtures/gcs-fixture/src/main/java/fixture/gcs/MockGcsBlobStore.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,16 @@ UpdateResponse updateResumableUpload(String uploadId, HttpHeaderParser.ContentRa
117117
if (contentRange.hasRange() == false) {
118118
// Content-Range: */... is a status check https://cloud.google.com/storage/docs/performing-resumable-uploads#status-check
119119
if (existing.completed) {
120-
updateResponse.set(new UpdateResponse(RestStatus.OK.getStatus(), calculateRangeHeader(blobs.get(existing.path))));
120+
updateResponse.set(
121+
new UpdateResponse(
122+
RestStatus.OK.getStatus(),
123+
calculateRangeHeader(blobs.get(existing.path)),
124+
existing.contents.length()
125+
)
126+
);
121127
} else {
122128
final HttpHeaderParser.Range range = calculateRangeHeader(existing);
123-
updateResponse.set(new UpdateResponse(RESUME_INCOMPLETE, range));
129+
updateResponse.set(new UpdateResponse(RESUME_INCOMPLETE, range, existing.contents.length()));
124130
}
125131
return existing;
126132
} else {
@@ -146,11 +152,11 @@ UpdateResponse updateResumableUpload(String uploadId, HttpHeaderParser.ContentRa
146152
// We just received the last chunk, update the blob and remove the resumable upload from the map
147153
if (contentRange.hasSize() && updatedContent.length() == contentRange.size()) {
148154
updateBlob(existing.path(), existing.ifGenerationMatch, updatedContent);
149-
updateResponse.set(new UpdateResponse(RestStatus.OK.getStatus(), null));
155+
updateResponse.set(new UpdateResponse(RestStatus.OK.getStatus(), null, updatedContent.length()));
150156
return existing.update(BytesArray.EMPTY, true);
151157
}
152158
final ResumableUpload updated = existing.update(updatedContent, false);
153-
updateResponse.set(new UpdateResponse(RESUME_INCOMPLETE, calculateRangeHeader(updated)));
159+
updateResponse.set(new UpdateResponse(RESUME_INCOMPLETE, calculateRangeHeader(updated), updated.contents.length()));
154160
return updated;
155161
}
156162
});
@@ -166,7 +172,7 @@ private static HttpHeaderParser.Range calculateRangeHeader(BlobVersion blob) {
166172
return blob.contents.length() > 0 ? new HttpHeaderParser.Range(0, blob.contents.length() - 1) : null;
167173
}
168174

169-
record UpdateResponse(int statusCode, HttpHeaderParser.Range rangeHeader) {}
175+
record UpdateResponse(int statusCode, HttpHeaderParser.Range rangeHeader, long storedContentLength) {}
170176

171177
void deleteBlob(String path) {
172178
blobs.remove(path);

0 commit comments

Comments
 (0)