This repository has been archived by the owner on Dec 16, 2023. It is now read-only.
/
xhr_test.coffee
157 lines (135 loc) · 4.93 KB
/
xhr_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
{ assert, brains, Browser } = require("./helpers")
describe "XMLHttpRequest", ->
describe "asynchronous", ->
before (done)->
brains.get "/xhr/async", (req, res)->
res.send """
<html>
<head><script src="/jquery.js"></script></head>
<body>
<script>
document.title = "One";
window.foo = "bar";
$.get("/xhr/async/backend", function(response) {
window.foo += window.foo;
document.title += response;
});
document.title += "Two";
</script>
</body>
</html>
"""
brains.get "/xhr/async/backend", (req, res)->
res.send "Three"
brains.ready done
before (done)->
@browser = new Browser()
@browser.visit "http://localhost:3003/xhr/async", done
it "should load resource asynchronously", ->
assert.equal @browser.window.title, "OneTwoThree"
it "should run callback in global context", ->
assert.equal @browser.window.foo, "barbar"
describe "response headers", ->
before (done)->
brains.get "/xhr/headers", (req, res)->
res.send """
<html>
<head><script src="/jquery.js"></script></head>
<body>
<script>
$.get("/xhr/headers/backend", function(data, textStatus, jqXHR) {
document.allHeaders = jqXHR.getAllResponseHeaders();
document.headerOne = jqXHR.getResponseHeader('Header-One');
document.headerThree = jqXHR.getResponseHeader('header-three');
});
</script>
</body>
</html>
"""
brains.get "/xhr/headers/backend", (req, res)->
res.setHeader "Header-One", "value1"
res.setHeader "Header-Two", "value2"
res.setHeader "Header-Three", "value3"
res.send ""
brains.ready done
before (done)->
@browser = new Browser()
@browser.visit "http://localhost:3003/xhr/headers", done
it "should return all headers as string", ->
assert ~@browser.document.allHeaders.indexOf("header-one: value1\nheader-two: value2\nheader-three: value3")
it "should return individual headers", ->
assert.equal @browser.document.headerOne, "value1"
assert.equal @browser.document.headerThree, "value3"
describe "cookies", ->
before (done)->
brains.get "/xhr/cookies", (req, res)->
res.cookie "xhr", "send", path: "/xhr"
res.send """
<html>
<head><script src="/jquery.js"></script></head>
<body>
<script>
$.get("/xhr/cookies/backend", function(cookie) {
document.received = cookie;
});
</script>
</body>
</html>
"""
brains.get "/xhr/cookies/backend", (req, res)->
cookie = req.cookies["xhr"]
res.cookie "xhr", "return", path: "/xhr"
res.send cookie
brains.ready done
before (done)->
@browser = new Browser()
@browser.visit "http://localhost:3003/xhr/cookies", done
it "should send cookies to XHR request", ->
assert.equal @browser.document.received, "send"
it "should return cookies from XHR request", ->
assert /xhr=return/.test(@browser.document.cookie)
describe "redirect", ->
before (done)->
brains.get "/xhr/redirect", (req, res)-> res.send """
<html>
<head><script src="/jquery.js"></script></head>
<body>
<script>
$.get("/xhr/redirect/backend", function(response) { window.response = response });
</script>
</body>
</html>
"""
brains.get "/xhr/redirect/backend", (req, res)->
res.redirect "/xhr/redirect/target"
brains.get "/xhr/redirect/target", (req, res)->
res.send "redirected " + req.headers["x-requested-with"]
brains.ready done
before (done)->
@browser = new Browser()
@browser.visit "http://localhost:3003/xhr/redirect", done
it "should follow redirect", ->
assert /redirected/.test(@browser.window.response)
it "should resend headers", ->
assert /XMLHttpRequest/.test(@browser.window.response)
describe "handle POST requests with no data", ->
before (done)->
brains.get "/xhr/post/empty", (req, res)->
res.send """
<html>
<head><script src="/jquery.js"></script></head>
<body>
<script>
$.post("/xhr/post/empty", function(response, status, xhr) { document.title = xhr.status + response });
</script>
</body>
</html>
"""
brains.post "/xhr/post/empty", (req, res)->
res.send "posted", 201
brains.ready done
before (done)->
@browser = new Browser()
@browser.visit "http://localhost:3003/xhr/post/empty", done
it "should post with no data", ->
assert.equal @browser.document.title, "201posted"