Updated the files.

This commit is contained in:
Batuhan Berk Başoğlu 2024-02-08 19:38:41 -05:00
parent 1553e6b971
commit 753967d4f5
23418 changed files with 3784666 additions and 0 deletions

1
my-app/node_modules/xhr2/test/fixtures/hello.json generated vendored Executable file
View file

@ -0,0 +1 @@
{"hello": "world", "answer": 42}

1
my-app/node_modules/xhr2/test/fixtures/hello.txt generated vendored Executable file
View file

@ -0,0 +1 @@
Hello world!

BIN
my-app/node_modules/xhr2/test/fixtures/xhr2.png generated vendored Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 563 B

30
my-app/node_modules/xhr2/test/html/browser_test.html generated vendored Executable file
View file

@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>node-xhr2 browser tests</title>
<link rel="stylesheet" href="/node_modules/mocha/mocha.css" />
<script src="/test/vendor/sinon.js"></script>
<script src="/test/vendor/chai.js"></script>
<script src="/node_modules/sinon-chai/lib/sinon-chai.js"></script>
<script src="/node_modules/mocha/mocha.js"></script>
<script src="/test/js/helpers/browser_mocha_setup.js"></script>
<script src="/test/js/helpers/xhr2.png.js"></script>
<script src="/test/js/helpers/setup.js"></script>
<script src="/test/js/event_target_test.js"></script>
<script src="/test/js/events_test.js"></script>
<script src="/test/js/headers_test.js"></script>
<script src="/test/js/redirect_test.js"></script>
<script src="/test/js/response_type_test.js"></script>
<script src="/test/js/send_test.js"></script>
<script src="/test/js/status_test.js"></script>
<script src="/test/js/xhr_test.js"></script>
<script src="/test/js/helpers/browser_mocha_runner.js"></script>
</head>
<body>
<div id="mocha"></div>
</body>
</html>

85
my-app/node_modules/xhr2/test/src/event_target_test.coffee generated vendored Executable file
View file

@ -0,0 +1,85 @@
describe 'XMLHttpRequestEventTarget', ->
describe 'dispatchEvent', ->
beforeEach ->
@xhr = new XMLHttpRequest
@loadEvent = new ProgressEvent 'load'
it 'works with a DOM0 listener', ->
count = 0
@xhr.onload = (event) ->
count += 1
@xhr.dispatchEvent @loadEvent
expect(count).to.equal 1
it 'works with a DOM2 listener', ->
count = 0
@xhr.addEventListener 'load', (event) ->
count += 1
@xhr.dispatchEvent @loadEvent
expect(count).to.equal 1
it 'removes a DOM2 listener correctly', ->
count = 0
listener = (event) ->
count += 1
@xhr.addEventListener 'load', listener
@xhr.dispatchEvent @loadEvent
expect(count).to.equal 1
count = 0
@xhr.removeEventListener 'load', listener
@xhr.dispatchEvent @loadEvent
expect(count).to.equal 0
it 'binds this correctly in a DOM0 listener', ->
eventThis = null
@xhr.onload = (event) ->
eventThis = @
@xhr.dispatchEvent @loadEvent
expect(eventThis).to.equal @xhr
it 'binds this correctly in a DOM2 listener', ->
eventThis = null
@xhr.addEventListener 'load', (event) ->
eventThis = @
@xhr.dispatchEvent @loadEvent
expect(eventThis).to.equal @xhr
it 'sets target correctly in a DOM0 listener', ->
eventTarget = null
@xhr.onload = (event) ->
eventTarget = event.target
@xhr.dispatchEvent @loadEvent
expect(eventTarget).to.equal @xhr
it 'sets target correctly in a DOM2 listener', ->
eventTarget = null
@xhr.addEventListener 'load', (event) ->
eventTarget = event.target
@xhr.dispatchEvent @loadEvent
expect(eventTarget).to.equal @xhr
it 'works with a DOM0 and two DOM2 listeners', ->
count = [0, 0, 0]
@xhr.addEventListener 'load', (event) ->
count[1] += 1
@xhr.onload = (event) ->
count[0] += 1
@xhr.addEventListener 'load', (event) ->
count[2] += 1
@xhr.dispatchEvent @loadEvent
expect(count).to.deep.equal [1, 1, 1]
it 'does not invoke a DOM0 listener for a different event', ->
count = 0
@xhr.onerror = (event) ->
count += 1
@xhr.dispatchEvent @loadEvent
expect(count).to.equal 0
it 'does not invoke a DOM2 listener for a different event', ->
count = 0
@xhr.addEventListener 'error', (event) ->
count += 1
@xhr.dispatchEvent @loadEvent
expect(count).to.equal 0

204
my-app/node_modules/xhr2/test/src/events_test.coffee generated vendored Executable file
View file

