Skip to content

Commit

Permalink
Added status header for query upload requests (#5425)
Browse files Browse the repository at this point in the history
  • Loading branch information
maneesht committed Jan 18, 2023
1 parent 1756213 commit c52ae18
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import {
const TEST_FILE_NAME = "testing/storage_ref/testFile";
const ENCODED_TEST_FILE_NAME = "testing%2Fstorage_ref%2FtestFile";

// headers
const uploadStatusHeader = "x-goog-upload-status";

// TODO(b/242314185): add more coverage.
describe("Firebase Storage endpoint conformance tests", () => {
// Temp directory to store generated files.
Expand Down Expand Up @@ -179,7 +182,7 @@ describe("Firebase Storage endpoint conformance tests", () => {
"X-Goog-Upload-Command": "finalize",
})
.expect(200)
.then((res) => res.header["x-goog-upload-status"]);
.then((res) => res.header[uploadStatusHeader]);

expect(uploadStatus).to.equal("final");

Expand Down Expand Up @@ -212,7 +215,7 @@ describe("Firebase Storage endpoint conformance tests", () => {
})
.send({})
.expect(200)
.then((res) => res.header["x-goog-upload-status"]);
.then((res) => res.header[uploadStatusHeader]);
expect(finalizeStatus).to.equal("final");
});

Expand All @@ -236,7 +239,7 @@ describe("Firebase Storage endpoint conformance tests", () => {
"X-Goog-Upload-Offset": 0,
})
.expect(403)
.then((res) => res.header["x-goog-upload-status"]);
.then((res) => res.header[uploadStatusHeader]);
expect(uploadStatus).to.equal("final");
});

Expand Down Expand Up @@ -266,7 +269,7 @@ describe("Firebase Storage endpoint conformance tests", () => {
"X-Goog-Upload-Command": "finalize",
})
.expect(403)
.then((res) => res.header["x-goog-upload-status"]);
.then((res) => res.header[uploadStatusHeader]);
expect(uploadStatus).to.equal("final");
});

Expand Down Expand Up @@ -360,7 +363,7 @@ describe("Firebase Storage endpoint conformance tests", () => {
"X-Goog-Upload-Command": "finalize",
})
.expect(200)
.then((res) => res.header["x-goog-upload-status"]);
.then((res) => res.header[uploadStatusHeader]);

expect(uploadStatus).to.equal("final");

Expand Down Expand Up @@ -546,6 +549,93 @@ describe("Firebase Storage endpoint conformance tests", () => {
});
});

describe("upload status", () => {
it("should update the status to active after an upload is started", async () => {
const uploadURL = await supertest(firebaseHost)
.post(`/v0/b/${storageBucket}/o?name=${TEST_FILE_NAME}`)
.set(authHeader)
.set({
"X-Goog-Upload-Protocol": "resumable",
"X-Goog-Upload-Command": "start",
})
.expect(200)
.then((res) => new URL(res.header["x-goog-upload-url"]));
const queryUploadStatus = await supertest(firebaseHost)
.put(uploadURL.pathname + uploadURL.search)
.set({
"X-Goog-Upload-Protocol": "resumable",
"X-Goog-Upload-Command": "query",
})
.expect(200)
.then((res) => res.header[uploadStatusHeader]);
expect(queryUploadStatus).to.equal("active");
});
it("should update the status to cancelled after an upload is cancelled", async () => {
const uploadURL = await supertest(firebaseHost)
.post(`/v0/b/${storageBucket}/o/${ENCODED_TEST_FILE_NAME}`)
.set(authHeader)
.set({
"X-Goog-Upload-Protocol": "resumable",
"X-Goog-Upload-Command": "start",
})
.expect(200)
.then((res) => new URL(res.header["x-goog-upload-url"]));
await supertest(firebaseHost)
.put(uploadURL.pathname + uploadURL.search)
.set({
"X-Goog-Upload-Protocol": "resumable",
"X-Goog-Upload-Command": "cancel",
})
.expect(200);
const queryUploadStatus = await supertest(firebaseHost)
.put(uploadURL.pathname + uploadURL.search)
.set({
"X-Goog-Upload-Protocol": "resumable",
"X-Goog-Upload-Command": "query",
})
.expect(200)
.then((res) => res.header[uploadStatusHeader]);
expect(queryUploadStatus).to.equal("cancelled");
});
it("should update the status to final after an upload is finalized", async () => {
const uploadURL = await supertest(firebaseHost)
.post(`/v0/b/${storageBucket}/o?name=${TEST_FILE_NAME}`)
.set(authHeader)
.set({
"X-Goog-Upload-Protocol": "resumable",
"X-Goog-Upload-Command": "start",
})
.expect(200)
.then((res) => new URL(res.header["x-goog-upload-url"]));

await supertest(firebaseHost)
.put(uploadURL.pathname + uploadURL.search)
.set({
"X-Goog-Upload-Protocol": "resumable",
"X-Goog-Upload-Command": "upload",
"X-Goog-Upload-Offset": 0,
})
.expect(200);
await supertest(firebaseHost)
.put(uploadURL.pathname + uploadURL.search)
.set({
"X-Goog-Upload-Protocol": "resumable",
"X-Goog-Upload-Command": "finalize",
})
.expect(200)
.then((res) => res.header[uploadStatusHeader]);
const queryUploadStatus = await supertest(firebaseHost)
.put(uploadURL.pathname + uploadURL.search)
.set({
"X-Goog-Upload-Protocol": "resumable",
"X-Goog-Upload-Command": "query",
})
.expect(200)
.then((res) => res.header[uploadStatusHeader]);
expect(queryUploadStatus).to.equal("final");
});
});

describe("tokens", () => {
beforeEach(async () => {
await testBucket.upload(smallFilePath, { destination: TEST_FILE_NAME });
Expand Down
1 change: 1 addition & 0 deletions src/emulator/storage/apis/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ export function createFirebaseEndpoints(emulator: StorageEmulator): Router {
throw err;
}
res.header("X-Goog-Upload-Size-Received", upload.size.toString());
res.header("x-goog-upload-status", upload.status);
return res.sendStatus(200);
}

Expand Down
2 changes: 1 addition & 1 deletion src/emulator/storage/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export function createApp(
exposedHeaders: [
"content-type",
"x-firebase-storage-version",
"X-Goog-Upload-Size-Received",
"x-goog-upload-url",
"x-goog-upload-status",
"x-goog-upload-command",
"x-gupload-uploadid",
"x-goog-upload-header-content-length",
Expand Down
6 changes: 3 additions & 3 deletions src/emulator/storage/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export enum UploadType {

/** The status of an upload. Multipart uploads can only ever be FINISHED. */
export enum UploadStatus {
ACTIVE,
CANCELLED,
FINISHED,
ACTIVE = "active",
CANCELLED = "cancelled",
FINISHED = "final",
}

/** Request object for {@link UploadService#mediaUpload}. */
Expand Down

0 comments on commit c52ae18

Please sign in to comment.