Skip to content

Add json parse when the request is 'application/x-www-form-urlencoded'. #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions addon/lib/json-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,40 @@ export function stringifyJSON(json={}){
export function jsonFromRequest(request){
let json = {};
if (request.requestBody) {
try {
json = JSON.parse(request.requestBody);
} catch(e) {
Ember.Logger.warn(`[FakeServer] Failed to parse json from request.requestBody "${request.requestBody}" (error: ${e})`);
if (contentTypeHeaderIsFormUrlEncoded(request.requestHeaders)) {
json = getJsonFromFormUrlEncodedRequest(request.requestBody);
} else {
try {
json = JSON.parse(request.requestBody);
} catch (e) {
Ember.Logger.warn(`[FakeServer] Failed to parse json from request.requestBody "${request.requestBody}" (error: ${e})`);
}
}
}

return json;
}

function contentTypeHeaderIsFormUrlEncoded(requestHeaders) {
// request headers string example:
// {"Content-Type":"application/x-www-form-urlencoded","Accept":"*/*","X-Requested-With":"XMLHttpRequest"}
const caseInsensitiveformUrlEncodedContentTypeHeader = /content-type[":' ]+application\/x-www-form-urlencoded/gi;
const contentTypeHeaderStringToCheckCaseInsensitive = JSON.stringify(requestHeaders);
const contentTypeHeaderIsFormUrlEncoded =
caseInsensitiveformUrlEncodedContentTypeHeader.test(contentTypeHeaderStringToCheckCaseInsensitive);

return contentTypeHeaderIsFormUrlEncoded;
}

function getJsonFromFormUrlEncodedRequest(requestBody) {
let json = {};
try {
// 'application/x-www-form-urlencoded' request body example:
// "foo=bar&hello=World!"
json = JSON.parse('{"' + decodeURIComponent(requestBody.replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g,'":"')) + '"}');
} catch(e) {
Ember.Logger.warn(`[FakeServer] Failed to parse json from request.requestBody "${requestBody}" (error: ${e})`);
}

return json;
}
46 changes: 46 additions & 0 deletions tests/unit/fake-server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,52 @@ test('#json reads JSON in request payload', (assert) => {
});
});

test('json() function parses x-www-form-urlencoded request payload', (assert) => {
let done = assert.async();
assert.expect(1);

const jsonPayload = {
foo: 'bar',
hello: 'World!',
};
const urlEncodedPayload = "foo=bar&hello=World!";

stubRequest('post', '/blah', (request) => {
assert.deepEqual(request.json(), jsonPayload, 'POST payload');
request.noContent();
});

jQuery.ajax('/blah', {
type: 'POST',
data: urlEncodedPayload,
contentType: 'application/x-www-form-urlencoded',
complete: done
});
});

test('json() function parses x-www-form-urlencoded request payload with special characters', (assert) => {
let done = assert.async();
assert.expect(1);

const jsonPayload = {
foo: 'bar',
password: '$€áÉîÖùñÑ',
};
const urlEncodedPayload = "foo=bar&password=$€áÉîÖùñÑ";

stubRequest('post', '/blah', (request) => {
assert.deepEqual(request.json(), jsonPayload, 'POST payload');
request.noContent();
});

jQuery.ajax('/blah', {
type: 'POST',
data: urlEncodedPayload,
contentType: 'application/x-www-form-urlencoded',
complete: done
});
});

test('FakeServer.config.afterResponse can modify responses', (assert) => {
let done = assert.async();
assert.expect(6);
Expand Down