Skip to content

Commit f7b9538

Browse files
Fix merging custom headers (#36)
1 parent a3328a1 commit f7b9538

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

src/submit.spec.ts

+24-6
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,34 @@ describe('submit', () => {
101101
expect(scope.isDone()).toBe(true);
102102
});
103103

104-
it('should accept and send request options', async () => {
104+
describe('requestInit option', () => {
105105
const apiKey = 'foo-bar-baz';
106106
const headers = { 'Api-Key': apiKey };
107-
const scope = nock(baseUrl, { reqheaders: headers }).get(path).reply(204);
107+
const action = new Action();
108+
action.name = 'foo';
109+
action.href = url;
110+
action.method = 'POST';
111+
action.fields = [nameField];
108112

109-
const response = await submit(action, { requestInit: { headers } });
113+
it('should send custom headers', async () => {
114+
const scope = nock(baseUrl, { reqheaders: headers }).post(path).reply(204);
110115

111-
expect(response.url).toBe(url);
112-
expect(response.status).toBe(204);
113-
expect(scope.isDone()).toBe(true);
116+
const response = await submit(action, { requestInit: { headers } });
117+
118+
expect(response.url).toBe(url);
119+
expect(response.status).toBe(204);
120+
expect(scope.isDone()).toBe(true);
121+
});
122+
123+
it('should merge custom headers with base headers', async () => {
124+
const scope = nock(baseUrl, { reqheaders: headers }).post(path).reply(204);
125+
126+
const response = await submit(action, { requestInit: { headers: Object.entries(headers) } });
127+
128+
expect(response.url).toBe(url);
129+
expect(response.status).toBe(204);
130+
expect(scope.isDone()).toBe(true);
131+
});
114132
});
115133

116134
it('should resolve relative URL', async () => {

src/submit.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fetch from 'cross-fetch';
1+
import { fetch, Headers } from 'cross-fetch';
22

33
import { Href } from './href';
44
import { Action } from './models/action';
@@ -52,7 +52,9 @@ export async function submit(action: Action, options: SubmitOptions = {}): Promi
5252
target.search = serialization.content.toString();
5353
} else {
5454
if (serialization.contentType) {
55-
init.headers = { ...init.headers, 'Content-Type': serialization.contentType };
55+
const headers = new Headers(init.headers);
56+
headers.set('Content-Type', serialization.contentType);
57+
init.headers = headers;
5658
}
5759
init.body = serialization.content;
5860
}

0 commit comments

Comments
 (0)