Skip to content

Commit

Permalink
Merge branch 'firefox-204-response' of git://github.com/TehShrike/xhr…
Browse files Browse the repository at this point in the history
… into firefox-204-response
  • Loading branch information
naugtur committed Jun 4, 2016
2 parents a34cec3 + ba57626 commit 13f1eea
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
},
"license": "MIT",
"scripts": {
"test": "run-browser test/index.js -b | tap-spec",
"browser": "run-browser test/index.js"
"test": "node test-server.js phantom | tap-spec",
"browser": "node test-server.js"
}
}
29 changes: 29 additions & 0 deletions test-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var http = require('http')
var runBrowser = require('run-browser')

var phantom = process.argv[2] === 'phantom'
var port = 3000

var handler = runBrowser.createHandler('test/index.js', null, phantom)
var server = http.createServer(function (req, res) {
if (req.url === '/no-content') {
res.statusCode = 204
res.end('')
} else if (req.url === '/timeout') {
setTimeout(function() {
res.statusCode = 200
res.end()
}, 5000)
} else {
return handler(req, res, phantom)
}
})
server.listen(port)

if (phantom) {
var proc = runBrowser.runPhantom('http://localhost:' + port+ '/')
proc.stdout.pipe(process.stdout)
proc.stderr.pipe(process.stderr)
} else {
console.log('Open a browser and navigate to "http://localhost:' + port+ '"')
}
12 changes: 11 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,20 @@ test("[func] Returns http error responses like npm's request (cross-domain)", fu
}
})

test("[func] Returns a falsey body for 204 responses", function(assert) {
xhr({
uri: "/no-content"
}, function(err, resp, body) {
assert.notOk(body, "body should be falsey")
assert.equal(resp.statusCode, 204)
assert.end()
})
})

test("[func] Times out to an error ", function(assert) {
xhr({
timeout: 1,
uri: "/tests-bundle.js?should-take-a-bit-to-parse=1&" + (new Array(300)).join("cachebreaker=" + Math.random().toFixed(5) + "&")
uri: "/timeout"
}, function(err, resp, body) {
assert.ok(err instanceof Error, "should return error")
assert.equal(err.message, "XMLHttpRequest timeout")
Expand Down

0 comments on commit 13f1eea

Please sign in to comment.