-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathrequest_url.js
94 lines (70 loc) · 2.21 KB
/
request_url.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
global.window = {
location: {
host: 'localhost:8081',
port: 8081,
protocol: 'http:'
}
};
var noop = function() {};
global.window.XMLHttpRequest = function(xhrParams) {
this.open = noop;
this.send = noop;
if(!xhrParams) return;
Object.keys(xhrParams).forEach(function(key) {
this[key] = xhrParams[key];
}.bind(this));
};
var test = require('tape').test;
var http = require('../index.js');
test('Test simple url string', function(t) {
var url = { path: '/api/foo' };
var request = http.get(url, noop);
t.equal( request.uri, 'http://localhost:8081/api/foo', 'Url should be correct');
t.end();
});
test('Test full url object', function(t) {
var url = {
host: "localhost:8081",
hostname: "localhost",
href: "http://localhost:8081/api/foo?bar=baz",
method: "GET",
path: "/api/foo?bar=baz",
pathname: "/api/foo",
port: "8081",
protocol: "http:",
query: "bar=baz",
search: "?bar=baz",
slashes: true
};
var request = http.get(url, noop);
t.equal( request.uri, 'http://localhost:8081/api/foo?bar=baz', 'Url should be correct');
t.end();
});
test('Test string as parameters', function(t) {
var url = '/api/foo';
var request = http.get(url, noop);
t.equal( request.uri, 'http://localhost:8081/api/foo', 'Url should be correct');
t.end();
});
test('Test withCredentials param', function(t) {
var url = '/api/foo';
var request = http.request({ url: url, withCredentials: false }, noop);
t.equal( request.xhr.withCredentials, false, 'xhr.withCredentials should be false');
var request = http.request({ url: url, withCredentials: true }, noop);
t.equal( request.xhr.withCredentials, true, 'xhr.withCredentials should be true');
var request = http.request({ url: url }, noop);
t.equal( request.xhr.withCredentials, true, 'xhr.withCredentials should be true');
t.end();
});
test('Test additional xhr params', function(t) {
var request = http.request({
url: '/api/foo',
xhrParams: {
mozSystem: true,
mozAnon: true
}
});
t.equal( request.xhr.mozSystem, true, 'xhr.mozSystem should be true');
t.equal( request.xhr.mozAnon, true, 'mozAnon should be true');
t.end();
});