-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathrequest_url.js
166 lines (124 loc) · 4.09 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
global.window = global;
global.location = {
host: 'localhost:8081',
port: 8081,
protocol: 'http:'
};
var noop = function() {};
global.XMLHttpRequest = function() {
this.open = noop;
this.send = noop;
};
global.FormData = function () {};
global.Blob = function () {};
global.ArrayBuffer = function () {};
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 alt protocol', function(t) {
var params = {
protocol: "foo:",
hostname: "localhost",
port: "3000",
path: "/bar"
};
var request = http.get(params, noop);
t.equal( request.uri, 'foo://localhost:3000/bar', '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 POST XHR2 types', function(t) {
t.plan(3);
var url = '/api/foo';
var request = http.request({ url: url, method: 'POST' }, noop);
request.xhr.send = function (data) {
t.ok(data instanceof global.ArrayBuffer, 'data should be instanceof ArrayBuffer');
};
request.end(new global.ArrayBuffer());
request = http.request({ url: url, method: 'POST' }, noop);
request.xhr.send = function (data) {
t.ok(data instanceof global.Blob, 'data should be instanceof Blob');
};
request.end(new global.Blob());
request = http.request({ url: url, method: 'POST' }, noop);
request.xhr.send = function (data) {
t.ok(data instanceof global.FormData, 'data should be instanceof FormData');
};
request.end(new global.FormData());
});
test('Test setTimeout sets xhr timeout and ontimeout', function(t) {
var url = '/api/foo';
var request = http.request({ url: url, method: 'POST' }, noop);
request.setTimeout( 999, function(){});
t.equal( request.xhr.timeout, 999);
t.ok( request.xhr.ontimeout, 'Make sure ontimeout is a function');
t.end();
});
test('Test setTimeout will execute callback after timeout', function(t) {
var url = '/api/foo';
var request = http.request({ url: url, method: 'POST' }, noop);
var abortSend;
t.plan(1);
// send query handler function
var onSend = function (data) {
t.fail('setTimeout should have cancelled the request');
};
// timeout callback function
var onTimeout = function(){
clearTimeout( abortSend);
t.pass('timeout called');
};
// simulate a slow-executing query
request.xhr.send = function( data) {
// execute the query after a delay
abortSend = setTimeout(
onSend.bind( this, data), 1000
);
// simulate xhr ontimeout, normally this
// gets done somewhere in the browser
if (this.timeout && this.ontimeout) {
setTimeout( this.ontimeout, this.timeout);
}
}
// setup a timeout that will abort before query fires
request.setTimeout( 200, onTimeout);
request.end();
});