xhr2
Version:
XMLHttpRequest emulation for node.js
163 lines (126 loc) • 6.41 kB
text/coffeescript
describe 'XMLHttpRequest', ->
describe '.nodejsSet', ->
beforeEach ->
= new XMLHttpRequest
= new XMLHttpRequest
describe 'with a httpAgent option', ->
beforeEach ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
= { custom: 'httpAgent' }
.nodejsHttpAgent =
= XMLHttpRequest::nodejsHttpAgent
= { mocking: 'httpAgent' }
XMLHttpRequest.nodejsSet httpAgent:
it 'sets the default nodejsHttpAgent', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
expect(.nodejsHttpAgent).to.equal
it 'does not interfere with custom nodejsHttpAgent settings', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
expect(.nodejsHttpAgent).to.equal
afterEach ->
XMLHttpRequest.nodejsSet httpAgent:
describe 'with a httpsAgent option', ->
beforeEach ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
= { custom: 'httpsAgent' }
.nodejsHttpsAgent =
= XMLHttpRequest::nodejsHttpsAgent
= { mocking: 'httpsAgent' }
XMLHttpRequest.nodejsSet httpsAgent:
it 'sets the default nodejsHttpsAgent', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
expect(.nodejsHttpsAgent).to.equal
it 'does not interfere with custom nodejsHttpsAgent settings', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
expect(.nodejsHttpsAgent).to.equal
afterEach ->
XMLHttpRequest.nodejsSet httpsAgent:
describe 'with a baseUrl option', ->
beforeEach ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
= 'http://custom.url/base'
.nodejsBaseUrl =
= XMLHttpRequest::nodejsBaseUrl
= 'http://localhost/base'
XMLHttpRequest.nodejsSet baseUrl:
it 'sets the default nodejsBaseUrl', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
expect(.nodejsBaseUrl).to.equal
it 'does not interfere with custom nodejsBaseUrl settings', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
expect(.nodejsBaseUrl).to.equal
afterEach ->
XMLHttpRequest.nodejsSet baseUrl:
describe '#nodejsSet', ->
beforeEach ->
= new XMLHttpRequest
= new XMLHttpRequest
describe 'with a httpAgent option', ->
beforeEach ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
= { custom: 'httpAgent' }
.nodejsSet httpAgent:
it 'sets nodejsHttpAgent on the XHR instance', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
expect(.nodejsHttpAgent).to.equal
it 'does not interfere with default nodejsHttpAgent settings', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
expect(.nodejsHttpAgent).not.to.equal
describe 'with a httpsAgent option', ->
beforeEach ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
= { custom: 'httpsAgent' }
.nodejsSet httpsAgent:
it 'sets nodejsHttpsAgent on the XHR instance', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
expect(.nodejsHttpsAgent).to.equal
it 'does not interfere with default nodejsHttpsAgent settings', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
expect(.nodejsHttpsAgent).not.to.equal
describe 'base URL parsing', ->
beforeEach ->
= new XMLHttpRequest
describe 'with null baseUrl', ->
beforeEach ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
.nodejsSet baseUrl: null
it 'parses an absolute URL', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
parsedUrl = ._parseUrl('http://www.domain.com/path')
expect(parsedUrl).to.be.ok
expect(parsedUrl).to.have.property 'href'
expect(parsedUrl.href).to.equal 'http://www.domain.com/path'
describe 'with a (protocol, domain, filePath) baseUrl', ->
beforeEach ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
.nodejsSet baseUrl: 'https://base.url/dir/file.html'
it 'parses an absolute URL', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
parsedUrl = ._parseUrl('http://www.domain.com/path')
expect(parsedUrl).to.be.ok
expect(parsedUrl).to.have.property 'href'
expect(parsedUrl.href).to.equal 'http://www.domain.com/path'
it 'parses a path-relative URL', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
parsedUrl = ._parseUrl('path/to.js')
expect(parsedUrl).to.be.ok
expect(parsedUrl).to.have.property 'href'
expect(parsedUrl.href).to.equal 'https://base.url/dir/path/to.js'
it 'parses a path-relative URL with ..', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
parsedUrl = ._parseUrl('../path/to.js')
expect(parsedUrl).to.be.ok
expect(parsedUrl).to.have.property 'href'
expect(parsedUrl.href).to.equal 'https://base.url/path/to.js'
it 'parses a host-relative URL', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
parsedUrl = ._parseUrl('/path/to.js')
expect(parsedUrl).to.be.ok
expect(parsedUrl).to.have.property 'href'
expect(parsedUrl.href).to.equal 'https://base.url/path/to.js'
it 'parses a protocol-relative URL', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
parsedUrl = ._parseUrl('//domain.com/path/to.js')
expect(parsedUrl).to.be.ok
expect(parsedUrl).to.have.property 'href'
expect(parsedUrl.href).to.equal 'https://domain.com/path/to.js'