-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathoverrides-http.js
189 lines (173 loc) · 6.12 KB
/
overrides-http.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import test from 'ava';
import { mpURL } from './helpers/testedValues';
import TestHelper from './helpers/testHelper';
/**
* @type {TestHelper}
*/
let helper = null;
test.before(async t => {
helper = await TestHelper.init(t);
await helper.initWombat();
});
test.beforeEach(async t => {
t.context.sandbox = helper.sandbox();
t.context.server = helper.server();
t.context.testPage = helper.testPage();
});
test.afterEach.always(async t => {
if (t.title.includes('SharedWorker')) {
await helper.fullRefresh();
} else {
await helper.ensureSandbox();
}
});
test.after.always(async t => {
await helper.stop();
});
test('XMLHttpRequest: should rewrite the URL argument of "open"', async t => {
const { sandbox, server } = t.context;
const response = await sandbox.evaluate(async () => {
let reqDone;
let to;
const prom = new Promise(resolve => {
reqDone = resolve;
to = setTimeout(() => resolve(false), 5000);
});
const onLoad = () => {
clearTimeout(to);
reqDone(true);
};
const xhr = new XMLHttpRequest();
xhr.addEventListener('load', onLoad);
xhr.open('GET', '/test');
xhr.send();
const loaded = await prom;
if (!loaded) throw new Error('no reply from server in 5 seconds');
return JSON.parse(xhr.responseText);
});
t.is(response.headers['x-pywb-requested-with'], 'XMLHttpRequest');
t.is(response.url, '/live/20180803160549mp_/https://tests.wombat.io/test');
});
test('XMLHttpRequest: should rewrite the "responseURL" property', async t => {
const { sandbox, server } = t.context;
const result = await sandbox.evaluate(async () => {
let reqDone;
let to;
const prom = new Promise(resolve => {
reqDone = resolve;
to = setTimeout(() => resolve(false), 5000);
});
const onLoad = () => {
clearTimeout(to);
reqDone(true);
};
const xhr = new XMLHttpRequest();
xhr.addEventListener('load', onLoad);
xhr.open('GET', '/test');
xhr.send();
const loaded = await prom;
if (!loaded) throw new Error('no reply from server in 5 seconds');
return xhr.responseURL === 'https://tests.wombat.io/test';
});
t.true(result);
});
test('fetch: should rewrite the input argument when it is a string (URL), also return original', async t => {
const { sandbox, server } = t.context;
const result = await sandbox.evaluate(async () => {
let to;
let response = await Promise.race([
fetch('/test'),
new Promise(resolve => {
to = setTimeout(() => resolve('timed out'), 5000);
})
]);
if (response === 'timed out')
throw new Error('no reply from server in 5 seconds');
clearTimeout(to);
const data = await response.json();
return {url: data.url, respURL: response.url};
});
t.is(result.url, '/live/20180803160549mp_/https://tests.wombat.io/test');
t.is(result.respURL, 'https://tests.wombat.io/test');
});
test('fetch: should rewrite the input argument when it is an Request object, also return original', async t => {
const { sandbox, server } = t.context;
const result = await sandbox.evaluate(async () => {
let to;
let request = new Request('/test', {
method: 'GET'
});
let response = await Promise.race([
fetch(request),
new Promise(resolve => {
to = setTimeout(() => resolve('timed out'), 5000);
})
]);
if (response === 'timed out')
throw new Error('no reply from server in 5 seconds');
clearTimeout(to);
const data = await response.json();
return {url: data.url,
rwUrl: request.url,
respURL: response.url};
});
t.is(result.rwUrl, 'https://tests.wombat.io/test');
t.is(result.url, '/live/20180803160549mp_/https://tests.wombat.io/test');
t.is(result.respURL, 'https://tests.wombat.io/test');
});
test('fetch: Request.referrer is rewritten across multiple copies of Request', async t => {
const { sandbox, server } = t.context;
const result = await sandbox.evaluate(async () => {
let to;
let A = new Request('/test', {'referrer': '/abc'});
let B = new Request('/test', A);
let C = new Request(B, {'referrer': '/xyz'});
let D = new Request({'url': '/test', 'referrer': '/cde'}, C);
D.__WB_no_unrewrite = true;
return {A: A.referrer,
B: B.referrer,
C: C.referrer,
D: D.referrer
};
});
t.is(result.A, 'https://tests.wombat.io/abc');
t.is(result.B, 'https://tests.wombat.io/abc');
t.is(result.C, 'https://tests.wombat.io/xyz');
t.is(result.D, 'http://localhost:3030/live/20180803160549mp_/https://tests.wombat.io/xyz');
});
test('fetch: should rewrite the input argument when it is a object, but return original', async t => {
const { sandbox, server } = t.context;
const result = await sandbox.evaluate(async () => {
let to;
let response = await Promise.race([
fetch({ href: '/test', toString: () => { return '/test'; }}),
new Promise(resolve => {
to = setTimeout(() => resolve('timed out'), 10000);
})
]);
if (response === 'timed out')
throw new Error('no reply from server in 10 seconds');
clearTimeout(to);
const data = await response.json();
return {url: data.url, respURL: response.url};
});
t.is(result.url, '/live/20180803160549mp_/https://tests.wombat.io/test');
t.is(result.respURL, 'https://tests.wombat.io/test');
});
test('Request: should rewrite the input argument to the constructor when it is a string (URL)', async t => {
const { sandbox, server } = t.context;
const result = await sandbox.evaluate(() => {
const req = new Request('/test', { method: 'GET' });
return {url: req.url};
});
t.is(result.url, 'https://tests.wombat.io/test');
});
test('Request: should rewrite the input argument to the constructor when it is an object with a url property', async t => {
const { sandbox, server } = t.context;
const result = await sandbox.evaluate(() => {
const req = new Request({ url: '/test' }, { method: 'GET', referrer: 'https://example.com/' });
return {url: req.url, referrer: req.referrer};
});
t.is(result.url, 'https://tests.wombat.io/test');
t.is(result.referrer, 'https://example.com/');
});