send_test.coffee
3.39 KB
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
describe 'XMLHttpRequest', ->
describe '#send', ->
beforeEach ->
@xhr = new XMLHttpRequest
@xhr.open 'POST', 'http://localhost:8912/_/echo'
@arrayBuffer = new ArrayBuffer xhr2PngBytes.length
@arrayBufferView = new Uint8Array @arrayBuffer
if typeof Buffer is 'undefined'
@buffer = null
else
@buffer = new Buffer xhr2PngBytes.length
for i in [0...xhr2PngBytes.length]
@arrayBufferView[i] = xhr2PngBytes[i]
@buffer.writeUInt8 xhr2PngBytes[i], i if @buffer
it 'works with ASCII DOMStrings', (done) ->
@xhr.onload = =>
expect(@xhr.getResponseHeader('content-type')).to.
match(/^text\/plain(;\s?charset=UTF-8)?$/)
expect(@xhr.responseText).to.equal 'Hello world!'
done()
@xhr.send "Hello world!"
it 'works with UTF-8 DOMStrings', (done) ->
@xhr.onloadend = =>
expect(@xhr.getResponseHeader('content-type')).to.
match(/^text\/plain(;\s?charset=UTF-8)?$/)
expect(@xhr.responseText).to.equal '世界你好!'
done()
@xhr.send '世界你好!'
it 'works with ArrayBufferViews', (done) ->
@xhr.responseType = 'arraybuffer'
@xhr.onload = =>
expect(@xhr.getResponseHeader('content-type')).to.equal null
responseView = new Uint8Array @xhr.response
responseBytes = (responseView[i] for i in [0...responseView.length])
expect(responseBytes).to.deep.equal xhr2PngBytes
done()
@xhr.send @arrayBufferView
it 'works with ArrayBufferViews with set index and length', (done) ->
@xhr.responseType = 'arraybuffer'
@xhr.onload = =>
expect(@xhr.getResponseHeader('content-type')).to.equal null
responseView = new Uint8Array @xhr.response
responseBytes = (responseView[i] for i in [0...responseView.length])
expect(responseBytes).to.deep.equal xhr2PngBytes[10...52]
done()
arrayBufferView10 = new Uint8Array @arrayBuffer, 10, 42
@xhr.send arrayBufferView10
it 'works with ArrayBuffers', (done) ->
@xhr.responseType = 'arraybuffer'
@xhr.onload = =>
expect(@xhr.getResponseHeader('content-type')).to.equal null
responseView = new Uint8Array @xhr.response
responseBytes = (responseView[i] for i in [0...responseView.length])
expect(responseBytes).to.deep.equal xhr2PngBytes
done()
@xhr.send @arrayBuffer
it 'works with node.js Buffers', (done) ->
return done() unless @buffer
# NOTE: using the same exact code as above, which is tested in a browser
@xhr.responseType = 'arraybuffer'
@xhr.onload = =>
expect(@xhr.getResponseHeader('content-type')).to.equal null
responseView = new Uint8Array @xhr.response
responseBytes = (responseView[i] for i in [0...responseView.length])
expect(responseBytes).to.deep.equal xhr2PngBytes
done()
@xhr.send @buffer
it 'sets POST headers correctly when given null data', (done) ->
@xhr.open 'POST', 'http://localhost:8912/_/headers'
@xhr.responseType = 'text'
@xhr.onload = =>
expect(@xhr.responseText).to.match(/^\{.*\}$/)
headers = JSON.parse @xhr.responseText
expect(headers).to.have.property 'content-length'
expect(headers['content-length']).to.equal '0'
expect(headers).not.to.have.property 'content-type'
done()
@xhr.send()