@ -0,0 +1,204 @@
describe 'XMLHttpRequest', ->
beforeEach ->
@xhr = new XMLHttpRequest
@dripUrl = 'http://localhost:8912/_/drip'
@dripJson = drips: 3, size: 1000, ms: 50, length: true
describe 'level 2 events', ->
beforeEach ->
@events = []
@endFired = false
@events.check = -> null # replaced by tests
@xhr.addEventListener 'loadstart', (event) =>
expect(event.type).to.equal 'loadstart'
expect(@endFired).to.equal false
@events.push event
@xhr.addEventListener 'progress', (event) =>
expect(event.type).to.equal 'progress'
expect(@endFired).to.equal false
@events.push event
@xhr.addEventListener 'load', (event) =>
expect(event.type).to.equal 'load'
expect(@endFired).to.equal false
@events.push event
@xhr.addEventListener 'loadend', (event) =>
expect(event.type).to.equal 'loadend'
expect(@endFired).to.equal false
@endFired = 'loadend already fired'
@events.push event
@events.check()
@xhr.addEventListener 'error', (event) =>
expect(event.type).to.equal 'error'
expect(@endFired).to.equal false
@events.push event
@xhr.addEventListener 'abort', (event) =>
expect(event.type).to.equal 'abort'
expect(@endFired).to.equal false
@events.push event
describe 'for a successful fetch with Content-Length set', ->
beforeEach ->
@xhr.open 'POST', @dripUrl
@xhr.send JSON.stringify(@dripJson)
it 'events have the correct target', (done) ->
@events.check = =>
for event in @events
expect(event.target).to.equal @xhr
done()
it 'events have the correct bubbling setup', (done) ->
@events.check = =>
for event in @events
expect(event.bubbles).to.equal false
expect(event.cancelable).to.equal false
done()
it 'events have the correct progress info', (done) ->
@events.check = =>
for event in @events
switch event.type
when 'loadstart'
expect(event.loaded).to.equal 0
expect(event.lengthComputable).to.equal false
expect(event.total).to.equal 0
when 'load', 'loadend'
expect(event.loaded).to.equal 3000
expect(event.lengthComputable).to.equal true
expect(event.total).to.equal 3000
when 'progress'
if event.lengthComputable
expect(event.loaded).to.be.gte 0
expect(event.loaded).to.be.lte 3000
expect(event.total).to.equal 3000
else
expect(event.loaded).to.be.gte 0
expect(event.total).to.equal 0
done()
it 'events include at least one intermediate progress event', (done) ->
@events.check = =>
found = 'no suitable progress event emitted'
for event in @events
continue unless event.type is 'progress'
continue unless event.loaded > 0
continue unless event.loaded < event.total
found = true
expect(found).to.equal true
done()
describe 'for a successful fetch without Content-Length set', ->
beforeEach ->
@xhr.open 'POST', @dripUrl
@dripJson.length = false
@xhr.send JSON.stringify(@dripJson)
it 'events have the correct progress info', (done) ->
@events.check = =>
for event in @events
expect(event.lengthComputable).to.equal false
expect(event.total).to.equal 0
switch event.type
when 'loadstart'
expect(event.loaded).to.equal 0
when 'load', 'loadend'
expect(event.loaded).to.equal 3000
when 'progress'
expect(event.loaded).to.be.gte 0
done()
it 'events include at least one intermediate progress event', (done) ->
@events.check = =>
found = 'no suitable progress event emitted'
for event in @events
continue unless event.type is 'progress'
continue if event.loaded is 0
continue if event.loaded is 3000
found = true
expect(found).to.equal true
done()
describe 'for a network error due to bad DNS', (done) ->
beforeEach ->
@xhr.open 'GET', 'https://broken.to.cause.an.xhrnetworkerror.com'
@xhr.send()
it 'no progress or load is emitted', (done) ->
@events.check = =>
for event in @events
expect(event.type).not.to.equal 'load'
expect(event.type).not.to.equal 'progress'
done()
it 'events include an error event', (done) ->
@events.check = =>
found = 'no suitable error emitted'
for event in @events
continue unless event.type is 'error'
found = true
expect(found).to.equal true
done()
describe 'readystatechange', ->
beforeEach ->
@events = []
@states = []
@doneFired = false
@events.check = -> null # replaced by tests
@xhr.addEventListener 'readystatechange', (event) =>
expect(event.type).to.equal 'readystatechange'
expect(@doneFired).to.equal false
@events.push event
@states.push event.target.readyState
if event.target.readyState is XMLHttpRequest.DONE
@doneFired = 'DONE already fired'
@events.check()
describe 'for a successful fetch with Content-Length set', ->
beforeEach ->
@xhr.open 'POST', @dripUrl
@xhr.send JSON.stringify(@dripJson)
it 'events have the correct target', (done) ->
@events.check = =>
for event in @events
expect(event.target).to.equal @xhr
done()
it 'events have the correct bubbling setup', (done) ->
@events.check = =>
for event in @events
expect(event.bubbles).to.equal false
expect(event.cancelable).to.equal false
done()
it 'events states are in the correct order', (done) ->
@events.check = =>
expect(@states).to.deep.equal [XMLHttpRequest.OPENED,
XMLHttpRequest.HEADERS_RECEIVED,
XMLHttpRequest.LOADING, XMLHttpRequest.DONE]
done()
describe 'for a successful fetch without Content-Length set', ->
beforeEach ->
@xhr.open 'POST', @dripUrl
@dripJson.length = false
@xhr.send JSON.stringify(@dripJson)
it 'events states are in the correct order', (done) ->
@events.check = =>
expect(@states).to.deep.equal [XMLHttpRequest.OPENED,
XMLHttpRequest.HEADERS_RECEIVED, XMLHttpRequest.LOADING,
XMLHttpRequest.DONE]
done()
describe 'for a network error due to bad DNS', (done) ->
beforeEach ->
@xhr.open 'GET', 'https://broken.to.cause.an.xhrnetworkerror.com'
@xhr.send()
it 'events states are in the correct order', (done) ->
@events.check = =>
expect(@states).to.deep.equal [XMLHttpRequest.OPENED,
XMLHttpRequest.DONE]
done()

