| 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263 |
1×
| import Request from '../../lib/request';
import Headers from '../../lib/headers';
import FormData from 'isomorphic-form-data';
import { default as chai, expect } from 'chai';
import { default as chaiAsPromised } from 'chai-as-promised';
import { default as _ } from 'lodash';
chai.use(chaiAsPromised);
describe('Request', () => {
describe('constructor', () => {
// https://github.com/whatwg/fetch/blob/010dd7ad85d9bb893c7bbb03e2cbb800068da18e/Overview.html#L3805
it('should throw a TypeError if the input is an instance of Request and input.bodyUsed is true', () => {
const testRequestInstance = new Request('http://example.com');
testRequestInstance.bodyUsed = true;
expect(() => new Request(testRequestInstance))
.to.throw(TypeError, 'Request body already used');
});
// https://github.com/whatwg/fetch/blob/010dd7ad85d9bb893c7bbb03e2cbb800068da18e/Overview.html#L4009
it('should set this.headers to a new instance of Headers with input.headers if the input is an ' +
'instance of Request and init.headers is not set', () => {
const testHeadersObject = {
'Content-Length': 100,
'Accept': '*/*',
'Connection': 'keep-alive',
'User-Agent': 'farfetched/0.0.1',
};
const testHeadersInstance = new Headers(testHeadersObject);
const testRequestInstance = new Request('http://example.com', { headers: testHeadersObject });
expect(new Request(testRequestInstance))
.to.have.property('headers')
.that.eqls(testHeadersInstance);
});
// https://github.com/whatwg/fetch/blob/010dd7ad85d9bb893c7bbb03e2cbb800068da18e/Overview.html#L4013
it('should set this.headers to a new instance of Headers with init.headers if init.headers is ' +
'not undefined', () => {
const testHeadersObject = { 'Content-Length': 333 };
expect(new Request('http://example.com', { headers: testHeadersObject }))
.to.have.deep.property('headers.map')
.that.has.property('content-length')
.that.eqls([ '333' ]);
});
// https://github.com/whatwg/fetch/blob/010dd7ad85d9bb893c7bbb03e2cbb800068da18e/Overview.html#L4102
it('should set this.headers to a new instance of Headers with no arguments if none of the ' +
'preceding conditions are met', () => {
expect(new Request('http://example.com'))
.to.have.property('headers')
.that.is.an.instanceOf(Headers);
});
// https://github.com/whatwg/fetch/blob/010dd7ad85d9bb893c7bbb03e2cbb800068da18e/Overview.html#L4086
it('should set the input bodyUsed property to true if the input is an instance of Request and ' +
'init.body is falsy', () => {
const testRequestInstance = new Request('http://example.com');
new Request(testRequestInstance);
expect(testRequestInstance)
.to.have.property('bodyUsed')
.that.eqls(true);
});
// https://github.com/whatwg/fetch/blob/010dd7ad85d9bb893c7bbb03e2cbb800068da18e/Overview.html#L3892
it('should set this.url to input if input is a string', () => {
const testUrl = 'http://example.com';
expect(new Request(testUrl))
.to.have.property('url')
.that.eqls(testUrl);
});
// https://github.com/whatwg/fetch/blob/010dd7ad85d9bb893c7bbb03e2cbb800068da18e/Overview.html#L3700
it('should throw a TypeError if input is neither a string nor an instance of Request', () => {
expect(() => new Request(new Request('http://example.com')))
.to.not.throw();
expect(() => new Request('http://example.com'))
.to.not.throw();
expect(() => new Request(true))
.to.throw(TypeError);
});
// https://github.com/whatwg/fetch/blob/010dd7ad85d9bb893c7bbb03e2cbb800068da18e/Overview.html#L4041
it('should throw a TypeError if the method is either HEAD or GET and a body is set', () => {
expect(() => new Request('http://example.com', {
method: 'GET',
body: 'test',
}))
.to.throw(TypeError);
expect(() => new Request('http://example.com', {
method: 'POST',
body: 'test',
}))
.to.not.throw();
});
// https://github.com/whatwg/fetch/blob/010dd7ad85d9bb893c7bbb03e2cbb800068da18e/Overview.html#L833
it('should throw a TypeError is init.mode is not \'navigate\', \'same-origin\', \'no-cors\', or \'cors\'', () => {
expect(() => new Request('http://example.com', {
mode: true,
}))
.to.throw(TypeError);
expect(() => new Request('http://example.com', {
mode: 'test',
}))
.to.throw(TypeError);
expect(() => new Request('http://example.com', {
mode: 'navigate',
}))
.to.not.throw();
});
// https://github.com/whatwg/fetch/blob/010dd7ad85d9bb893c7bbb03e2cbb800068da18e/Overview.html#L847
it('should throw a TypeError is init.credentialsMode is not \'omit\', \'same-origin\', or \'include\'', () => {
expect(() => new Request('http://example.com', {
credentialsMode: true,
}))
.to.throw(TypeError);
expect(() => new Request('http://example.com', {
credentialsMode: 'test',
}))
.to.throw(TypeError);
expect(() => new Request('http://example.com', {
credentialsMode: 'omit',
}))
.to.not.throw();
});
// https://github.com/whatwg/fetch/blob/010dd7ad85d9bb893c7bbb03e2cbb800068da18e/Overview.html#L861
it('should throw a TypeError is init.cache is not \'default\', \'no-store\', \'reload\', \'no-cache\', or \'force-cache\'', () => {
expect(() => new Request('http://example.com', {
cacheMode: true,
}))
.to.throw(TypeError);
expect(() => new Request('http://example.com', {
cacheMode: 'test',
}))
.to.throw(TypeError);
expect(() => new Request('http://example.com', {
cacheMode: 'reload',
}))
.to.not.throw();
});
// https://github.com/whatwg/fetch/blob/010dd7ad85d9bb893c7bbb03e2cbb800068da18e/Overview.html#L2529
it(`should default the 'Content-Length' header to the body length`, () => {
const testPayload = new FormData();
testPayload.append('id', '5');
testPayload.append('name', 'charmeleon');
expect(new Request('http://example.com', {
method: 'POST',
body: 'test',
}))
.to.have.deep.property('headers.map')
.to.have.property('content-length')
.that.eqls([ '4' ]);
// Only test content length header in supported contexts
Eif (typeof testPayload.getLengthSync === 'function')
expect(new Request('http://example.com', {
method: 'POST',
body: testPayload,
}))
.to.have.deep.property('headers.map')
.to.have.property('content-length')
.that.eqls([ '271' ]);
});
// https://github.com/whatwg/fetch/blob/010dd7ad85d9bb893c7bbb03e2cbb800068da18e/Overview.html#L1698
it(`should default the 'Accept' header to '*/*'`, () => {
expect(new Request('http://example.com'))
.to.have.deep.property('headers.map')
.to.have.property('accept')
.that.eqls([ '*/*' ]);
});
// https://github.com/whatwg/fetch/blob/010dd7ad85d9bb893c7bbb03e2cbb800068da18e/Overview.html#L2557
it(`should default the 'User-Agent' header to 'farfetched/${ require('../../package.json').version }'`, () => {
expect(new Request('http://example.com'))
.to.have.deep.property('headers.map')
.to.have.property('user-agent')
.that.eqls([ `farfetched/${ require('../../package.json').version }` ]);
});
it(`should default the 'Connection' header to 'keep-alive'`, () => {
expect(new Request('http://example.com'))
.to.have.deep.property('headers.map')
.to.have.property('connection')
.that.eqls([ 'keep-alive' ]);
});
// https://github.com/whatwg/fetch/blob/010dd7ad85d9bb893c7bbb03e2cbb800068da18e/Overview.html#L3551
it(`it should default the 'Content-Type' header to 'multipart/form-data;boundary=' when body is
an instance of FormData`);
// https://github.com/whatwg/fetch/blob/010dd7ad85d9bb893c7bbb03e2cbb800068da18e/Overview.html#L3573
it(`it should default the 'Content-Type' header to 'text/plain;charset=UTF-8' when body is
a string`);
});
describe('clone', () => {
// https://github.com/whatwg/fetch/blob/010dd7ad85d9bb893c7bbb03e2cbb800068da18e/Overview.html#L4154
it('should return a new instance of Request', () => {
const testUrl = 'http://example.com';
const testRequestInstanceA = new Request(testUrl);
const testRequestInstanceB = testRequestInstanceA.clone();
testRequestInstanceA.test = true;
expect(testRequestInstanceB)
.to.be.an.instanceOf(Request)
.that.not.has.property('test');
expect(testRequestInstanceB)
.to.be.an.instanceOf(Request)
.that.has.property('url')
.that.eqls(testUrl);
});
});
});
|