-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
karma.spec.coffee
369 lines (273 loc) · 12.2 KB
/
karma.spec.coffee
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
describe 'middleware.karma', ->
q = require 'q'
constants = require '../../../lib/constants'
mocks = require 'mocks'
HttpResponseMock = mocks.http.ServerResponse
HttpRequestMock = mocks.http.ServerRequest
File = require('../../../lib/file_list').File
Url = require('../../../lib/file_list').Url
MockFile = (path, sha) ->
File.call @, path
@sha = sha or 'sha-default'
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%')
createServeFile = require('../../../lib/middleware/common').createServeFile
createKarmaMiddleware = require('../../../lib/middleware/karma').create
handler = serveFile = filesDeferred = nextSpy = response = null
beforeEach ->
clientConfig = foo: 'bar'
nextSpy = sinon.spy()
response = new HttpResponseMock
filesDeferred = q.defer()
serveFile = createServeFile fsMock, '/karma/static'
handler = createKarmaMiddleware filesDeferred.promise, serveFile,
'/base/path', '/__karma__/', clientConfig
# helpers
includedFiles = (files) ->
filesDeferred.resolve {included: files, served: []}
servedFiles = (files) ->
filesDeferred.resolve {included: [], served: files}
normalizedHttpRequest = (urlPath) ->
req = new HttpRequestMock(urlPath)
req.normalizedUrl = req.url
return req
callHandlerWith = (urlPath, next) ->
promise = handler normalizedHttpRequest(urlPath), response, next or nextSpy
if promise and promise.done then 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, '/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, '/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, '/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"></script>\n' +
'<script type="application/dart" src="/__karma__/absolute/second.dart?sha456"></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"></script>\n' +
'<script type="text/javascript" src="/__karma__/base/b.js?shaaa"></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"></script>'
done()
callHandlerWith '/__karma__/context.html'
it 'should send non-caching headers for context.html', (done) ->
ZERO_DATE = (new Date 0).toString()
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 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"></script>\n' +
'<script type="text/javascript" src="/__karma__/base/b.js"></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 invoke custom handler', (done) ->
# response.once 'end', ->
# expect(nextSpy).not.to.have.been.called
# expect(response.statusCode).to.equal 200
# expect(response._content.toString()).to.equal 'Hello World'
# done()
# customHandler =
# urlRegex: /\/test/,
# handler: (request, response, staticFolder, adapterFolder, baseFolder, urlRoot) ->
# response.end 'Hello World'
# karmaSrcHandler = m.createKarmaSourceHandler promiseContainer, staticFolderPath,
# adapterFolderPath, baseFolder, '/_karma_/', [customHandler], []
# karmaSrcHandler new httpMock.ServerRequest('/_karma_/test'), response, nextSpy
# it 'should set custom script type', (done) ->
# mocks.fs._touchFile '/karma/static/context.html', 0, 'CONTEXT\n%SCRIPTS%'
# includedFiles [{path: 'http://some.url.com/whatever.blah', isUrl: true}]
# response.once 'end', ->
# expect(response._content.toString()).to.equal 'CONTEXT\n' +
# '<script type="application/blah" src="http://some.url.com/whatever.blah"></script>'
# expect(response.statusCode).to.equal 200
# done()
# customScriptType =
# extension: 'blah',
# contentType: 'application/blah'
# karmaSrcHandler = m.createKarmaSourceHandler promiseContainer, staticFolderPath,
# adapterFolderPath, baseFolder, '/_karma_/', [], [customScriptType]
# karmaSrcHandler new httpMock.ServerRequest('/_karma_/context.html'), response, nextSpy