Skip to content

Commit

Permalink
HTTP redirect tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jhs committed Dec 4, 2011
1 parent 23896cd commit a471ed2
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions tests/test-redirect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
var server = require('./server')
, assert = require('assert')
, request = require('../main.js')

var s = server.createServer()

s.listen(s.port, function () {
var server = 'http://localhost:' + s.port;
var hits = {}
var passed = 0;

bouncer(301, 'temp')
bouncer(302, 'perm')
bouncer(302, 'nope')

function bouncer(code, label) {
var landing = label+'_landing';

s.on('/'+label, function (req, res) {
hits[label] = true;
res.writeHead(code, {'location':server + '/'+landing})
res.end()
})

s.on('/'+landing, function (req, res) {
hits[landing] = true;
res.writeHead(200)
res.end(landing)
})
}

// Permanent bounce
request(server+'/perm', function (er, res, body) {
try {
assert.ok(hits.perm, 'Original request is to /perm')
assert.ok(hits.perm_landing, 'Forward to permanent landing URL')
assert.equal(body, 'perm_landing', 'Got permanent landing content')
passed += 1
} finally {
done()
}
})

// Temporary bounce
request(server+'/temp', function (er, res, body) {
try {
assert.ok(hits.temp, 'Original request is to /temp')
assert.ok(hits.temp_landing, 'Forward to temporary landing URL')
assert.equal(body, 'temp_landing', 'Got temporary landing content')
passed += 1
} finally {
done()
}
})

// Prevent bouncing.
request({uri:server+'/nope', followRedirect:false}, function (er, res, body) {
try {
assert.ok(hits.nope, 'Original request to /nope')
assert.ok(!hits.nope_landing, 'No chasing the redirect')
assert.equal(res.statusCode, 302, 'Response is the bounce itself')
passed += 1
} finally {
done()
}
})

var reqs_done = 0;
function done() {
reqs_done += 1;
if(reqs_done == 3) {
console.log(passed + ' tests passed.')
s.close()
}
}
})

0 comments on commit a471ed2

Please sign in to comment.