2014-05-04 08:45:22 +02:00
|
|
|
var tape = require("tape")
|
|
|
|
|
|
2018-01-23 15:09:57 +00:00
|
|
|
var sendRequest = require("../../lib/send-request.js")
|
2014-05-04 08:45:22 +02:00
|
|
|
|
2016-07-10 21:55:47 -07:00
|
|
|
// Polyfill responseURL property into XMLHttpRequest if it doesn't exist,
|
|
|
|
|
// just for the purposes of this test
|
|
|
|
|
// This polyfill is not complete; it won't show the updated location if a
|
|
|
|
|
// redirection occurred, but it's fine for our purposes.
|
2018-01-09 00:44:20 -05:00
|
|
|
if (!("responseURL" in XMLHttpRequest.prototype)) {
|
2016-07-10 21:55:47 -07:00
|
|
|
var nativeOpen = XMLHttpRequest.prototype.open
|
2018-01-09 00:44:20 -05:00
|
|
|
XMLHttpRequest.prototype.open = function(method, url) {
|
2016-07-10 21:55:47 -07:00
|
|
|
this.responseURL = url
|
|
|
|
|
return nativeOpen.apply(this, arguments)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-04 08:45:22 +02:00
|
|
|
tape("test xhr request", function(t) {
|
2018-01-08 17:21:18 -05:00
|
|
|
var url = "https://httpbin.org/get"
|
|
|
|
|
|
2016-06-27 21:46:14 -07:00
|
|
|
t.test("- request is made, gets a result, and is cache-busted", function(t) {
|
2018-01-23 15:09:57 +00:00
|
|
|
var requestCacheBust = sendRequest.bind({
|
2016-06-27 21:46:14 -07:00
|
|
|
options: {
|
2018-02-02 09:52:44 -05:00
|
|
|
cacheBust: true
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-01-08 17:21:18 -05:00
|
|
|
var r = requestCacheBust(url, {}, function(result) {
|
|
|
|
|
t.equal(r.responseURL.indexOf("?"), url.length, "XHR URL is cache-busted when configured to be")
|
2016-06-27 21:46:14 -07:00
|
|
|
try {
|
|
|
|
|
result = JSON.parse(result)
|
|
|
|
|
}
|
|
|
|
|
catch (e) {
|
|
|
|
|
t.fail("xhr doesn't get a JSON response")
|
|
|
|
|
}
|
|
|
|
|
t.same(typeof result, "object", "xhr request get a result")
|
|
|
|
|
t.end()
|
|
|
|
|
})
|
2014-05-04 08:45:22 +02:00
|
|
|
})
|
2016-06-27 21:46:14 -07:00
|
|
|
t.test("- request is not cache-busted when configured not to be", function(t) {
|
2018-01-23 15:09:57 +00:00
|
|
|
var requestNoCacheBust = sendRequest.bind({
|
2016-06-27 21:46:14 -07:00
|
|
|
options: {
|
2018-02-02 09:52:44 -05:00
|
|
|
cacheBust: false
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-01-08 17:21:18 -05:00
|
|
|
var r = requestNoCacheBust(url, {}, function() {
|
|
|
|
|
t.equal(r.responseURL, url, "XHR URL is left untouched")
|
2016-06-27 21:46:14 -07:00
|
|
|
t.end()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
t.end()
|
2014-05-04 08:45:22 +02:00
|
|
|
})
|