Skip to content

Commit

Permalink
Merge pull request #3743 from clement-escolano/patch-1
Browse files Browse the repository at this point in the history
Add Content-Type header only for non GET requests.
  • Loading branch information
fzaninotto authored Oct 3, 2019
2 parents 735bfee + 7bcdd9e commit f940e4c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
"prettier": "~1.17.1",
"raf": "~3.4.0",
"ts-jest": "^23.10.5",
"wait-on": "^2.1.0"
"wait-on": "^2.1.0",
"whatwg-fetch": "^3.0.0"
},
"workspaces": [
"packages/*",
Expand Down
32 changes: 31 additions & 1 deletion packages/ra-core/src/util/fetch.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import assert from 'assert';
import { queryParameters, flattenObject } from './fetch';
import {
createHeadersFromOptions,
queryParameters,
flattenObject,
} from './fetch';

describe('queryParameters', () => {
it('should generate a query parameter', () => {
Expand Down Expand Up @@ -57,3 +61,29 @@ describe('flattenObject', () => {
assert.deepEqual(flattenObject(input), expected);
});
});

describe('createHeadersFromOptions', () => {
it('should add a Content-Type header for POST requests', () => {
const options = {
method: 'POST',
};

const headers = createHeadersFromOptions(options);
assert.strictEqual(headers.get('Content-Type'), 'application/json');
});

it('should not add a Content-Type header for GET requests', () => {
const optionsWithoutMethod = {};
const optionsWithMethod = {
method: 'GET',
};

const headersWithMethod = createHeadersFromOptions(optionsWithMethod);
assert.strictEqual(headersWithMethod.get('Content-Type'), null);

const headersWithoutMethod = createHeadersFromOptions(
optionsWithoutMethod
);
assert.strictEqual(headersWithoutMethod.get('Content-Type'), null);
});
});
9 changes: 8 additions & 1 deletion packages/ra-core/src/util/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ interface Options extends RequestInit {
};
}

export const fetchJson = (url, options: Options = {}) => {
export const createHeadersFromOptions = (options: Options): Headers => {
const requestHeaders = (options.headers ||
new Headers({
Accept: 'application/json',
})) as Headers;
if (
!requestHeaders.has('Content-Type') &&
!(options && (!options.method || options.method === 'GET')) &&
!(options && options.body && options.body instanceof FormData)
) {
requestHeaders.set('Content-Type', 'application/json');
Expand All @@ -23,6 +24,12 @@ export const fetchJson = (url, options: Options = {}) => {
requestHeaders.set('Authorization', options.user.token);
}

return requestHeaders;
};

export const fetchJson = (url, options: Options = {}) => {
const requestHeaders = createHeadersFromOptions(options);

return fetch(url, { ...options, headers: requestHeaders })
.then(response =>
response.text().then(text => ({
Expand Down
10 changes: 10 additions & 0 deletions test-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,13 @@ jest.mock('popper.js', () => {
];
return Popper;
});

/**
* Mock fetch objects Response, Request and Headers
*/

const { Response, Headers, Request } = require('whatwg-fetch');

global.Response = Response;
global.Headers = Headers;
global.Request = Request;
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16035,7 +16035,7 @@ whatwg-encoding@^1.0.3, whatwg-encoding@^1.0.5:
dependencies:
iconv-lite "0.4.24"

[email protected]:
[email protected], whatwg-fetch@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-3.0.0.tgz#fc804e458cc460009b1a2b966bc8817d2578aefb"
integrity sha512-9GSJUgz1D4MfyKU7KRqwOjXCXTqWdFNvEr7eUBYchQiVc744mqK/MzXPNR2WsPkmkOa4ywfg8C2n8h+13Bey1Q==
Expand Down

0 comments on commit f940e4c

Please sign in to comment.