168
my-app/node_modules/xhr2/test/src/headers_test.coffee generated vendored Executable file
View file

@ -0,0 +1,168 @@
describe 'XMLHttpRequest', ->
beforeEach ->
@xhr = new XMLHttpRequest
describe '#setRequestHeader', ->
beforeEach ->
@xhr.open 'POST', 'http://localhost:8912/_/headers'
@xhr.responseType = 'text'
describe 'with allowed headers', ->
beforeEach ->
@xhr.setRequestHeader 'Authorization', 'lol'
@xhr.setRequestHeader 'User-Agent', 'toaster'
@xhr.setRequestHeader 'X-Answer', '42'
@xhr.setRequestHeader 'X-Header-Name', 'value'
it 'should send the headers', (done) ->
@xhr.onload = =>
expect(@xhr.responseText).to.match(/^\{.*\}$/)
headers = JSON.parse @xhr.responseText
expect(headers).to.have.property 'authorization'
expect(headers['authorization']).to.equal 'lol'
expect(headers).to.have.property 'user-agent'
expect(headers['user-agent']).to.equal 'toaster'
expect(headers).to.have.property 'x-answer'
expect(headers['x-answer']).to.equal '42'
expect(headers).to.have.property 'x-header-name'
expect(headers['x-header-name']).to.equal 'value'
done()
@xhr.send ''
describe 'with a mix of allowed and forbidden headers', ->
beforeEach ->
@xhr.setRequestHeader 'Authorization', 'lol'
@xhr.setRequestHeader 'Proxy-Authorization', 'evil:kitten'
@xhr.setRequestHeader 'Sec-Breach', 'yes please'
@xhr.setRequestHeader 'Host', 'www.google.com'
@xhr.setRequestHeader 'Origin', 'https://www.google.com'
@xhr.setRequestHeader 'X-Answer', '42'
it 'should only send the allowed headers', (done) ->
@xhr.onloadend = =>
expect(@xhr.responseText).to.match(/^\{.*\}$/)
headers = JSON.parse @xhr.responseText
expect(headers).to.have.property 'authorization'
expect(headers['authorization']).to.equal 'lol'
expect(headers).not.to.have.property 'proxy-authorization'
expect(headers).not.to.have.property 'sec-breach'
expect(headers['origin']).not.to.match /www\.google\.com/
expect(headers['host']).not.to.match /www\.google\.com/
expect(headers).to.have.property 'x-answer'
expect(headers['x-answer']).to.equal '42'
done()
@xhr.send ''
describe 'with repeated headers', ->
beforeEach ->
@xhr.setRequestHeader 'Authorization', 'trol'
@xhr.setRequestHeader 'Authorization', 'lol'
@xhr.setRequestHeader 'Authorization', 'lol'
@xhr.setRequestHeader 'X-Answer', '42'
it 'should only send the allowed headers', (done) ->
_done = false
@xhr.onreadystatechange = =>
return if _done or @xhr.readyState isnt XMLHttpRequest.DONE
_done = true
expect(@xhr.responseText).to.match(/^\{.*\}$/)
headers = JSON.parse @xhr.responseText
expect(headers).to.have.property 'authorization'
expect(headers['authorization']).to.equal 'trol, lol, lol'
expect(headers).to.have.property 'x-answer'
expect(headers['x-answer']).to.equal '42'
done()
@xhr.send ''
describe 'with no headers', ->
beforeEach ->
@xhr.open 'POST', 'http://localhost:8912/_/headers'
@xhr.responseType = 'text'
it 'should set the protected headers correctly', (done) ->
@xhr.onload = =>
expect(@xhr.responseText).to.match(/^\{.*\}$/)
headers = JSON.parse @xhr.responseText
expect(headers).to.have.property 'connection'
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 'user-agent'
expect(headers['user-agent']).to.match(/^Mozilla\//)
done()
@xhr.send ''
describe '#getResponseHeader', ->
beforeEach ->
@xhr.open 'POST', 'http://localhost:8912/_/get_headers'
@headerJson =
'''
{"Accept-Ranges": "bytes",
"Content-Type": "application/xhr2; charset=utf-1337",
"Set-Cookie": "UserID=JohnDoe; Max-Age=3600; Version=1",
"X-Header": "one, more, value"}
'''
it 'returns accessible headers', (done) ->
@xhr.onloadend = =>
expect(@xhr.getResponseHeader('AccEPt-RANgeS')).to.equal 'bytes'
expect(@xhr.getResponseHeader('content-Type')).to.
equal 'application/xhr2; charset=utf-1337'
expect(@xhr.getResponseHeader('X-Header')).to.equal "one, more, value"
done()
@xhr.send @headerJson
it 'returns null for private headers', (done) ->
@xhr.onloadend = =>
expect(@xhr.getResponseHeader('set-cookie')).to.equal null
done()
@xhr.send @headerJson
it 'returns headers when the XHR enters HEADERS_RECEIVED', (done) ->
_done = false
@xhr.onreadystatechange = =>
return if _done or @xhr.readyState isnt XMLHttpRequest.HEADERS_RECEIVED
_done = true
expect(@xhr.getResponseHeader('AccEPt-RANgeS')).to.equal 'bytes'
done()
@xhr.send @headerJson
describe '#getAllResponseHeaders', ->
beforeEach ->
@xhr.open 'POST', 'http://localhost:8912/_/get_headers'
@headerJson =
'''
{"Accept-Ranges": "bytes",
"Content-Type": "application/xhr2; charset=utf-1337",
"Set-Cookie": "UserID=JohnDoe; Max-Age=3600; Version=1",
"X-Header": "one, more, value"}
'''
it 'contains accessible headers', (done) ->
@xhr.onloadend = =>
headers = @xhr.getAllResponseHeaders()
expect(headers).to.match(/(\A|\r\n)accept-ranges: bytes(\r\n|\Z)/mi)
expect(headers).to.match(
/(\A|\r\n)content-type: application\/xhr2; charset=utf-1337(\r\n|\Z)/mi)
expect(headers).to.match(/(\A|\r\n)X-Header: one, more, value(\r\n|\Z)/mi)
done()
@xhr.send @headerJson
it 'does not contain private headers', (done) ->
@xhr.onloadend = =>
headers = @xhr.getAllResponseHeaders()
expect(headers).not.to.match(/(\A|\r\n)set-cookie:/mi)
done()
@xhr.send @headerJson
it 'returns headers when the XHR enters HEADERS_RECEIVED', (done) ->
_done = false
@xhr.onreadystatechange = =>
return if _done or @xhr.readyState isnt XMLHttpRequest.HEADERS_RECEIVED
_done = true
headers = @xhr.getAllResponseHeaders()
expect(headers).to.match(/(\A|\r\n)accept-ranges: bytes(\r\n|\Z)/mi)
done()
@xhr.send @headerJson

View file

@ -0,0 +1,10 @@
window.addEventListener 'load', ->
runner = null
runner = mocha.run ->
return if runner is null # Synchronous tests may call this spuriously.
failures = runner.failures || 0
total = runner.total || 0
image = new Image()
image.src = "/diediedie?failed=#{failures}&total=#{total}";
image.onload = ->
null

View file

@ -0,0 +1 @@
mocha.setup ui: 'bdd', slow: 150, timeout: 1000, bail: false

38
my-app/node_modules/xhr2/test/src/helpers/setup.coffee generated vendored Executable file
View file

@ -0,0 +1,38 @@
if typeof window is 'undefined'
# node.js
global.XMLHttpRequest = require '../../../lib/xhr2'
global.ProgressEvent = XMLHttpRequest.ProgressEvent
global.NetworkError = XMLHttpRequest.NetworkError
global.SecurityError = XMLHttpRequest.SecurityError
global.InvalidStateError = XMLHttpRequest.InvalidStateError
global.chai = require 'chai'
global.assert = global.chai.assert
global.expect = global.chai.expect
global.sinon = require 'sinon'
global.sinonChai = require 'sinon-chai'
xhrServer = require './xhr_server'
require './xhr2.png.js'
https = require 'https'
agent = new https.Agent
agent.options.rejectUnauthorized = true
agent.options.ca = xhrServer.https.sslCertificate()
global.XMLHttpRequest.nodejsSet httpsAgent: agent
global.XMLHttpRequest.nodejsSet(
baseUrl: xhrServer.http.testUrl().replace('https://', 'http://'))
else
# browser
# HACK(pwnall): the test is first loaded on https so the developer can bypass
# the SSL interstitial; however, we need to run the test on http, because
# Chrome blocks https -> http XHRs
if window.location.href.indexOf('https://') is 0
window.location.href = window.location.href.replace('https://', 'http://').
replace(':8911', ':8912')
window.NetworkError = window.Error
window.SecurityError = window.Error
window.assert = window.chai.assert
window.expect = window.chai.expect

156
my-app/node_modules/xhr2/test/src/helpers/xhr_server.coffee generated vendored Executable file
View file

@ -0,0 +1,156 @@
express = require 'express'
fs = require 'fs'
http = require 'http'
https = require 'https'
open = require 'open'
# express.js server for testing the Web application.
class XhrServer
# Starts up a HTTP server.
constructor: (@port, @useHttps) ->
@createApp()
# Opens the test URL in a browser.
openBrowser: (appName) ->
open @testUrl(), appName
# The URL that should be used to start the tests.
testUrl: ->
"https://localhost:#{@port}/test/html/browser_test.html"
# The self-signed certificate used by this server.
sslCertificate: ->
return null unless @useHttps
keyMaterial = fs.readFileSync 'test/ssl/cert.pem', 'utf8'
certIndex = keyMaterial.indexOf '-----BEGIN CERTIFICATE-----'
keyMaterial.substring certIndex
# The key for the self-signed certificate used by this server.
sslKey: ->
return null unless @useHttps
keyMaterial = fs.readFileSync 'test/ssl/cert.pem', 'utf8'
certIndex = keyMaterial.indexOf '-----BEGIN CERTIFICATE-----'
keyMaterial.substring 0, certIndex
# The server code.
createApp: ->
@app = express()
## Middleware.
# CORS headers on everything, in case that ever gets implemented.
@app.use (request, response, next) ->
response.header 'Access-Control-Allow-Origin', '*'
response.header 'Access-Control-Allow-Methods', 'DELETE,GET,POST,PUT'
response.header 'Access-Control-Allow-Headers',
'Content-Type, Authorization'
next()
@app.use express.static(fs.realpathSync(__dirname + '/../../../'),
{ dotfiles: 'allow' })
## Routes
@app.all '/_/method', (request, response) ->
body = request.method
response.header 'Content-Type', 'text/plain; charset=utf-8'
response.header 'Content-Length', body.length.toString()
response.end body
# Echoes the request body. Used to test send(data).
@app.post '/_/echo', (request, response) ->
if request.headers['content-type']
response.header 'Content-Type', request.headers['content-type']
if request.headers['content-length']
response.header 'Content-Length', request.headers['content-length']
request.on 'data', (chunk) -> response.write chunk
request.on 'end', -> response.end()
# Lists the request headers. Used to test setRequestHeader().
@app.all '/_/headers', (request, response) ->
body = JSON.stringify request.headers
response.header 'Content-Type', 'application/json'
response.header 'Content-Length', body.length.toString()
response.end body
# Sets the response headers in the request. Used to test getResponse*().
@app.post '/_/get_headers', (request, response) ->
jsonString = ''
request.on 'data', (chunk) -> jsonString += chunk
request.on 'end', ->
headers = JSON.parse jsonString
for name, value of headers
response.header name, value
response.header 'Content-Length', '0'
response.end ''
# Sets every response detail. Used for error testing.
@app.post '/_/response', (request, response) ->
jsonString = ''
request.on 'data', (chunk) -> jsonString += chunk
request.on 'end', ->
json = JSON.parse jsonString
response.writeHead json.code, json.status, json.headers
response.write json.body if json.body
response.end()
# Sends data in small chunks. Used for event testing.
@app.post '/_/drip', (request, response) ->
request.connection.setNoDelay()
jsonString = ''
request.on 'data', (chunk) -> jsonString += chunk
request.on 'end', ->
json = JSON.parse jsonString
sentDrips = 0
drip = new Array(json.size + 1).join '.'
response.header 'Content-Type', 'text/plain'
if json.length
response.header 'Content-Length', (json.drips * json.size).toString()
sendDrip = =>
response.write drip
sentDrips += 1
if sentDrips >= json.drips
response.end()
else
setTimeout sendDrip, json.ms
sendDrip()
# Returns a HTTP redirect. Used to test the redirection handling code.
@app.all '/_/redirect/:status/:next_page', (request, response) =>
response.statusCode = parseInt(request.params.status)
response.header 'Location',
"http://#{request.get('host')}/_/#{request.params.next_page}"
body = "<p>This is supposed to have a redirect link</p>"
response.header 'Content-Type', 'text/html'
response.header 'Content-Length', body.length.toString()
response.header 'X-Redirect-Header', 'should not show up'
response.end body
# Requested when the browser test suite completes.
@app.get '/diediedie', (request, response) =>
if 'failed' of request.query
failed = parseInt request.query['failed']
else
failed = 1
total = parseInt request.query['total'] || 0
passed = total - failed
exitCode = if failed == 0 then 0 else 1
console.log "#{passed} passed, #{failed} failed"
response.header 'Content-Type', 'image/png'
response.header 'Content-Length', '0'
response.end ''
unless 'NO_EXIT' of process.env
@server.close()
process.exit exitCode
if @useHttps
options = key: @sslKey(), cert: @sslCertificate()
@server = https.createServer options, @app
else
@server = http.createServer @app
@server.listen @port
module.exports.https = new XhrServer 8911, true
module.exports.http = new XhrServer 8912, false

162
my-app/node_modules/xhr2/test/src/nodejs_set_test.coffee generated vendored Executable file
View file

@ -0,0 +1,162 @@
describe 'XMLHttpRequest', ->
describe '.nodejsSet', ->
beforeEach ->
@xhr = new XMLHttpRequest
@customXhr = new XMLHttpRequest
describe 'with a httpAgent option', ->
beforeEach ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
@customAgent = { custom: 'httpAgent' }
@customXhr.nodejsHttpAgent = @customAgent
@default = XMLHttpRequest::nodejsHttpAgent
@agent = { mocking: 'httpAgent' }
XMLHttpRequest.nodejsSet httpAgent: @agent
it 'sets the default nodejsHttpAgent', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
expect(@xhr.nodejsHttpAgent).to.equal @agent
it 'does not interfere with custom nodejsHttpAgent settings', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
expect(@customXhr.nodejsHttpAgent).to.equal @customAgent
afterEach ->
XMLHttpRequest.nodejsSet httpAgent: @default
describe 'with a httpsAgent option', ->
beforeEach ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
@customAgent = { custom: 'httpsAgent' }
@customXhr.nodejsHttpsAgent = @customAgent
@default = XMLHttpRequest::nodejsHttpsAgent
@agent = { mocking: 'httpsAgent' }
XMLHttpRequest.nodejsSet httpsAgent: @agent
it 'sets the default nodejsHttpsAgent', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
expect(@xhr.nodejsHttpsAgent).to.equal @agent
it 'does not interfere with custom nodejsHttpsAgent settings', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
expect(@customXhr.nodejsHttpsAgent).to.equal @customAgent
afterEach ->
XMLHttpRequest.nodejsSet httpsAgent: @default
describe 'with a baseUrl option', ->
beforeEach ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
@customBaseUrl = 'http://custom.url/base'
@customXhr.nodejsBaseUrl = @customBaseUrl
@default = XMLHttpRequest::nodejsBaseUrl
@baseUrl = 'http://localhost/base'
XMLHttpRequest.nodejsSet baseUrl: @baseUrl
it 'sets the default nodejsBaseUrl', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
expect(@xhr.nodejsBaseUrl).to.equal @baseUrl
it 'does not interfere with custom nodejsBaseUrl settings', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
expect(@customXhr.nodejsBaseUrl).to.equal @customBaseUrl
afterEach ->
XMLHttpRequest.nodejsSet baseUrl: @default
describe '#nodejsSet', ->
beforeEach ->
@xhr = new XMLHttpRequest
@customXhr = new XMLHttpRequest
describe 'with a httpAgent option', ->
beforeEach ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
@customAgent = { custom: 'httpAgent' }
@customXhr.nodejsSet httpAgent: @customAgent
it 'sets nodejsHttpAgent on the XHR instance', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
expect(@customXhr.nodejsHttpAgent).to.equal @customAgent
it 'does not interfere with default nodejsHttpAgent settings', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
expect(@xhr.nodejsHttpAgent).not.to.equal @customAgent
describe 'with a httpsAgent option', ->
beforeEach ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
@customAgent = { custom: 'httpsAgent' }
@customXhr.nodejsSet httpsAgent: @customAgent
it 'sets nodejsHttpsAgent on the XHR instance', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
expect(@customXhr.nodejsHttpsAgent).to.equal @customAgent
it 'does not interfere with default nodejsHttpsAgent settings', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
expect(@xhr.nodejsHttpsAgent).not.to.equal @customAgent
describe 'base URL parsing', ->
beforeEach ->
@xhr = new XMLHttpRequest
describe 'with null baseUrl', ->
beforeEach ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
@xhr.nodejsSet baseUrl: null
it 'parses an absolute URL', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
parsedUrl = @xhr._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.
@xhr.nodejsSet baseUrl: 'https://base.url/dir/file.html'
it 'parses an absolute URL', ->
return unless XMLHttpRequest.nodejsSet # Skip in browsers.
parsedUrl = @xhr._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 = @xhr._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 = @xhr._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 = @xhr._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 = @xhr._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'

57
my-app/node_modules/xhr2/test/src/redirect_test.coffee generated vendored Executable file
View file

@ -0,0 +1,57 @@
describe 'XMLHttpRequest', ->
describe 'when redirected', ->
beforeEach ->
@xhr = new XMLHttpRequest
it 'issues a GET for the next location', (done) ->
@xhr.open 'POST', 'http://localhost:8912/_/redirect/302/method'
@xhr.onload = =>
expect(@xhr.responseText).to.match(/GET/i)
done()
@xhr.send 'This should be dropped during the redirect'
it 'does not return the redirect headers', (done) ->
@xhr.open 'GET', 'http://localhost:8912/_/redirect/302/method'
@xhr.onload = =>
expect(@xhr.getResponseHeader('Content-Type')).to.equal(
'text/plain; charset=utf-8')
expect(@xhr.getResponseHeader('X-Redirect-Header')).not.to.be.ok
done()
@xhr.send()
it 'persists custom request headers across redirects', (done) ->
@xhr.open 'GET', 'http://localhost:8912/_/redirect/302/headers'
@xhr.setRequestHeader 'X-Redirect-Test', 'should be preserved'
@xhr.onload = =>
expect(@xhr.responseText).to.match(/^\{.*\}$/)
headers = JSON.parse @xhr.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()
@xhr.send()
it 'drops content-related headers across redirects', (done) ->
@xhr.open 'POST', 'http://localhost:8912/_/redirect/302/headers'
@xhr.setRequestHeader 'X-Redirect-Test', 'should be preserved'
@xhr.onload = =>
expect(@xhr.responseText).to.match(/^\{.*\}$/)
headers = JSON.parse @xhr.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()
@xhr.send 'This should be dropped during the redirect'
it 'provides the final responseURL', (done) ->
@xhr.open 'GET', 'http://localhost:8912/_/redirect/302/method'
@xhr.onload = =>
expect(@xhr.responseURL).to.equal("http://localhost:8912/_/method")
done()
@xhr.send()

88
my-app/node_modules/xhr2/test/src/response_type_test.coffee generated vendored Executable file
View file

@ -0,0 +1,88 @@
describe 'XMLHttpRequest', ->
describe '#responseType', ->
beforeEach ->
@xhr = new XMLHttpRequest
@jsonUrl = 'http://localhost:8912/test/fixtures/hello.json'
@jsonString = '{"hello": "world", "answer": 42}\n'
@imageUrl = 'http://localhost:8912/test/fixtures/xhr2.png'
describe 'text', ->
it 'reads a JSON file into a String', (done) ->
@xhr.addEventListener 'load', =>
expect(@xhr.response).to.equal @jsonString
expect(@xhr.responseText).to.equal @jsonString
done()
@xhr.open 'GET', @jsonUrl
@xhr.responseType = 'text'
@xhr.send()
describe 'json', ->
it 'reads a JSON file into a parsed JSON object', (done) ->
_done = false
@xhr.addEventListener 'readystatechange', =>
return if _done or @xhr.readyState isnt XMLHttpRequest.DONE
_done = true
expect(@xhr.response).to.deep.equal hello: 'world', answer: 42
done()
@xhr.open 'GET', @jsonUrl
@xhr.responseType = 'json'
@xhr.send()
it 'produces null when reading a non-JSON file ', (done) ->
@xhr.addEventListener 'loadend', =>
expect(@xhr.response).to.equal null
done()
@xhr.open 'GET', 'http://localhost:8912/test/fixtures/hello.txt'
@xhr.responseType = 'json'
@xhr.send()
describe 'arraybuffer', ->
it 'reads a JSON file into an ArrayBuffer', (done) ->
@xhr.addEventListener 'loadend', =>
expect(@xhr.response).to.be.instanceOf ArrayBuffer
view = new Uint8Array @xhr.response
string = (String.fromCharCode(view[i]) for i in [0...view.length]).
join ''
expect(string).to.equal @jsonString
done()
@xhr.open 'GET', @jsonUrl
@xhr.responseType = 'arraybuffer'
@xhr.send()
it 'reads a binary file into an ArrayBuffer', (done) ->
@xhr.addEventListener 'loadend', =>
expect(@xhr.response).to.be.instanceOf ArrayBuffer
view = new Uint8Array @xhr.response
bytes = (view[i] for i in [0...view.length])
expect(bytes).to.deep.equal xhr2PngBytes
done()
@xhr.open 'GET', @imageUrl
@xhr.responseType = 'arraybuffer'
@xhr.send()
describe 'buffer', ->
it 'reads a JSON file into a node.js Buffer', (done) ->
return done() if typeof Buffer is 'undefined'
@xhr.addEventListener 'loadend', =>
buffer = @xhr.response
expect(buffer).to.be.instanceOf Buffer
stringChars = for i in [0...buffer.length]
String.fromCharCode buffer.readUInt8(i)
expect(stringChars.join('')).to.equal @jsonString
done()
@xhr.open 'GET', @jsonUrl
@xhr.responseType = 'buffer'
@xhr.send()
it 'reads a binary file into a node.js Buffer', (done) ->
return done() if typeof Buffer is 'undefined'
@xhr.addEventListener 'loadend', =>
buffer = @xhr.response
expect(buffer).to.be.instanceOf Buffer
bytes = (buffer.readUInt8(i) for i in [0...buffer.length])
expect(bytes).to.deep.equal xhr2PngBytes
done()
@xhr.open 'GET', @imageUrl
@xhr.responseType = 'buffer'
@xhr.send()

18
my-app/node_modules/xhr2/test/src/responseurl_test.coffee generated vendored Executable file
View file

@ -0,0 +1,18 @@
describe 'XMLHttpRequest', ->
describe '#responseURL', ->
beforeEach ->
@xhr = new XMLHttpRequest
it 'provies the URL of the response', (done) ->
@xhr.open 'GET', 'http://localhost:8912/_/method'
@xhr.onload = =>
expect(@xhr.responseURL).to.equal("http://localhost:8912/_/method")
done()
@xhr.send()
it 'ignores the hash fragment', (done) ->
@xhr.open 'GET', 'http://localhost:8912/_/method#foo'
@xhr.onload = =>
expect(@xhr.responseURL).to.equal("http://localhost:8912/_/method")
done()
@xhr.send()

89
my-app/node_modules/xhr2/test/src/send_test.coffee generated vendored Executable file
View file

@ -0,0 +1,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 = Buffer.alloc 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()

47
my-app/node_modules/xhr2/test/src/status_test.coffee generated vendored Executable file
View file

@ -0,0 +1,47 @@
describe 'XMLHttpRequest', ->
beforeEach ->
@xhr = new XMLHttpRequest
@okUrl = 'http://localhost:8912/test/fixtures/hello.txt'
@errorUrl = 'http://localhost:8912/_/response'
@errorJson = JSON.stringify
code: 401, status: 'Unauthorized',
body: JSON.stringify(error: 'Credential error'),
headers:
'Content-Type': 'application/json', 'Content-Length': '28'
describe '#status', ->
it 'is 200 for a normal request', (done) ->
@xhr.open 'GET', @okUrl
_done = false
@xhr.addEventListener 'readystatechange', =>
return if _done
if @xhr.readyState < XMLHttpRequest.HEADERS_RECEIVED
expect(@xhr.status).to.equal 0
expect(@xhr.statusText).to.equal ''
else
expect(@xhr.status).to.equal 200
expect(@xhr.statusText).to.be.ok
expect(@xhr.statusText).to.not.equal ''
if @xhr.readyState is XMLHttpRequest.DONE
_done = true
done()
@xhr.send()
it 'returns the server-reported status', (done) ->
@xhr.open 'POST', @errorUrl
_done = false
@xhr.addEventListener 'readystatechange', =>
return if _done
if @xhr.readyState < XMLHttpRequest.HEADERS_RECEIVED
expect(@xhr.status).to.equal 0
expect(@xhr.statusText).to.equal ''
else
expect(@xhr.status).to.equal 401
expect(@xhr.statusText).to.be.ok
expect(@xhr.statusText).to.not.equal ''
if @xhr.readyState is XMLHttpRequest.DONE
_done = true
done()
@xhr.send @errorJson

102
my-app/node_modules/xhr2/test/src/xhr_test.coffee generated vendored Executable file
View file

@ -0,0 +1,102 @@
describe 'XMLHttpRequest', ->
beforeEach ->
@xhr = new XMLHttpRequest
describe 'constructor', ->
it 'sets readyState to UNSENT', ->
expect(@xhr.readyState).to.equal XMLHttpRequest.UNSENT
it 'sets timeout to 0', ->
expect(@xhr.timeout).to.equal 0
it 'sets responseType to ""', ->
expect(@xhr.responseType).to.equal ''
it 'sets status to 0', ->
expect(@xhr.status).to.equal 0
it 'sets statusText to ""', ->
expect(@xhr.statusText).to.equal ''
describe '#open', ->
it 'throws SecurityError on CONNECT', ->
expect(=> @xhr.open 'CONNECT', 'http://localhost:8912/test').to.
throw(SecurityError)
describe 'with a GET for a local https request', ->
beforeEach ->
@xhr.open 'GET', 'https://localhost:8911/test/fixtures/hello.txt'
it 'sets readyState to OPENED', ->
expect(@xhr.readyState).to.equal XMLHttpRequest.OPENED
it 'keeps status 0', ->
expect(@xhr.status).to.equal 0
it 'keeps statusText ""', ->
expect(@xhr.statusText).to.equal ''
describe '#send', ->
describe 'on a local http GET', ->
beforeEach ->
@xhr.open 'GET', 'http://localhost:8912/test/fixtures/hello.txt'
it 'kicks off the request', (done) ->
@xhr.onload = (event) =>
expect(@xhr.status).to.equal 200
expect(@xhr.responseText).to.equal 'Hello world!\n'
done()
@xhr.send()
describe 'on a local https GET', ->
beforeEach ->
@xhr.open 'GET', 'https://localhost:8911/test/fixtures/hello.txt'
it 'kicks off the request', (done) ->
@xhr.onload = (event) =>
expect(@xhr.status).to.equal 200
expect(@xhr.responseText).to.equal 'Hello world!\n'
done()
@xhr.send()
describe 'on a local relative GET', ->
beforeEach ->
@xhr.open 'GET', '../fixtures/hello.txt'
it 'kicks off the request', (done) ->
@xhr.onload = (event) =>
expect(@xhr.status).to.equal 200
expect(@xhr.responseText).to.equal 'Hello world!\n'
done()
@xhr.send()
describe 'on a local gopher GET', ->
describe '#open + #send', ->
it 'throw a NetworkError', ->
expect(=>
@xhr.open 'GET', 'gopher:localhost:8911'
@xhr.send()
).to.throw(NetworkError)
describe 'readyState constants', ->
it 'UNSENT < OPENED', ->
expect(XMLHttpRequest.UNSENT).to.be.below(XMLHttpRequest.OPENED)
it 'OPENED < HEADERS_RECEIVED', ->
expect(XMLHttpRequest.OPENED).to.be.
below(XMLHttpRequest.HEADERS_RECEIVED)
it 'HEADERS_RECEIVED < LOADING', ->
expect(XMLHttpRequest.HEADERS_RECEIVED).to.be.
below(XMLHttpRequest.LOADING)
it 'LOADING < DONE', ->
expect(XMLHttpRequest.LOADING).to.be.below(XMLHttpRequest.DONE)
it 'XMLHttpRequest constants match the instance costants', ->
expect(XMLHttpRequest.UNSENT).to.equal @xhr.UNSENT
expect(XMLHttpRequest.OPENED).to.equal @xhr.OPENED
expect(XMLHttpRequest.HEADERS_RECEIVED).to.equal @xhr.HEADERS_RECEIVED
expect(XMLHttpRequest.LOADING).to.equal @xhr.LOADING
expect(XMLHttpRequest.DONE).to.equal @xhr.DONE