forked from sockjs/sockjs-client
/
domtests.coffee
152 lines (135 loc) · 5.07 KB
/
domtests.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
module('Dom')
u = SockJS.getUtils()
newIframe = (path = '/iframe.html') ->
# Requires to put:
# document.domain = document.domain
# in HEAD, for IE7
hook = u.createHook()
err = ->
log('iframe error. bad.')
hook.iobj = u.createIframe(path + '?a=' + Math.random() + '#' + hook.id, err)
return hook
onunload_test_factory = (code) ->
return ->
expect(3)
hook = newIframe()
hook.open = ->
ok(true, 'open hook called by an iframe')
hook.callback(code)
hook.load = ->
ok(true, 'onload hook called by an iframe')
f = -> hook.iobj.cleanup()
setTimeout(f, 1)
hook.unload = ->
ok(true, 'onunload hook called by an iframe')
hook.del()
start()
if navigator.userAgent.indexOf('Konqueror') isnt -1
test "onunload [unsupported by client]", ->
ok(true)
else
asyncTest('onunload', onunload_test_factory("""
var u = SockJS.getUtils();
u.attachEvent('load', function(){
hook.load();
});
u.attachEvent('unload', function(){
hook.unload();
});
"""))
# if navigator.userAgent.indexOf('Konqueror') isnt -1 or $.browser.opera
# test "onbeforeunload [unsupported by client]", ->
# ok(true)
# else
# asyncTest('onbeforeunload', onunload_test_factory("""
# var u = SockJS.getUtils();
# u.attachEvent('load', function(){
# hook.load();
# });
# u.attachEvent('beforeunload', function(){
# hook.unload();
# });
# """))
if not SockJS.getIframeTransport().enabled()
test "onmessage [unsupported by client]", ->
ok(true)
else
asyncTest 'onmessage', ->
expect(3)
hook = newIframe()
hook.open = ->
ok(true, 'open hook called by an iframe')
hook.callback("""
var u = SockJS.getUtils();
u.attachMessage(function(e) {
var b = e.data;
parent.postMessage(window_id + ' ' + 'e', '*');
});
parent.postMessage(window_id + ' ' + 's', '*');
""")
u.attachMessage (e) ->
[window_id, data] = e.data.split(' ')
if window_id is hook.id
switch data
when 's'
hook.iobj.loaded()
ok(true, 'start frame send')
origin = u.getOrigin(u.amendUrl('/'))
hook.iobj.iframe.contentWindow.postMessage(hook.id + ' ' + 's' , origin)
when 'e'
ok(true, 'done hook called by an iframe')
hook.iobj.cleanup()
hook.del()
start()
ajax_simple_factory = (name) ->
asyncTest name + ' simple', ->
expect(2)
x = new u[name]('GET', '/simple.txt', null)
x.onfinish = (status, text) ->
equal(text.length, 2051)
equal(text.slice(-2), 'b\n')
start()
ajax_streaming_factory = (name) ->
asyncTest name + ' streaming', ->
expect(4)
x = new u[name]('GET', '/streaming.txt', null)
x.onchunk = (status, text) ->
equal(status, 200)
ok(text.length <= 2049, 'Most likely you\'re behind a transparent Proxy that can\'t do streaming. QUnit tests won\'t work properly. Sorry!')
delete x.onchunk
x.onfinish = (status, text) ->
equal(status, 200)
equal(text.slice(-4), 'a\nb\n')
start()
test_wrong_url = (name, url, statuses) ->
if window.console and console.log
console.log(' [*] Connecting to wrong url ' + url)
expect(2)
x = new u[name]('GET', url, null)
x.onchunk = ->
ok(false, "chunk shall not be received")
x.onfinish = (status, text) ->
ok(u.arrIndexOf(statuses, status) isnt -1)
equal(text, '')
start()
ajax_wrong_port_factory = (name) ->
for port in [25, 8999, 65300]
asyncTest name + ' wrong port ' + port, ->
test_wrong_url(name, 'http://localhost:'+port+'/wrong_url_indeed.txt', [0])
ajax_simple_factory('XHRLocalObject')
if window.XDomainRequest
ajax_simple_factory('XDRObject')
if not window.ActiveXObject
# Ajax streaming is not working in ie.
ajax_streaming_factory('XHRLocalObject')
if window.XDomainRequest
ajax_streaming_factory('XDRObject')
ajax_wrong_port_factory('XHRLocalObject')
if window.XDomainRequest
ajax_wrong_port_factory('XDRObject')
asyncTest 'XHRLocalObject wrong url', ->
# Opera responds with 0, all other browsers with 404
test_wrong_url('XHRLocalObject', '/wrong_url_indeed.txt', [0, 404])
if window.XDomainRequest
asyncTest 'XDRObject wrong url', ->
test_wrong_url('XDRObject', '/wrong_url_indeed.txt', [0])