xhr2
Version:
XMLHttpRequest emulation for node.js
58 lines (52 loc) • 2.5 kB
text/coffeescript
describe 'XMLHttpRequest', ->
describe 'when redirected', ->
beforeEach ->
= new XMLHttpRequest
it 'issues a GET for the next location', (done) ->
.open 'POST', 'http://localhost:8912/_/redirect/302/method'
.onload = =>
expect(.responseText).to.match(/GET/i)
done()
.send 'This should be dropped during the redirect'
it 'does not return the redirect headers', (done) ->
.open 'GET', 'http://localhost:8912/_/redirect/302/method'
.onload = =>
expect(.getResponseHeader('Content-Type')).to.equal(
'text/plain; charset=utf-8')
expect(.getResponseHeader('X-Redirect-Header')).not.to.be.ok
done()
.send()
it 'persists custom request headers across redirects', (done) ->
.open 'GET', 'http://localhost:8912/_/redirect/302/headers'
.setRequestHeader 'X-Redirect-Test', 'should be preserved'
.onload = =>
expect(.responseText).to.match(/^\{.*\}$/)
headers = JSON.parse .responseText
expect(headers['connection']).to.equal 'keep-alive'
expect(headers).to.have.property 'host'
expect(headers['host']).to.equal 'localhost:8912'
expect(headers).to.have.property 'x-redirect-test'
expect(headers['x-redirect-test']).to.equal 'should be preserved'
done()
.send()
it 'drops content-related headers across redirects', (done) ->
.open 'POST', 'http://localhost:8912/_/redirect/302/headers'
.setRequestHeader 'X-Redirect-Test', 'should be preserved'
.onload = =>
expect(.responseText).to.match(/^\{.*\}$/)
headers = JSON.parse .responseText
expect(headers['connection']).to.equal 'keep-alive'
expect(headers).to.have.property 'host'
expect(headers['host']).to.equal 'localhost:8912'
expect(headers).to.have.property 'x-redirect-test'
expect(headers['x-redirect-test']).to.equal 'should be preserved'
expect(headers).not.to.have.property 'content-type'
expect(headers).not.to.have.property 'content-length'
done()
.send 'This should be dropped during the redirect'
it 'provides the final responseURL', (done) ->
.open 'GET', 'http://localhost:8912/_/redirect/302/method'
.onload = =>
expect(.responseURL).to.equal("http://localhost:8912/_/method")
done()
.send()