forked from assaf/zombie
/
script_test.coffee
427 lines (353 loc) · 13 KB
/
script_test.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
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
{ assert, brains, Browser } = require("./helpers")
describe "Scripts", ->
before (done)->
brains.ready done
describe "basic", ->
before ->
brains.get "/script/living/", (req, res)->
res.send """
<html>
<head>
<script src="/jquery.js"></script>
<script src="/sammy.js"></script>
<script src="./app.js"></script>
</head>
<body>
<div id="main">
<a href="/script/dead">Kill</a>
<form action="#/dead" method="post">
<label>Email <input type="text" name="email"></label>
<label>Password <input type="password" name="password"></label>
<button>Sign Me Up</button>
</form>
</div>
<div class="now">Walking Aimlessly</div>
</body>
</html>
"""
brains.get "/script/living/app.js", (req, res)->
res.send """
Sammy("#main", function(app) {
app.get("#/", function(context) {
document.title = "The Living";
});
app.get("#/dead", function(context) {
context.swap("The Living Dead");
});
app.post("#/dead", function(context) {
document.title = "Signed up";
});
});
$(function() { Sammy("#main").run("#/") });
"""
describe "run app", ->
before (done)->
@browser = new Browser()
@browser.visit "http://localhost:3003/script/living/", done
it "should execute route", ->
assert.equal @browser.document.title, "The Living"
it "should change location", ->
assert.equal @browser.location.href, "http://localhost:3003/script/living/#/"
describe "move around", ->
before (done)->
@browser.visit @browser.location.href + "dead", done
it "should execute route", ->
assert.equal @browser.text("#main"), "The Living Dead"
it "should change location", ->
assert.equal @browser.location.href, "http://localhost:3003/script/living/#/dead"
describe "live events", ->
before (done)->
@browser = new Browser()
@browser.visit("http://localhost:3003/script/living/")
.then =>
@browser.fill "Email", "armbiter@zombies"
@browser.fill "Password", "br41nz"
@browser.pressButton "Sign Me Up"
.then(done, done)
it "should change location", ->
assert.equal @browser.location.href, "http://localhost:3003/script/living/#/"
it "should process event", ->
assert.equal @browser.document.title, "Signed up"
describe "evaluate", ->
before (done)->
Browser.visit "http://localhost:3003/script/living/", (error, browser)=>
@title = browser.evaluate "document.title"
done(error)
it "should evaluate in context and return value", ->
assert.equal @title, "The Living"
describe "evaluating", ->
describe "context", ->
before (done)->
brains.get "/script/context", (req, res)->
res.send """
<html>
<script>var foo = 1</script>
<script>window.foo = foo + 1</script>
<script>document.title = this.foo</script>
<script>
setTimeout(function() {
document.title = foo + window.foo
});</script>
</html>
"""
@browser = new Browser()
@browser.visit "http://localhost:3003/script/context", done
it "should be shared by all scripts", ->
assert.equal @browser.text("title"), "4"
describe "window", ->
before (done)->
brains.get "/script/window", (req, res)->
res.send """
<html>
<script>document.title = [window == this,
this == window.window,
this == top,
top == window.top,
this == parent,
top == parent].join(',')</script>
</html>
"""
@browser = new Browser()
@browser.visit "http://localhost:3003/script/window", done
it "should be the same as this, top and parent", ->
assert.equal @browser.text("title"), "true,true,true,true,true,true"
describe "global and function", ->
before (done)->
brains.get "/script/global_and_fn", (req, res)->
res.send """
<html>
<script>
var foo;
(function() {
if (!foo)
foo = "foo";
})()
document.title = foo;
</script>
</html>
"""
@browser = new Browser()
@browser.visit "http://localhost:3003/script/global_and_fn", done
it "should not fail with an error", ->
assert.equal @browser.errors.length, 0
it "should set global variable", ->
assert.equal @browser.text("title"), "foo"
describe "order", ->
before (done)->
brains.get "/script/order", (req, res)->
res.send """
<html>
<head>
<title>Zero</title>
<script src="/script/order.js"></script>
</head>
<body>
<script>
document.title = document.title + "Two";
</script>
</body>
</html>
"""
brains.get "/script/order.js", (req, res)->
res.send "document.title = document.title + 'One'"
@browser = new Browser()
@browser.visit "http://localhost:3003/script/order", done
it "should run scripts in order regardless of source", ->
assert.equal @browser.text("title"), "ZeroOneTwo"
describe "eval", ->
before (done)->
brains.get "/script/eval", (req, res)->
res.send """
<html>
<script>
var foo = "One";
(function() {
var bar = "Two"; // standard eval sees this
var e = eval; // this 'eval' only sees global scope
try {
var baz = e("bar");
} catch (ex) {
var baz = "Three";
}
// In spite of local variable, global scope eval finds global foo
var foo = "NotOne";
var e_foo = e("foo");
var qux = window.eval.call(window, "foo");
document.title = eval('e_foo + bar + baz + qux');
})();
</script>
</html>
"""
@browser = new Browser()
@browser.visit "http://localhost:3003/script/eval", done
it "should evaluate in global scope", ->
assert.equal @browser.document.title, "OneTwoThreeOne"
describe "failing", ->
describe "incomplete", ->
before (done)->
brains.get "/script/incomplete", (req, res)->
res.send """
<html>
<script>1+</script>
</html>
"""
@browser = new Browser()
@browser.visit "http://localhost:3003/script/incomplete", (@error)=>
done()
it "should pass error to callback", ->
assert.equal @error.message, "Unexpected end of input"
it "should propagate error to window", ->
assert.equal @browser.error.message, "Unexpected end of input"
describe "error", ->
before (done)->
brains.get "/script/error", (req, res)->
res.send """
<html>
<script>(function(foo) { foo.bar })()</script>
</html>
"""
@browser = new Browser()
@browser.visit "http://localhost:3003/script/error", (@error)=>
done()
it "should pass error to callback", ->
assert.equal @error.message, "Cannot read property 'bar' of undefined"
it "should propagate error to window", ->
assert.equal @browser.error.message, "Cannot read property 'bar' of undefined"
describe "loading", ->
describe "with entities", ->
before (done)->
brains.get "/script/split", (req, res)->
res.send """
<html>
<script>foo = 1 < 2 ? 1 : 2; '&'; document.title = foo</script>
</html>
"""
@browser = new Browser()
@browser.visit "http://localhost:3003/script/split", done
it "should run full script", ->
assert.equal @browser.text("title"), "1"
# TODO: handle CDATA scripts
###
describe "with CDATA", ->
before (done)->
brains.get "/script/cdata", (req, res)-> res.send """
<html>
<script>foo = 2; <![CDATA[ document.title ]]> = foo</script>
</html>
"""
@browser = new Browser()
@browser.visit "http://localhost:3003/script/cdata", done
it "should run full script", ->
assert.equal @browser.text("title"), "2"
###
describe "using document.write", ->
before (done)->
brains.get "/script/write", (req, res)-> res.send """
<html>
<body>
<script>document.write(unescape(\'%3Cscript %3Edocument.title = document.title + ".write"%3C/script%3E\'));</script>
<script>
document.title = document.title + "document";
</script>
</body>
</html>
"""
@browser = new Browser()
@browser.visit "http://localhost:3003/script/write", done
it "should run script", ->
assert.equal @browser.document.title, "document.write"
describe "using appendChild", ->
before (done)->
brains.get "/script/append", (req, res)->
res.send """
<html>
<head>
<script>
var s = document.createElement('script'); s.type = 'text/javascript'; s.async = true;
s.src = '/script/append.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(s);
</script>
</head>
<body>
<script>
document.title = document.title + "element.";
</script>
</body>
</html>
"""
brains.get "/script/append.js", (req, res)->
res.send "document.title = document.title + \"appendChild\""
@browser = new Browser()
@browser.visit "http://localhost:3003/script/append", done
it "should run script", ->
assert.equal @browser.document.title, "element.appendChild"
describe "scripts disabled", ->
before (done)->
brains.get "/script/no-scripts", (req, res)->
res.send """
<html>
<head>
<title>Zero</title>
<script src="/script/no-scripts.js"></script>
</head>
<body>
<script>
document.title = document.title + "Two";</script>
</body>
</html>
"""
brains.get "/script/no-scripts.js", (req, res)->
res.send "document.title = document.title + 'One'"
@browser = new Browser(runScripts: false)
@browser.visit "http://localhost:3003/script/order", done
it "should not run scripts", ->
assert.equal @browser.document.title, "Zero"
describe "file:// uri scheme", ->
before (done)->
@browser = new Browser()
@browser.visit "file://#{__dirname}/data/file_scheme.html", done
it "should run scripts with file url src", ->
assert.equal @browser.document.title, "file://"
describe "javascript: URL", ->
before (done)->
@browser = new Browser()
@browser.visit "javascript:document.write('hi')", done
it "should evaluate script in context of window", ->
assert.equal @browser.text(), "hi"
describe "new Image", ->
before ->
@browser = new Browser()
it "should construct an img tag", ->
assert.equal @browser.evaluate("new Image").tagName, "IMG"
it "should construct an img tag with width and height", ->
assert.equal @browser.evaluate("new Image(1, 1)").height, 1
describe "Event", ->
it "should be available in global context", ->
browser = new Browser()
assert browser.evaluate("Event")
describe "on- event handler", ->
before (done)->
brains.get "/script/event", (req, res)->
res.send """
<form onsubmit="document.title = event.eventType; return false">
<button>Submit</button>
</form>
"""
brains.ready done
before (done)->
@browser = new Browser()
@browser.visit("http://localhost:3003/script/event")
.then =>
@browser.pressButton "Submit"
.then(done, done)
it "should prevent default handling by returning false", ->
assert.equal @browser.location.href, "http://localhost:3003/script/event"
it "should have access to window.event", ->
assert.equal @browser.document.title, "HTMLEvents"
describe "JSON parsing", ->
it "should respect prototypes", ->
browser = new Browser()
assert browser.evaluate("""
Array.prototype.method = function() {};
JSON.parse("[0, 1]").method;
""")