Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80538 views
1
global.window = global;
2
global.location = {
3
host: 'localhost:8081',
4
port: 8081,
5
protocol: 'http:'
6
};
7
8
var noop = function() {};
9
global.XMLHttpRequest = function() {
10
this.open = noop;
11
this.send = noop;
12
};
13
14
global.FormData = function () {};
15
global.Blob = function () {};
16
global.ArrayBuffer = function () {};
17
18
var test = require('tape').test;
19
var http = require('../index.js');
20
21
22
test('Test simple url string', function(t) {
23
var url = { path: '/api/foo' };
24
var request = http.get(url, noop);
25
26
t.equal( request.uri, 'http://localhost:8081/api/foo', 'Url should be correct');
27
t.end();
28
29
});
30
31
32
test('Test full url object', function(t) {
33
var url = {
34
host: "localhost:8081",
35
hostname: "localhost",
36
href: "http://localhost:8081/api/foo?bar=baz",
37
method: "GET",
38
path: "/api/foo?bar=baz",
39
pathname: "/api/foo",
40
port: "8081",
41
protocol: "http:",
42
query: "bar=baz",
43
search: "?bar=baz",
44
slashes: true
45
};
46
47
var request = http.get(url, noop);
48
49
t.equal( request.uri, 'http://localhost:8081/api/foo?bar=baz', 'Url should be correct');
50
t.end();
51
52
});
53
54
test('Test alt protocol', function(t) {
55
var params = {
56
protocol: "foo:",
57
hostname: "localhost",
58
port: "3000",
59
path: "/bar"
60
};
61
62
var request = http.get(params, noop);
63
64
t.equal( request.uri, 'foo://localhost:3000/bar', 'Url should be correct');
65
t.end();
66
67
});
68
69
test('Test string as parameters', function(t) {
70
var url = '/api/foo';
71
var request = http.get(url, noop);
72
73
t.equal( request.uri, 'http://localhost:8081/api/foo', 'Url should be correct');
74
t.end();
75
76
});
77
78
test('Test withCredentials param', function(t) {
79
var url = '/api/foo';
80
81
var request = http.request({ url: url, withCredentials: false }, noop);
82
t.equal( request.xhr.withCredentials, false, 'xhr.withCredentials should be false');
83
84
var request = http.request({ url: url, withCredentials: true }, noop);
85
t.equal( request.xhr.withCredentials, true, 'xhr.withCredentials should be true');
86
87
var request = http.request({ url: url }, noop);
88
t.equal( request.xhr.withCredentials, true, 'xhr.withCredentials should be true');
89
90
t.end();
91
});
92
93
test('Test POST XHR2 types', function(t) {
94
t.plan(3);
95
var url = '/api/foo';
96
97
var request = http.request({ url: url, method: 'POST' }, noop);
98
request.xhr.send = function (data) {
99
t.ok(data instanceof global.ArrayBuffer, 'data should be instanceof ArrayBuffer');
100
};
101
request.end(new global.ArrayBuffer());
102
103
request = http.request({ url: url, method: 'POST' }, noop);
104
request.xhr.send = function (data) {
105
t.ok(data instanceof global.Blob, 'data should be instanceof Blob');
106
};
107
request.end(new global.Blob());
108
109
request = http.request({ url: url, method: 'POST' }, noop);
110
request.xhr.send = function (data) {
111
t.ok(data instanceof global.FormData, 'data should be instanceof FormData');
112
};
113
request.end(new global.FormData());
114
});
115
116