From 197850e2f4bcda71e27c79a725ffd460022afaa0 Mon Sep 17 00:00:00 2001 From: Daniel Hritzkiv Date: Wed, 23 Nov 2016 19:45:27 -0500 Subject: [PATCH 1/5] Make changes to comply with behaviour of xhr@v2.3 xhr @v2.3 suggests that the `json` option should be a boolean, and the data should be set on `body`. While the behaviour is backwards compatible, I figure it would be a good idea to match its behaviour. --- core.js | 5 +++-- test/unit.js | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/core.js b/core.js index 95a6467..c9b173a 100644 --- a/core.js +++ b/core.js @@ -67,7 +67,8 @@ module.exports = function (xhr) { // Ensure that we have the appropriate request data. if (options.data == null && model && (method === 'create' || method === 'update' || method === 'patch')) { - params.json = options.attrs || model.toJSON(options); + params.body = options.attrs || model.toJSON(options); + params.json = true; } // If passed a data param, we add it to the URL or body depending on request type @@ -82,7 +83,7 @@ module.exports = function (xhr) { // For older servers, emulate JSON by encoding the request into an HTML-form. if (options.emulateJSON) { params.headers['content-type'] = 'application/x-www-form-urlencoded'; - params.body = params.json ? {model: params.json} : {}; + params.body = params.json ? {model: params.body || params.json} : {}; delete params.json; } diff --git a/test/unit.js b/test/unit.js index f5bcb90..3eda633 100644 --- a/test/unit.js +++ b/test/unit.js @@ -116,7 +116,7 @@ test('create', function (t) { t.equal(reqStub.recentOpts.url, '/library'); t.equal(reqStub.recentOpts.method, 'POST'); t.ok(reqStub.recentOpts.json, 'body passed as json'); - var data = reqStub.recentOpts.json; + var data = reqStub.recentOpts.body; t.equal(data.title, 'The Tempest'); t.equal(data.author, 'Bill Shakespeare'); t.equal(data.length, 123); @@ -131,7 +131,7 @@ test('update', function (t) { t.equal(reqStub.recentOpts.url, '/library'); t.equal(reqStub.recentOpts.method, 'PUT'); t.ok(reqStub.recentOpts.json, 'body passed as json'); - var data = reqStub.recentOpts.json; + var data = reqStub.recentOpts.body; t.equal(data.id, '1-the-tempest'); t.equal(data.author, 'William Shakespeare'); t.end(); @@ -164,7 +164,7 @@ test('update with just emulateHTTP', function (t) { t.equal(reqStub.recentOpts.url, '/library'); t.equal(reqStub.recentOpts.method, 'POST'); t.ok(reqStub.recentOpts.json, 'body passed as json'); - var data = reqStub.recentOpts.json; + var data = reqStub.recentOpts.body; t.equal(data.id, '2-the-tempest'); t.equal(data.author, 'Tim Shakespeare'); t.equal(data.length, 123); From 82935eeae0c62b5cf924602caa830a97a835f967 Mon Sep 17 00:00:00 2001 From: Daniel Hritzkiv Date: Wed, 23 Nov 2016 19:46:22 -0500 Subject: [PATCH 2/5] Bump xhr version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0215d6c..57c9d89 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "media-type": "0.3.0", "qs": "^6.1.0", "request": "^2.55.0", - "xhr": "^2.0.5" + "xhr": "^2.3.1" }, "devDependencies": { "ampersand-model": "^7.0.0", From ae2dacbba80333aa8de1434eff9e32f463c430a3 Mon Sep 17 00:00:00 2001 From: Daniel Hritzkiv Date: Fri, 25 Nov 2016 12:29:56 -0500 Subject: [PATCH 3/5] Better assertions for `opts.json` value --- test/unit.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/unit.js b/test/unit.js index 3eda633..cc56f31 100644 --- a/test/unit.js +++ b/test/unit.js @@ -115,7 +115,7 @@ test('create', function (t) { })); t.equal(reqStub.recentOpts.url, '/library'); t.equal(reqStub.recentOpts.method, 'POST'); - t.ok(reqStub.recentOpts.json, 'body passed as json'); + t.equal(reqStub.recentOpts.json, true, 'json is set to true'); var data = reqStub.recentOpts.body; t.equal(data.title, 'The Tempest'); t.equal(data.author, 'Bill Shakespeare'); @@ -130,7 +130,7 @@ test('update', function (t) { })); t.equal(reqStub.recentOpts.url, '/library'); t.equal(reqStub.recentOpts.method, 'PUT'); - t.ok(reqStub.recentOpts.json, 'body passed as json'); + t.equal(reqStub.recentOpts.json, true, 'json is set to true'); var data = reqStub.recentOpts.body; t.equal(data.id, '1-the-tempest'); t.equal(data.author, 'William Shakespeare'); @@ -163,7 +163,7 @@ test('update with just emulateHTTP', function (t) { }); t.equal(reqStub.recentOpts.url, '/library'); t.equal(reqStub.recentOpts.method, 'POST'); - t.ok(reqStub.recentOpts.json, 'body passed as json'); + t.equal(reqStub.recentOpts.json, true, 'json is set to true'); var data = reqStub.recentOpts.body; t.equal(data.id, '2-the-tempest'); t.equal(data.author, 'Tim Shakespeare'); From 852626b06bf21175c93ea8030b0a770e39a3adb1 Mon Sep 17 00:00:00 2001 From: Daniel Hritzkiv Date: Fri, 25 Nov 2016 12:39:12 -0500 Subject: [PATCH 4/5] Add test for passing `body` as an option MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …where the explicitly provided body should take precedence over the model’s data. --- test/unit.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/unit.js b/test/unit.js index cc56f31..8d2d007 100644 --- a/test/unit.js +++ b/test/unit.js @@ -352,3 +352,21 @@ test('should parse json for different media types', function (t) { }); }); +test('passing `body` in the opts should take precedence over the model\'s data', function (t) { + var model = modelStub({ + title: 'The Tempest', + author: 'Bill Shakespeare', + length: 123 + }); + + sync('create', model, { + body: { + rating: "5" + } + }); + + t.equal(reqStub.recentOpts.json, true, 'json is set to true'); + var data = reqStub.recentOpts.body; + t.equal(data.rating, '5'); + t.end(); +}); \ No newline at end of file From 33054ab4f7b0d57e81d3486a3b86b7f502f3e69e Mon Sep 17 00:00:00 2001 From: Daniel Hritzkiv Date: Fri, 25 Nov 2016 15:59:22 -0500 Subject: [PATCH 5/5] Simplify setting `params.body` if emulating JSON --- core.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core.js b/core.js index c9b173a..c14bf4e 100644 --- a/core.js +++ b/core.js @@ -83,7 +83,7 @@ module.exports = function (xhr) { // For older servers, emulate JSON by encoding the request into an HTML-form. if (options.emulateJSON) { params.headers['content-type'] = 'application/x-www-form-urlencoded'; - params.body = params.json ? {model: params.body || params.json} : {}; + params.body = params.json ? {model: params.body} : {}; delete params.json; }