Skip to content

Commit ff5f1e6

Browse files
authored
fix: normalise request headers before sending (#133)
* fix: normalise request headers before sending * ci: backport semantic release config fixes #15
1 parent a7b1173 commit ff5f1e6

File tree

6 files changed

+55
-9
lines changed

6 files changed

+55
-9
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
out/
22
jest.config.js
3+
*.cjs

.github/workflows/pr.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: PR
22

33
on:
44
pull_request:
5-
branches: [main]
5+
branches: [main, 4.x]
66

77
jobs:
88
test:

.github/workflows/main.yml renamed to .github/workflows/release.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ name: Release workflow
22

33
on:
44
push:
5-
branches: [main]
5+
branches:
6+
- 'main'
7+
- 'next'
8+
- '4.x'
69

710
jobs:
811
test:
@@ -42,3 +45,4 @@ jobs:
4245
- run: yarn release
4346
env:
4447
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
48+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

release.config.cjs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
const { repository } = require("./package.json");
22
module.exports = {
3-
branches: ["main"],
3+
branches: [
4+
"+([0-9])?(.{+([0-9]),x}).x",
5+
{ name: "main" },
6+
{ name: "next", channel: "next", prerelease: "next" },
7+
],
48
plugins: [
59
[
610
"@semantic-release/commit-analyzer",
@@ -17,6 +21,7 @@ module.exports = {
1721
"@semantic-release/changelog",
1822
"@semantic-release/npm",
1923
"@semantic-release/git",
24+
"@semantic-release/github"
2025
],
2126

2227
repositoryUrl: repository.url,

src/lib/fetch.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,33 @@ export function createFetch(got: Got): GotFetch {
5353
responseType: undefined
5454
};
5555

56-
const { body, headers: bodyHeaders } = serializeBody(request.body);
56+
const { body, headers: bodyHeaders = {} } = serializeBody(request.body);
5757

5858
// only set the `body` key on the options if a body is sent
5959
// otherwise got crashes
6060
if (body) {
6161
gotOpts.body = body;
6262
}
6363

64-
if (bodyHeaders || request.headers) {
65-
gotOpts.headers = {
66-
...bodyHeaders,
67-
...(request.headers as object)
68-
};
64+
const headers: GotOptions["headers"] = { ...bodyHeaders };
65+
if (request.headers) {
66+
if (Array.isArray(request.headers)) {
67+
request.headers.forEach(([header, value]) => {
68+
headers[header.toLowerCase()] = value;
69+
});
70+
} else if (typeof request.headers?.forEach === "function") {
71+
request.headers.forEach((value, header) => {
72+
headers[header.toLowerCase()] = value;
73+
});
74+
} else {
75+
Object.keys(request.headers).forEach((header) => {
76+
headers[header.toLowerCase()] = (request.headers as Record<string, string>)[header];
77+
});
78+
}
6979
}
7080

81+
gotOpts.headers = headers;
82+
7183
// there's a bug in got where it crashes if we send both a body and cache
7284
// https://github.com/sindresorhus/got/issues/1021
7385
if ((typeof request.cache === 'undefined' || request.cache === 'default') && !gotOpts.body) {

src/test/fetch.request.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,29 @@ describe('fetch request', () => {
174174
}));
175175

176176
});
177+
178+
it.only("sends own content-type header", async () => {
179+
expect.assertions(1);
180+
181+
// as set by the client
182+
interceptor
183+
.post("/")
184+
.matchHeader("content-type", "text/plain; charset=utf-8")
185+
.reply(400);
186+
187+
interceptor
188+
.post("/")
189+
.matchHeader("Content-Type", "application/json")
190+
.reply(200);
191+
192+
const fetch = createFetch(got);
193+
await assert200(
194+
fetch(url("/"), {
195+
method: "post",
196+
body: JSON.stringify({ foo: "bar" }),
197+
headers: { "content-Type": "application/json" },
198+
})
199+
);
200+
});
177201
});
178202
});

0 commit comments

Comments
 (0)