Skip to content

Commit

Permalink
fix: adds content type only on post method (#198)
Browse files Browse the repository at this point in the history
Only sets the content-type header on POST methods. The SDK used to set it on GET methods too, but GET methods don't have a body, so the header isn't needed.

* fix: adds content type only on post method

* fix: adds check for header in GET and Post test

---------

Co-authored-by: mohsen <m.khalili@emofid.com>
  • Loading branch information
mohsen-khalili and mohsen authored Mar 18, 2024
1 parent 743a398 commit 5875dbe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ test('Should perform an initial fetch as POST', async () => {

expect(request.method).toBe('POST');
expect(body.context.appName).toBe('webAsPOST');
expect(request.headers).toMatchObject({
'Content-Type': 'application/json',
});
});

test('Should perform an initial fetch as GET', async () => {
Expand All @@ -75,6 +78,9 @@ test('Should perform an initial fetch as GET', async () => {
const request = getTypeSafeRequest(fetchMock, 0);

expect(request.method).toBe('GET');
expect(request.headers).not.toMatchObject({
'Content-Type': 'application/json',
});
});

test('Should have correct variant', async () => {
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,14 @@ export class UnleashClient extends TinyEmitter {
}

private getHeaders() {
const isPOST = this.usePOSTrequests;
const headers = {
[this.headerName]: this.clientKey,
Accept: 'application/json',
'Content-Type': 'application/json',
};
if (isPOST) {
headers['Content-Type'] = 'application/json';
}
if (this.etag) {
headers['If-None-Match'] = this.etag;
}
Expand Down

0 comments on commit 5875dbe

Please sign in to comment.