Skip to content

Commit

Permalink
Merge pull request johntron#14 from elbstack/master
Browse files Browse the repository at this point in the history
Fixed overriding of querystring in internet explorer
  • Loading branch information
johntron committed May 6, 2016
2 parents 704de56 + d853d32 commit 72d97e3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ components
build
coverage
node_modules
.idea/
6 changes: 5 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] += '&' + timestamp
} else {
request._query = [timestamp]
}

return request
}

Expand Down
26 changes: 23 additions & 3 deletions test/superagent-no-cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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()
}
}
Expand All @@ -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')
Expand All @@ -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])
Expand All @@ -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&[0-9].*$/)
done();
}

/* Mocks */

Expand Down

0 comments on commit 72d97e3

Please sign in to comment.