From 641119e3487cae910c9efd20e43fc9c5c2116b16 Mon Sep 17 00:00:00 2001 From: Sebastian Gronewold Date: Wed, 18 Nov 2015 12:11:38 +0100 Subject: [PATCH] Fixed overriding of querystring in internet explorer * Added Webstorm IDE directory to .gitignore * Added check if querystring exists and then only append a cache buster * Adjusted and also fixed tests (were not working) * Added info about how to run tests to Readme.md --- .gitignore | 1 + Readme.md | 6 +++++- index.js | 8 +++++++- test/superagent-no-cache.test.js | 26 +++++++++++++++++++++++--- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 496f79d..9f3a8ce 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ components build coverage node_modules +.idea/ diff --git a/Readme.md b/Readme.md index 46a48a1..2496f0d 100644 --- a/Readme.md +++ b/Readme.md @@ -29,7 +29,11 @@ request.get('/url') ## Contributing -To come soon... +To run the tests install mocha and run: + +```bash +$ mocha test/superagent-no-cache.test.js +``` ## License diff --git a/index.js b/index.js index 4bd0220..955272f 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,13 @@ try { } function with_query_strings (request) { - request._query = [Date.now().toString()] + var timestamp = Date.now().toString() + if (request._query !== undefined && request._query[0]) { + request._query[0] += '&buster=' + timestamp + } else { + request._query = [timestamp] + } + return request } diff --git a/test/superagent-no-cache.test.js b/test/superagent-no-cache.test.js index 6d0ce0a..b617591 100644 --- a/test/superagent-no-cache.test.js +++ b/test/superagent-no-cache.test.js @@ -19,6 +19,7 @@ function _suiteMain () { it('should require the "set" subfunction', _specSetCheck) it('should run the "set" subfunction, assigning the appropriate values', _specSetRun) it('should add an additional query for IE browsers', _specIE) + it('should add an additional param to an existing query string for IE browsers', _specIEExistingQuery) } /* Specs */ @@ -40,7 +41,7 @@ function _specSetCheck (done) { expect(error) .to.be.an('object') expect(error.toString()) - .to.equal('TypeError: undefined is not a function') + .to.contain('TypeError') done() } } @@ -59,7 +60,7 @@ function _specSetRun (done) { expect(results.data['Cache-Control']) .to.be.a('string') expect(results.data['Cache-Control']) - .to.equal('no-cache,no-store,must-revalidate,max-age=-1') + .to.equal('no-cache,no-store,must-revalidate,max-age=-1,private') expect(results._query) .to.be.an('undefined') @@ -75,7 +76,7 @@ function _specIE (done) { expect(results.data['X-Requested-With']) .to.equal('XMLHttpRequest') expect(results.data['Cache-Control']) - .to.equal('no-cache,no-store,must-revalidate,max-age=-1') + .to.equal('no-cache,no-store,must-revalidate,max-age=-1,private') expect(results._query) .to.be.an('array') expect(results._query[0]) @@ -84,6 +85,25 @@ function _specIE (done) { .to.equal(Date.now().toString().substring(0, results._query[0].length - 1)) done() } +function _specIEExistingQuery (done) { + var request = { + set:_mockSet, + _query: ['param=123'], // random query string + data:{} + }; + var results = nocache(request, true); + expect(results.data['X-Requested-With']) + .to.equal('XMLHttpRequest') + expect(results.data['Cache-Control']) + .to.equal('no-cache,no-store,must-revalidate,max-age=-1,private') + expect(results._query) + .to.be.an('array') + expect(results._query[0]) + .to.be.a('string') + expect(results._query[0]) + .to.match(/^param=123&buster=[0-9].*$/) + done(); +} /* Mocks */