This repository has been archived by the owner on Dec 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 520
/
xhr_test.coffee
238 lines (204 loc) · 7.13 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
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
{ assert, brains, Browser } = require("./helpers")
describe "XMLHttpRequest", ->
browser = null
before ->
browser = Browser.create()
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.visit("http://localhost:3003/xhr/async", done)
it "should load resource asynchronously", ->
browser.assert.text "title", "OneTwoThree"
it "should run callback in global context", ->
browser.assert.global "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.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.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.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.visit("http://localhost:3003/xhr/post/empty", done)
it "should post with no data", ->
browser.assert.text "title", "201posted"
describe "empty response", ->
before (done)->
brains.get "/xhr/get-empty", (req, res)->
res.send """
<html>
<head><script src="/jquery.js"></script></head>
<body>
<script>
$.get("/xhr/empty", function(response, status, xhr) {
document.text = xhr.responseText;
});
</script>
</body>
</html>
"""
brains.get "/xhr/empty", (req, res)->
res.send ""
brains.ready done
before (done)->
browser.visit("http://localhost:3003/xhr/get-empty", done)
it "responseText should be an empty string", ->
assert.strictEqual "", browser.document.text
describe "response text", ->
before (done)->
brains.get "/xhr/get-utf8-octet-stream", (req, res)->
res.send """
<html>
<head><script src="/jquery.js"></script></head>
<body>
<script>
$.get("/xhr/utf8-octet-stream", function(response, status, xhr) {
document.text = xhr.responseText;
});
</script>
</body>
</html>
"""
brains.get "/xhr/utf8-octet-stream", (req, res)->
res.type "application/octet-stream"
res.send "Text"
brains.ready done
before (done)->
browser.visit("http://localhost:3003/xhr/get-utf8-octet-stream", done)
it "responseText should be a string", ->
assert.equal "string", typeof browser.document.text
assert.equal "Text", browser.document.text
describe "HTML document", ->
before (done)->
brains.get "/xhr/get-html", (req, res)->
res.send """
<html>
<head><script src="/jquery.js"></script></head>
<body>
<script>
$.get("/xhr/html", function(response, status, xhr) {
window.html = xhr.responseXML;
});
</script>
</body>
</html>
"""
brains.get "/xhr/html", (req, res)->
res.type("text/html")
res.send "<foo><bar id='bar'></foo>"
brains.ready done
before (done)->
browser.visit("http://localhost:3003/xhr/get-html", done)
it "should parse HTML document", ->
assert browser.document.body.querySelectorAll("foo > bar#bar")
after ->
browser.destroy()