Skip to content

Commit

Permalink
Make storage emulator content type parser case insensitive (#4824)
Browse files Browse the repository at this point in the history
* make storage emulator content type case insensitive

* add changelog
  • Loading branch information
Yuangwang authored Aug 3, 2022
1 parent 11bc020 commit d446fff
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Replaces underlying terminal coloring library.
- Make storage emulator content type case insensitive #3953
4 changes: 2 additions & 2 deletions src/emulator/storage/multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ function parseMultipartRequestBodyPart(bodyPart: Buffer): MultipartRequestBodyPa
// splitting the entire body part buffer.
const sections = splitBufferByDelimiter(bodyPart, LINE_SEPARATOR, /* maxResults = */ 3);

const contentTypeRaw = sections[0].toString();
if (!contentTypeRaw.startsWith("Content-Type: ")) {
const contentTypeRaw = sections[0].toString().toLowerCase();
if (!contentTypeRaw.startsWith("content-type: ")) {
throw new Error(`Failed to parse multipart request body part. Missing content type.`);
}

Expand Down
19 changes: 19 additions & 0 deletions src/test/emulators/storage/multipart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ Content-Type: text/plain\r
expect(dataRaw.byteLength).to.equal(data.byteLength);
});

it("parses an upload object multipart request with lowercase content-type", () => {
const body = Buffer.from(`--b1d5b2e3-1845-4338-9400-6ac07ce53c1e\r
content-type: application/json\r
\r
{"contentType":"text/plain"}\r
--b1d5b2e3-1845-4338-9400-6ac07ce53c1e\r
content-type: text/plain\r
\r
hello there!
\r
--b1d5b2e3-1845-4338-9400-6ac07ce53c1e--\r
`);

const { metadataRaw, dataRaw } = parseObjectUploadMultipartRequest(CONTENT_TYPE_HEADER, body);

expect(metadataRaw).to.equal('{"contentType":"text/plain"}');
expect(dataRaw.toString()).to.equal("hello there!\n");
});

it("fails to parse with invalid Content-Type value", () => {
const invalidContentTypeHeader = "blah";
expect(() => parseObjectUploadMultipartRequest(invalidContentTypeHeader, BODY)).to.throw(
Expand Down

0 comments on commit d446fff

Please sign in to comment.