/
karma.spec.js
436 lines (360 loc) · 13.2 KB
/
karma.spec.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import helper from '../../../lib/helper'
import constants from '../../../lib/constants'
import File from '../../../lib/file'
import Url from '../../../lib/url'
import mocks from 'mocks'
var HttpResponseMock = mocks.http.ServerResponse
var HttpRequestMock = mocks.http.ServerRequest
describe('middleware.karma', () => {
var serveFile
var filesDeferred
var nextSpy
var response
var MockFile = function (path, sha) {
File.call(this, path)
this.sha = sha || 'sha-default'
}
var fsMock = mocks.fs.create({
karma: {
static: {
'client.html': mocks.fs.file(0, 'CLIENT HTML\n%X_UA_COMPATIBLE%%X_UA_COMPATIBLE_URL%'),
'context.html': mocks.fs.file(0, 'CONTEXT\n%SCRIPTS%'),
'debug.html': mocks.fs.file(0, 'DEBUG\n%SCRIPTS%\n%X_UA_COMPATIBLE%'),
'karma.js': mocks.fs.file(0, 'root: %KARMA_URL_ROOT%, v: %KARMA_VERSION%')
}
}
})
var createServeFile = require('../../../lib/middleware/common').createServeFile
var createKarmaMiddleware = require('../../../lib/middleware/karma').create
var handler = serveFile = filesDeferred = nextSpy = response = null
var clientConfig = {foo: 'bar'}
var injector = {
get (val) {
switch (val) {
case 'config.client':
return clientConfig
default:
return null
}
}
}
beforeEach(() => {
nextSpy = sinon.spy()
response = new HttpResponseMock()
filesDeferred = helper.defer()
serveFile = createServeFile(fsMock, '/karma/static')
handler = createKarmaMiddleware(
filesDeferred.promise,
serveFile,
null,
injector,
'/base/path',
'/__karma__/'
)
})
// helpers
var includedFiles = (files) => {
return filesDeferred.resolve({included: files, served: []})
}
var servedFiles = (files) => {
return filesDeferred.resolve({included: [], served: files})
}
var normalizedHttpRequest = (urlPath) => {
var req = new HttpRequestMock(urlPath)
req.normalizedUrl = req.url
return req
}
var callHandlerWith = function (urlPath, next) {
var promise = handler(normalizedHttpRequest(urlPath), response, next || nextSpy)
if (promise && promise.done) promise.done()
}
it('should redirect urlRoot without trailing slash', (done) => {
response.once('end', () => {
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs(301, 'MOVED PERMANENTLY')
expect(response._headers['Location']).to.equal('/__karma__/')
done()
})
callHandlerWith('/__karma__')
})
it('should not serve outside of urlRoot', () => {
handler(normalizedHttpRequest('/'), null, nextSpy)
expect(nextSpy).to.have.been.called
nextSpy.reset()
handler(normalizedHttpRequest('/client.html'), null, nextSpy)
expect(nextSpy).to.have.been.called
nextSpy.reset()
handler(normalizedHttpRequest('/debug.html'), null, nextSpy)
expect(nextSpy).to.have.been.called
nextSpy.reset()
handler(normalizedHttpRequest('/context.html'), null, nextSpy)
expect(nextSpy).to.have.been.called
})
it('should serve client.html', (done) => {
handler = createKarmaMiddleware(
null,
serveFile,
null,
injector,
'/base',
'/'
)
response.once('end', () => {
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs(200, 'CLIENT HTML')
done()
})
callHandlerWith('/')
})
it('should serve /?id=xxx', (done) => {
handler = createKarmaMiddleware(
null,
serveFile,
null,
injector,
'/base',
'/'
)
response.once('end', () => {
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs(200, 'CLIENT HTML')
done()
})
callHandlerWith('/?id=123')
})
it('should serve /?x-ua-compatible with replaced values', (done) => {
handler = createKarmaMiddleware(
null,
serveFile,
null,
injector,
'/base',
'/'
)
response.once('end', () => {
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs(200, 'CLIENT HTML\n<meta http-equiv="X-UA-Compatible" content="xxx=yyy"/>?x-ua-compatible=xxx%3Dyyy')
done()
})
callHandlerWith('/?x-ua-compatible=xxx%3Dyyy')
})
it('should serve debug.html/?x-ua-compatible with replaced values', (done) => {
includedFiles([])
response.once('end', () => {
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs(200, 'DEBUG\n\n<meta http-equiv="X-UA-Compatible" content="xxx=yyy"/>')
done()
})
callHandlerWith('/__karma__/debug.html?x-ua-compatible=xxx%3Dyyy')
})
it('should serve karma.js with version and urlRoot variables', (done) => {
response.once('end', () => {
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs(200, 'root: /__karma__/, v: ' + constants.VERSION)
expect(response._headers['Content-Type']).to.equal('application/javascript')
done()
})
callHandlerWith('/__karma__/karma.js')
})
it('should serve context.html with replaced script tags', (done) => {
includedFiles([
new MockFile('/first.js', 'sha123'),
new MockFile('/second.dart', 'sha456')
])
response.once('end', () => {
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs(200, 'CONTEXT\n<script type="text/javascript" src="/__karma__/absolute/first.js?sha123" crossorigin="anonymous"></script>\n<script type="application/dart" src="/__karma__/absolute/second.dart?sha456" crossorigin="anonymous"></script>')
done()
})
callHandlerWith('/__karma__/context.html')
})
it('should serve context.html with replaced link tags', (done) => {
includedFiles([
new MockFile('/first.css', 'sha007'),
new MockFile('/second.html', 'sha678')
])
response.once('end', () => {
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs(200, 'CONTEXT\n<link type="text/css" href="/__karma__/absolute/first.css?sha007" rel="stylesheet">\n<link href="/__karma__/absolute/second.html?sha678" rel="import">')
done()
})
callHandlerWith('/__karma__/context.html')
})
it('should serve context.html with the correct path for the script tags', (done) => {
includedFiles([
new MockFile('/some/abc/a.js', 'sha'),
new MockFile('/base/path/b.js', 'shaaa')
])
response.once('end', () => {
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs(200, 'CONTEXT\n<script type="text/javascript" src="/__karma__/absolute/some/abc/a.js?sha" crossorigin="anonymous"></script>\n<script type="text/javascript" src="/__karma__/base/b.js?shaaa" crossorigin="anonymous"></script>')
done()
})
callHandlerWith('/__karma__/context.html')
})
it('should serve context.html with the correct path for link tags', (done) => {
includedFiles([
new MockFile('/some/abc/a.css', 'sha1'),
new MockFile('/base/path/b.css', 'sha2'),
new MockFile('/some/abc/c.html', 'sha3'),
new MockFile('/base/path/d.html', 'sha4')
])
response.once('end', () => {
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs(200, 'CONTEXT\n<link type="text/css" href="/__karma__/absolute/some/abc/a.css?sha1" rel="stylesheet">\n<link type="text/css" href="/__karma__/base/b.css?sha2" rel="stylesheet">\n<link href="/__karma__/absolute/some/abc/c.html?sha3" rel="import">\n<link href="/__karma__/base/d.html?sha4" rel="import">')
done()
})
callHandlerWith('/__karma__/context.html')
})
it('should serve context.json with the correct paths for all files', (done) => {
includedFiles([
new MockFile('/some/abc/a.css', 'sha1'),
new MockFile('/base/path/b.css', 'sha2'),
new MockFile('/some/abc/c.html', 'sha3'),
new MockFile('/base/path/d.html', 'sha4')
])
response.once('end', () => {
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs(200, JSON.stringify({
files: [
'/__karma__/absolute/some/abc/a.css?sha1',
'/__karma__/base/b.css?sha2',
'/__karma__/absolute/some/abc/c.html?sha3',
'/__karma__/base/d.html?sha4'
]
}))
done()
})
callHandlerWith('/__karma__/context.json')
})
it('should not change urls', (done) => {
includedFiles([
new Url('http://some.url.com/whatever')
])
response.once('end', () => {
expect(response).to.beServedAs(200, 'CONTEXT\n<script type="text/javascript" src="http://some.url.com/whatever" crossorigin="anonymous"></script>')
done()
})
callHandlerWith('/__karma__/context.html')
})
it('should send non-caching headers for context.html', (done) => {
var ZERO_DATE = (new Date(0)).toUTCString()
includedFiles([])
response.once('end', () => {
expect(nextSpy).not.to.have.been.called
expect(response._headers['Cache-Control']).to.equal('no-cache')
// idiotic IE8 needs more
expect(response._headers['Pragma']).to.equal('no-cache')
expect(response._headers['Expires']).to.equal(ZERO_DATE)
done()
})
callHandlerWith('/__karma__/context.html')
})
it('should inline mappings with all served files', (done) => {
fsMock._touchFile('/karma/static/context.html', 0, '%MAPPINGS%')
servedFiles([
new MockFile('/some/abc/a.js', 'sha_a'),
new MockFile('/base/path/b.js', 'sha_b'),
new MockFile('\\windows\\path\\uuu\\c.js', 'sha_c')
])
response.once('end', () => {
expect(response).to.beServedAs(200, "window.__karma__.files = {\n '/__karma__/absolute/some/abc/a.js': 'sha_a',\n '/__karma__/base/b.js': 'sha_b',\n '/__karma__/absolute\\\\windows\\\\path\\\\uuu\\\\c.js': 'sha_c'\n};\n")
done()
})
callHandlerWith('/__karma__/context.html')
})
it('should escape quotes in mappings with all served files', (done) => {
fsMock._touchFile('/karma/static/context.html', 0, '%MAPPINGS%')
servedFiles([
new MockFile("/some/abc/a'b.js", 'sha_a'),
new MockFile('/base/path/ba.js', 'sha_b')
])
response.once('end', () => {
expect(response).to.beServedAs(200, 'window.__karma__.files = {\n \'/__karma__/absolute/some/abc/a\\\'b.js\': \'sha_a\',\n \'/__karma__/base/ba.js\': \'sha_b\'\n};\n')
done()
})
callHandlerWith('/__karma__/context.html')
})
it('should serve debug.html with replaced script tags without timestamps', (done) => {
includedFiles([
new MockFile('/first.js'),
new MockFile('/base/path/b.js')
])
response.once('end', () => {
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs(200, 'DEBUG\n<script type="text/javascript" src="/__karma__/absolute/first.js" crossorigin="anonymous"></script>\n<script type="text/javascript" src="/__karma__/base/b.js" crossorigin="anonymous"></script>')
done()
})
callHandlerWith('/__karma__/debug.html')
})
it('should serve debug.html with replaced link tags without timestamps', (done) => {
includedFiles([
new MockFile('/first.css'),
new MockFile('/base/path/b.css'),
new MockFile('/second.html'),
new MockFile('/base/path/d.html')
])
response.once('end', () => {
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs(200, 'DEBUG\n<link type="text/css" href="/__karma__/absolute/first.css" rel="stylesheet">\n<link type="text/css" href="/__karma__/base/b.css" rel="stylesheet">\n<link href="/__karma__/absolute/second.html" rel="import">\n<link href="/__karma__/base/d.html" rel="import">')
done()
})
callHandlerWith('/__karma__/debug.html')
})
it('should inline client config to debug.html', (done) => {
includedFiles([
new MockFile('/first.js')
])
fsMock._touchFile('/karma/static/debug.html', 1, '%CLIENT_CONFIG%')
response.once('end', () => {
expect(response).to.beServedAs(200, 'window.__karma__.config = {"foo":"bar"};\n')
done()
})
callHandlerWith('/__karma__/debug.html')
})
it('should not serve other files even if they are in urlRoot', (done) => {
includedFiles([])
callHandlerWith('/__karma__/something/else.js', () => {
expect(response).to.beNotServed()
done()
})
})
it('should update handle updated configs', (done) => {
let i = 0
handler = createKarmaMiddleware(
filesDeferred.promise,
serveFile,
null,
{
get (val) {
if (val === 'config.client') {
i++
if (i === 1) {
return {foo: 'bar'}
} else {
return {foo: 'baz'}
}
} else {
return null
}
}
},
'/base/path',
'/__karma__/'
)
includedFiles([
new MockFile('/first.js')
])
fsMock._touchFile('/karma/static/debug.html', 1, '%CLIENT_CONFIG%')
response.once('end', () => {
expect(response).to.beServedAs(200, 'window.__karma__.config = {"foo":"bar"};\n')
response = new HttpResponseMock()
callHandlerWith('/__karma__/debug.html')
response.once('end', () => {
expect(response).to.beServedAs(200, 'window.__karma__.config = {"foo":"baz"};\n')
done()
})
})
callHandlerWith('/__karma__/debug.html')
})
})