From 2d605b3d47df11b0d872761faaf483cd8424cd88 Mon Sep 17 00:00:00 2001 From: rekarnar Date: Wed, 27 Jan 2016 11:54:42 +0545 Subject: [PATCH 1/7] Cherry-pick 'adding JSON test' from PR#27 by rek --- test/test-najax.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/test-najax.js b/test/test-najax.js index 719d66b..851239a 100644 --- a/test/test-najax.js +++ b/test/test-najax.js @@ -82,6 +82,19 @@ describe('url', function (next) { najax({ url: 'http://www.example.com' }, createSuccess(done)) }) + it('should not fail when result is broken JSON', function (done) { + mockPlain('get') + najax({url: 'http://www.example.com', dataType: 'json'}, jsonError(done)) + }) + + it('should succeed when result is good JSON', function (done) { + nock('http://www.example.com') + .get('/') + .reply(200, {"test": "ok"}) + + najax({url: 'http://www.example.com', dataType: 'json'}, jsonSuccess(done)) + }) + it('should parse auth from the url', function (done) { mockAuth('get') najax({ url: 'http://' + authString + '@www.example.com' }, createSuccess(done)) @@ -343,6 +356,22 @@ function createSuccess (done) { } } +function jsonSuccess (done) { + return function (data, statusText) { + expect(data.test).to.equal('ok') + expect(statusText).to.equal('success') + done() + } +} + +function jsonError (done) { + return function (data, statusText) { + expect(data.test).to.be.undefined + expect(statusText).to.equal('success') + done() + } +} + function error (e) { throw e } From 549eded183e350692dbb8313c3df24dc88ff8139 Mon Sep 17 00:00:00 2001 From: rekarnar Date: Wed, 27 Jan 2016 15:14:09 +0545 Subject: [PATCH 2/7] Cherry-pick 'lint' from PR#27 by rek --- test/test-najax.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test-najax.js b/test/test-najax.js index 851239a..88dffa6 100644 --- a/test/test-najax.js +++ b/test/test-najax.js @@ -90,7 +90,7 @@ describe('url', function (next) { it('should succeed when result is good JSON', function (done) { nock('http://www.example.com') .get('/') - .reply(200, {"test": "ok"}) + .reply(200, {'test': 'ok'}) najax({url: 'http://www.example.com', dataType: 'json'}, jsonSuccess(done)) }) From ef033f521c9fb009c24c225ef5649c57c21485a0 Mon Sep 17 00:00:00 2001 From: JoshuaCWebDeveloper Date: Mon, 11 Jul 2016 18:49:37 -0700 Subject: [PATCH 3/7] Emulate jQuery by resolving with error object on JSON parse error --- lib/najax.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/najax.js b/lib/najax.js index 952ea7e..a761e6d 100644 --- a/lib/najax.js +++ b/lib/najax.js @@ -138,7 +138,11 @@ function najax (uri, options, callback) { try { data = JSON.parse(data.replace(/[\cA-\cZ]/gi, '')) } catch (e) { - return onError(e) + //emulate jQuery functionality (replace data with JSON parse error) + data = { + state: 'parsererror', + error: 'Failed to parse JSON string: ' + e.message + } } } From fe4ff6d2c721e7087ac5a0f643c5cd5a01010490 Mon Sep 17 00:00:00 2001 From: JoshuaCWebDeveloper Date: Mon, 11 Jul 2016 18:50:08 -0700 Subject: [PATCH 4/7] Update jsonError tester to check for error state prop --- test/test-najax.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test-najax.js b/test/test-najax.js index 88dffa6..3742c65 100644 --- a/test/test-najax.js +++ b/test/test-najax.js @@ -366,7 +366,7 @@ function jsonSuccess (done) { function jsonError (done) { return function (data, statusText) { - expect(data.test).to.be.undefined + expect(data.state).to.equal('parsererror') expect(statusText).to.equal('success') done() } From da1ee3cedf37caaa6179caf03708254fecd9cc16 Mon Sep 17 00:00:00 2001 From: JoshuaCWebDeveloper Date: Mon, 11 Jul 2016 20:16:37 -0700 Subject: [PATCH 5/7] Add workaround for nock glitch in test-najax --- test/test-najax.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/test-najax.js b/test/test-najax.js index 3742c65..06516a8 100644 --- a/test/test-najax.js +++ b/test/test-najax.js @@ -73,6 +73,14 @@ describe('url', function (next) { } it('should accept plain URL', function (done) { + // An extra nock seems to be being + // registered and affecting the subsequent tests + // (I beleive it is caused by: + // https://github.com/najaxjs/najax/commit/54cfac6aa0b7cf8a89efae0f39d1f054c8859de0 + // commenting out the 'default to "GET"' test also fixes this) + // make an extra call to najax in order to clear it + najax({url: 'http://www.example.com'}) + mockPlain('get') najax('http://www.example.com', createSuccess(done)) }) From 616e3bb93226d839a19bfe4b3d601dae50365b6a Mon Sep 17 00:00:00 2001 From: JoshuaCWebDeveloper Date: Wed, 13 Jul 2016 11:00:06 -0700 Subject: [PATCH 6/7] Correct style, add space after comment --- lib/najax.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/najax.js b/lib/najax.js index a761e6d..08942dc 100644 --- a/lib/najax.js +++ b/lib/najax.js @@ -138,7 +138,7 @@ function najax (uri, options, callback) { try { data = JSON.parse(data.replace(/[\cA-\cZ]/gi, '')) } catch (e) { - //emulate jQuery functionality (replace data with JSON parse error) + // emulate jQuery functionality (replace data with JSON parse error) data = { state: 'parsererror', error: 'Failed to parse JSON string: ' + e.message From 9655ef53d035082cdf91794f84fba6432b9df7f5 Mon Sep 17 00:00:00 2001 From: JoshuaCWebDeveloper Date: Wed, 13 Jul 2016 11:02:32 -0700 Subject: [PATCH 7/7] Replace extra najax call hack with global hook to clear nock afterEach test --- test/test-najax.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/test/test-najax.js b/test/test-najax.js index 06516a8..cd48028 100644 --- a/test/test-najax.js +++ b/test/test-najax.js @@ -1,9 +1,15 @@ -/* globals describe beforeEach it */ +/* globals describe beforeEach afterEach it */ var najax = require('../lib/najax.js') var expect = require('chai').expect var nock = require('nock') var zlib = require('zlib') +// prevent cross-test or cross-describe contamination by +// globally clearing nock after each test +afterEach(function () { + nock.cleanAll() +}) + describe('method overloads', function (next) { najax.defaults({ error: error }) @@ -73,14 +79,6 @@ describe('url', function (next) { } it('should accept plain URL', function (done) { - // An extra nock seems to be being - // registered and affecting the subsequent tests - // (I beleive it is caused by: - // https://github.com/najaxjs/najax/commit/54cfac6aa0b7cf8a89efae0f39d1f054c8859de0 - // commenting out the 'default to "GET"' test also fixes this) - // make an extra call to najax in order to clear it - najax({url: 'http://www.example.com'}) - mockPlain('get') najax('http://www.example.com', createSuccess(done)) })