Skip to content

Commit

Permalink
Add Content-Type check for headers (#3829)
Browse files Browse the repository at this point in the history
* adding in check if Content-Type exists

* changelog

* nits
  • Loading branch information
colerogers committed Oct 18, 2021
1 parent a45a8fc commit 8f3a1ec
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/apiv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,10 @@ export class Client {
reqOptions.headers.set("User-Agent", `FirebaseCLI/${CLI_VERSION}`);
}
reqOptions.headers.set("X-Client-Version", `FirebaseCLI/${CLI_VERSION}`);
if (reqOptions.responseType === "json") {
reqOptions.headers.set("Content-Type", "application/json");
if (!reqOptions.headers.has("Content-Type")) {
if (reqOptions.responseType === "json") {
reqOptions.headers.set("Content-Type", "application/json");
}
}
return reqOptions;
}
Expand Down
23 changes: 22 additions & 1 deletion src/test/apiv2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,34 @@ describe("apiv2", () => {

it("should make a basic POST request", async () => {
const POST_DATA = { post: "data" };
nock("https://example.com").post("/path/to/foo", POST_DATA).reply(200, { success: true });
nock("https://example.com")
.matchHeader("Content-Type", "application/json")
.post("/path/to/foo", POST_DATA)
.reply(200, { success: true });

const c = new Client({ urlPrefix: "https://example.com" });
const r = await c.request({
method: "POST",
path: "/path/to/foo",
body: POST_DATA,
});
expect(r.body).to.deep.equal({ success: true });
expect(nock.isDone()).to.be.true;
});

it("should make a basic POST request without overriding Content-Type", async () => {
const POST_DATA = { post: "data" };
nock("https://example.com")
.matchHeader("Content-Type", "application/json+customcontent")
.post("/path/to/foo", POST_DATA)
.reply(200, { success: true });

const c = new Client({ urlPrefix: "https://example.com" });
const r = await c.request({
method: "POST",
path: "/path/to/foo",
body: POST_DATA,
headers: { "Content-Type": "application/json+customcontent" },
});
expect(r.body).to.deep.equal({ success: true });
expect(nock.isDone()).to.be.true;
Expand Down

0 comments on commit 8f3a1ec

Please sign in to comment.