2019-03-03 01:37:45 -05:00
|
|
|
var tape = require("tape");
|
2014-05-04 08:45:22 +02:00
|
|
|
|
2019-03-03 01:37:45 -05: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)) {
|
2019-03-03 01:37:45 -05:00
|
|
|
var nativeOpen = XMLHttpRequest.prototype.open;
|
2018-01-09 00:44:20 -05:00
|
|
|
XMLHttpRequest.prototype.open = function(method, url) {
|
2019-03-03 01:37:45 -05:00
|
|
|
this.responseURL = url;
|
|
|
|
|
return nativeOpen.apply(this, arguments);
|
|
|
|
|
};
|
2016-07-10 21:55:47 -07:00
|
|
|
}
|
|
|
|
|
|
2014-05-04 08:45:22 +02:00
|
|
|
tape("test xhr request", function(t) {
|
2019-03-03 01:37:45 -05:00
|
|
|
var url = "https://httpbin.org/get";
|
2018-01-08 17:21:18 -05:00
|
|
|
|
2016-06-27 21:46:14 -07:00
|
|
|
t.test("- request is made, gets a result, and is cache-busted", function(t) {
|
2019-03-03 01:37:45 -05:00
|
|
|
var r = sendRequest(url, { cacheBust: true }, 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 {
|
2019-03-03 01:37:45 -05:00
|
|
|
result = JSON.parse(result);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
t.fail("xhr doesn't get a JSON response");
|
2016-06-27 21:46:14 -07:00
|
|
|
}
|
2019-03-03 01:37:45 -05:00
|
|
|
t.same(typeof result, "object", "xhr request get a result");
|
|
|
|
|
t.end();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
t.test("- request is not cache-busted when configured not to be", function(
|
|
|
|
|
t
|
|
|
|
|
) {
|
2018-03-06 10:06:38 +00:00
|
|
|
var r = sendRequest(url, {}, function() {
|
2019-03-03 01:37:45 -05:00
|
|
|
t.equal(r.responseURL, url, "XHR URL is left untouched");
|
|
|
|
|
t.end();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
t.end();
|
|
|
|
|
});
|
2018-03-20 11:40:49 -04:00
|
|
|
|
|
|
|
|
tape("request headers are sent properly", function(t) {
|
2019-03-03 01:37:45 -05:00
|
|
|
var url = "https://httpbin.org/headers";
|
2018-03-20 11:40:49 -04:00
|
|
|
var options = {
|
|
|
|
|
selectors: ["div.pjax", "div.container"]
|
2019-03-03 01:37:45 -05:00
|
|
|
};
|
2018-03-20 11:40:49 -04:00
|
|
|
|
|
|
|
|
sendRequest(url, options, function(responseText) {
|
2019-03-03 01:37:45 -05:00
|
|
|
var headers = JSON.parse(responseText).headers;
|
2018-03-20 11:40:49 -04:00
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
t.equals(
|
|
|
|
|
headers["X-Requested-With"],
|
|
|
|
|
"XMLHttpRequest",
|
|
|
|
|
"X-Requested-With header is set correctly"
|
|
|
|
|
);
|
2018-03-20 11:40:49 -04:00
|
|
|
// Httpbin.org changes the case to 'X-Pjax'
|
2019-03-03 01:37:45 -05:00
|
|
|
t.equals(headers["X-Pjax"], "true", "X-PJAX header is set correctly");
|
|
|
|
|
t.equals(
|
|
|
|
|
headers["X-Pjax-Selectors"],
|
|
|
|
|
'["div.pjax","div.container"]',
|
|
|
|
|
"X-PJAX-Selectors header is set correctly"
|
|
|
|
|
);
|
2018-03-20 11:40:49 -04:00
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
t.end();
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-03-20 11:40:49 -04:00
|
|
|
|
2018-04-09 23:36:32 -04:00
|
|
|
tape("HTTP status codes other than 200 are handled properly", function(t) {
|
2019-03-03 01:37:45 -05:00
|
|
|
var url = "https://httpbin.org/status/400";
|
2018-04-09 23:36:32 -04:00
|
|
|
|
|
|
|
|
sendRequest(url, {}, function(responseText, request) {
|
2019-03-03 01:37:45 -05:00
|
|
|
t.equals(responseText, null, "responseText is null");
|
|
|
|
|
t.equals(request.status, 400, "HTTP status code is correct");
|
2018-04-09 23:36:32 -04:00
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
t.end();
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-04-09 23:36:32 -04:00
|
|
|
|
|
|
|
|
tape.skip("XHR error is handled properly", function(t) {
|
2019-03-03 01:37:45 -05:00
|
|
|
var url = "https://encrypted.google.com/foobar";
|
2018-04-09 23:36:32 -04:00
|
|
|
|
|
|
|
|
sendRequest(url, {}, function(responseText) {
|
2019-03-03 01:37:45 -05:00
|
|
|
t.equals(responseText, null, "responseText is null");
|
2018-04-09 23:36:32 -04:00
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
t.end();
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-04-09 23:36:32 -04:00
|
|
|
|
|
|
|
|
tape("POST body data is sent properly", function(t) {
|
2019-03-03 01:37:45 -05:00
|
|
|
var url = "https://httpbin.org/post";
|
|
|
|
|
var params = [
|
|
|
|
|
{
|
|
|
|
|
name: "test",
|
|
|
|
|
value: "1"
|
|
|
|
|
}
|
|
|
|
|
];
|
2018-04-09 23:36:32 -04:00
|
|
|
var options = {
|
|
|
|
|
requestOptions: {
|
|
|
|
|
requestMethod: "POST",
|
|
|
|
|
requestParams: params
|
|
|
|
|
}
|
2019-03-03 01:37:45 -05:00
|
|
|
};
|
2018-04-09 23:36:32 -04:00
|
|
|
|
|
|
|
|
sendRequest(url, options, function(responseText) {
|
2019-03-03 01:37:45 -05:00
|
|
|
var response = JSON.parse(responseText);
|
|
|
|
|
|
|
|
|
|
t.same(
|
|
|
|
|
response.form[params[0].name],
|
|
|
|
|
params[0].value,
|
|
|
|
|
"requestParams were sent properly"
|
|
|
|
|
);
|
|
|
|
|
t.equals(
|
|
|
|
|
response.headers["Content-Type"],
|
|
|
|
|
"application/x-www-form-urlencoded",
|
|
|
|
|
"Content-Type header was set properly"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-04-09 23:36:32 -04:00
|
|
|
|
|
|
|
|
tape("GET query data is sent properly", function(t) {
|
2019-03-03 01:37:45 -05:00
|
|
|
var url = "https://httpbin.org/get";
|
|
|
|
|
var params = [
|
|
|
|
|
{
|
|
|
|
|
name: "test",
|
|
|
|
|
value: "1"
|
|
|
|
|
}
|
|
|
|
|
];
|
2018-04-09 23:36:32 -04:00
|
|
|
var options = {
|
|
|
|
|
requestOptions: {
|
|
|
|
|
requestParams: params
|
|
|
|
|
}
|
2019-03-03 01:37:45 -05:00
|
|
|
};
|
2018-04-09 23:36:32 -04:00
|
|
|
|
|
|
|
|
sendRequest(url, options, function(responseText) {
|
2019-03-03 01:37:45 -05:00
|
|
|
var response = JSON.parse(responseText);
|
2018-04-09 23:36:32 -04:00
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
t.same(
|
|
|
|
|
response.args[params[0].name],
|
|
|
|
|
params[0].value,
|
|
|
|
|
"requestParams were sent properly"
|
|
|
|
|
);
|
2018-04-09 23:36:32 -04:00
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
t.end();
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-04-09 23:36:32 -04:00
|
|
|
|
|
|
|
|
tape("XHR timeout is handled properly", function(t) {
|
2019-03-03 01:37:45 -05:00
|
|
|
var url = "https://httpbin.org/delay/5";
|
2018-04-09 23:36:32 -04:00
|
|
|
var options = {
|
|
|
|
|
timeout: 1000
|
2019-03-03 01:37:45 -05:00
|
|
|
};
|
2018-04-09 23:36:32 -04:00
|
|
|
|
|
|
|
|
sendRequest(url, options, function(responseText) {
|
2019-03-03 01:37:45 -05:00
|
|
|
t.equals(responseText, null, "responseText is null");
|
2018-04-09 23:36:32 -04:00
|
|
|
|
2019-03-03 01:37:45 -05:00
|
|
|
t.end();
|
|
|
|
|
});
|
|
|
|
|
});
|