Skip to content

Commit

Permalink
Fix merging custom headers (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
dillonredding authored Aug 2, 2023
1 parent a3328a1 commit f7b9538
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
30 changes: 24 additions & 6 deletions src/submit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,34 @@ describe('submit', () => {
expect(scope.isDone()).toBe(true);
});

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

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

expect(response.url).toBe(url);
expect(response.status).toBe(204);
expect(scope.isDone()).toBe(true);
const response = await submit(action, { requestInit: { headers } });

expect(response.url).toBe(url);
expect(response.status).toBe(204);
expect(scope.isDone()).toBe(true);
});

it('should merge custom headers with base headers', async () => {
const scope = nock(baseUrl, { reqheaders: headers }).post(path).reply(204);

const response = await submit(action, { requestInit: { headers: Object.entries(headers) } });

expect(response.url).toBe(url);
expect(response.status).toBe(204);
expect(scope.isDone()).toBe(true);
});
});

it('should resolve relative URL', async () => {
Expand Down
6 changes: 4 additions & 2 deletions src/submit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fetch from 'cross-fetch';
import { fetch, Headers } from 'cross-fetch';

import { Href } from './href';
import { Action } from './models/action';
Expand Down Expand Up @@ -52,7 +52,9 @@ export async function submit(action: Action, options: SubmitOptions = {}): Promi
target.search = serialization.content.toString();
} else {
if (serialization.contentType) {
init.headers = { ...init.headers, 'Content-Type': serialization.contentType };
const headers = new Headers(init.headers);
headers.set('Content-Type', serialization.contentType);
init.headers = headers;
}
init.body = serialization.content;
}
Expand Down

0 comments on commit f7b9538

Please sign in to comment.