-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathfetch.js
More file actions
100 lines (90 loc) · 3.48 KB
/
fetch.js
File metadata and controls
100 lines (90 loc) · 3.48 KB
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
import { serveTest } from '../test-server.js';
import { strictEqual, deepStrictEqual, throws } from '../../assert.js';
export const handler = serveTest(async (t) => {
await t.test('headers-non-ascii-latin1-field-value', async () => {
const response = await fetch("https://http-me.glitch.me/meow?header=cat:é");
strictEqual(response.headers.get('cat'), "é");
});
t.test('request-clone-bad-calls', () => {
throws(() => new Request.prototype.clone(), TypeError);
throws(() => new Request.prototype.clone.call(undefined), TypeError);
});
await t.test('request-clone-valid', async () => {
{
const request = new Request('https://www.fastly.com', {
headers: {
hello: 'world'
},
body: 'te',
method: 'post'
});
const newRequest = request.clone();
strictEqual(newRequest instanceof Request, true, 'newRequest instanceof Request');
strictEqual(newRequest.method, request.method, 'newRequest.method');
strictEqual(newRequest.url, request.url, 'newRequest.url');
deepStrictEqual([...newRequest.headers], [...request.headers], 'newRequest.headers');
strictEqual(request.bodyUsed, false, 'request.bodyUsed');
strictEqual(newRequest.bodyUsed, false, 'newRequest.bodyUsed');
strictEqual(newRequest.body instanceof ReadableStream, true, 'newRequest.body instanceof ReadableStream');
}
{
const request = new Request('https://www.fastly.com', {
method: 'get'
})
const newRequest = request.clone();
strictEqual(newRequest.bodyUsed, false, 'newRequest.bodyUsed');
strictEqual(newRequest.body, null, 'newRequest.body');
}
});
await t.test('request-clone-invalid', async () => {
const request = new Request('https://www.fastly.com', {
headers: {
hello: 'world'
},
body: 'te',
method: 'post'
});
await request.text();
throws(() => request.clone());
});
t.test('response-clone-bad-calls', () => {
throws(() => new Response.prototype.clone(), TypeError);
throws(() => new Response.prototype.clone.call(undefined), TypeError);
});
await t.test('response-clone-valid', async () => {
{
const response = new Response('test body', {
headers: {
hello: 'world'
},
status: 200,
statusText: 'Success'
});
const newResponse = response.clone();
strictEqual(newResponse instanceof Response, true, 'newResponse instanceof Request');
strictEqual(response.bodyUsed, false, 'response.bodyUsed');
strictEqual(newResponse.bodyUsed, false, 'newResponse.bodyUsed');
deepStrictEqual([...newResponse.headers], [...response.headers], 'newResponse.headers');
strictEqual(newResponse.status, 200, 'newResponse.status');
strictEqual(newResponse.statusText, 'Success', 'newResponse.statusText');
strictEqual(newResponse.body instanceof ReadableStream, true, 'newResponse.body instanceof ReadableStream');
}
{
const response = new Response(null, {
status: 404,
statusText: "Not found",
});
const newResponse = response.clone();
strictEqual(newResponse.bodyUsed, false, 'newResponse.bodyUsed');
strictEqual(newResponse.body, null, 'newResponse.body');
}
});
await t.test('response-clone-invalid', async () => {
const response = new Response('test body', {
status: 200,
statusText: "Success"
});
await response.text();
throws(() => response.clone());
});
});