forked from DevExpress/testcafe-hammerhead
-
Notifications
You must be signed in to change notification settings - Fork 1
/
file-protocol-test.js
245 lines (195 loc) · 7.76 KB
/
file-protocol-test.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
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
const {
createSession,
createProxy,
getFileProtocolUrl,
compareCode
} = require('../common/utils');
const {
PAGE_ACCEPT_HEADER
} = require('../common/constants');
const request = require('request-promise-native');
const fs = require('fs');
const os = require('os');
const path = require('path');
const { expect } = require('chai');
describe('file:// protocol', () => {
let session = null;
let proxy = null;
beforeEach(() => {
session = createSession();
proxy = createProxy();
});
afterEach(() => {
proxy.close();
});
it('Should process page and ignore search string', () => {
session.id = 'sessionId';
session.injectable.scripts.push('/script1.js', '/script2.js');
session.injectable.styles.push('/styles1.css', '/styles2.css');
session.useStateSnapshot({
cookies: null,
storages: {
localStorage: '[["key1"],[" \' \\" \\\\ \\n \\t \\b \\f "]]',
sessionStorage: '[["key2"],["value"]]'
}
});
const options = {
url: proxy.openSession(getFileProtocolUrl('./../data/page/src.html') + '?a=1&b=3', session),
headers: {
accept: PAGE_ACCEPT_HEADER
}
};
return request(options)
.then(body => {
// NOTE: The host property is empty in url with file: protocol.
// The expected.html template is used for both tests with http: and file: protocol.
const expected = fs.readFileSync('test/server/data/page/expected.html').toString()
.replace(/(hammerhead\|storage-wrapper\|sessionId\|)127\.0\.0\.1:2000/g, '$1')
.replace('!127.0.0.1%3A2000', '!');
compareCode(body, expected);
});
});
it('Should process stylesheets', () => {
session.id = 'sessionId';
const options = {
url: proxy.openSession(getFileProtocolUrl('./../data/stylesheet/src.css'), session),
headers: {
accept: 'text/css,*/*;q=0.1'
}
};
return request(options)
.then(body => {
const expected = fs.readFileSync('test/server/data/stylesheet/expected.css').toString();
compareCode(body, expected);
});
});
it('Should process page with absolute urls', () => {
session.id = 'sessionId';
const filePostfix = os.platform() === 'win32' ? 'win' : 'nix';
const fileUrl = getFileProtocolUrl('./../data/page-with-file-protocol/src-' + filePostfix + '.html');
const options = {
url: proxy.openSession(fileUrl, session),
headers: {
accept: 'text/html,*/*;q=0.1'
}
};
return request(options)
.then(body => {
const filePath = 'test/server/data/page-with-file-protocol/expected-' + filePostfix + '.html';
const expected = fs.readFileSync(filePath).toString();
compareCode(body, expected);
});
});
if (os.platform() === 'win32') {
it('Should process page with non-conforming Windows url', () => {
session.id = 'sessionId';
const fileUrl = 'file://' + path.join(__dirname, './../data/page-with-file-protocol/src-win.html');
const options = {
url: proxy.openSession(fileUrl, session),
headers: {
accept: 'text/html,*/*;q=0.1'
}
};
return request(options)
.then(body => {
const expected = fs.readFileSync('test/server/data/page-with-file-protocol/expected-win.html').toString();
compareCode(body, expected);
});
});
}
it('Should set the correct content-type header', () => {
session.id = 'sessionId';
const options = {
url: proxy.openSession(getFileProtocolUrl('./../data/images/icons.svg'), session),
resolveWithFullResponse: true,
headers: {
accept: 'image/webp,image/*,*/*;q=0.8'
}
};
return request(options)
.then(res => {
expect(res.headers['content-type']).eql('image/svg+xml');
});
});
it('Should pass an error to the session if target is a directory', done => {
const url = getFileProtocolUrl('./../data');
session.id = 'sessionId';
session.handlePageError = (ctx, err) => {
expect(err).contains([
'Failed to read a file at <a href="' + url + '">' + url + '</a> because of the error:',
'',
'The target of the operation is not a file'
].join('\n'));
ctx.res.end();
done();
return true;
};
const options = {
url: proxy.openSession(url, session),
headers: {
accept: 'text/html,*/*;q=0.1'
}
};
request(options);
});
it('Should pass an error to the session if target does not exist', done => {
const url = getFileProtocolUrl('./../data/non-exist-file');
session.id = 'sessionId';
session.handlePageError = (ctx, err) => {
expect(err).contains([
'Failed to read a file at <a href="' + url + '">' + url + '</a> because of the error:',
'',
'ENOENT'
].join('\n'));
ctx.res.end();
done();
return true;
};
const options = {
url: proxy.openSession(url, session),
headers: {
accept: 'text/html,*/*;q=0.1'
}
};
request(options);
});
it('Should pass an error to the session if target (a file in an "asar" archive) does not exist (GH-2033)', done => {
const url = getFileProtocolUrl('./../data/file-in-asar-archive/directory-looks-like-archive.asar/app.asar/non-existent-dir/non-existent-file.txt');
const archive = path.resolve(__dirname, './../data/file-in-asar-archive/directory-looks-like-archive.asar/app.asar').replace(/\\/g, '/');
const fileName = 'non-existent-dir/non-existent-file.txt';
session.id = 'sessionId';
session.handlePageError = (ctx, err) => {
expect(err).contains([
'Failed to read a file at <a href="' + url + '">' + url + '</a> because of the error:',
'',
'Cannot find the "' + fileName + '" file in the "' + archive + '" archive.'
].join('\n'));
ctx.res.end();
done();
return true;
};
const options = {
url: proxy.openSession(url, session),
headers: {
accept: 'text/html,*/*;q=0.1'
}
};
request(options);
});
it('Should resolve an "asar" archive file and set the correct "content-type" header (GH-2033)', () => {
session.id = 'sessionId';
const fileUrl = getFileProtocolUrl('./../data/file-in-asar-archive/directory-looks-like-archive.asar/app.asar/folder-in-asar-archive/another-folder/src.txt');
const options = {
url: proxy.openSession(fileUrl, session),
resolveWithFullResponse: true,
headers: {
accept: '*/*'
}
};
return request(options)
.then(res => {
expect(res.headers['content-type']).eql('text/plain');
expect(res.body).eql('asar archive file: src.txt');
});
});
});