Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
test for preserving headers on redirect for superagent
var express = require('express')
, app = express()
, secapp = express()
, request = require('../../')
, assert = require('assert')
, should = require('should')
, querystring = require('querystring')
, https = require('https');
app.use(express.cookieParser())
.use(express.session({ secret: 'secret' }))
.use(express.bodyParser())
.use(express.logger())
secapp.use(express.cookieParser())
.use(express.session({ secret: 'secret' }))
.use(express.bodyParser())
.use(express.logger())
var port = 4000
var secure_port = 4001
app.get('/headercheck', function(req, res) {
req.headers.should.have.property('user-agent')
req.session.user = 'hunter@hunterloftis.com';
// redirect to the requested service if so
if(req.param('service')){
return res.redirect(req.param('service'))
}else{
return res.redirect('/dashboard');
}
});
app.get('/sessioncheck', function(req, res) {
req.session.user = 'hunter@hunterloftis.com';
// redirect to the requested service if so
if(req.param('service')){
return res.redirect(req.param('service'))
}else{
return res.redirect('/dashboard');
}
});
app.get('/dashboard', function(req, res) {
if (req.session.user) return res.send(200, 'dashboard');
var return_url = querystring.stringify({'service':'http://127.0.0.1:'+port+'/dashboard'})
return res.redirect(307, '/sessioncheck?'+return_url);
});
app.get('/redirect3', function(req, res) {
if (req.session.user){
return res.redirect('http://www.google.com')
}else{
if(req.session === undefined){
console.log('no req.session')
}else{
console.log('no req.session.user value')
}
}
var return_url = querystring.stringify({'service':'http://127.0.0.1:'+port+'/redirect3'})
return res.redirect(307, 'http://127.0.0.1:'+port+'/sessioncheck?'+return_url);
});
app.get('/redirect', function(req, res) {
if (req.session.user){
return res.redirect('http://www.google.com')
}else{
if(req.session === undefined){
console.log('no req.session')
}else{
console.log('no req.session.user value')
}
}
req.should.have.property('headers')
req.headers.should.have.property('user-agent')
var return_url = querystring.stringify({'service':'http://127.0.0.1:'+port+'/redirect'})
return res.redirect(307, 'http://127.0.0.1:'+port+'/headercheck?'+return_url);
});
// use this to test redirect to secure server after redirect works okay
app.get('/redirect2', function(req, res) {
if (req.session.user){
return res.redirect('http://www.google.com')
}
req.should.have.property('headers')
req.headers.should.have.property('user-agent')
var return_url = querystring.stringify({'service':'http://127.0.0.1:'+port+'/redirect2'})
return res.redirect(307, 'https://127.0.0.1:'+secure_port+'/headercheck?'+return_url);
});
app.listen(port);
// https.createServer({key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
// cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
// }
// ,app).listen(secure_port);
describe('request', function() {
describe('persistent agent', function() {
var agent1 = request.agent();
var agent2 = request.agent();
var agent3 = request.agent();
var agent4 = request.agent();
var agent5 = request.agent();
it('should preserve header information in a redirect sequence', function(done) {
agent1
.get('http://127.0.0.1:'+port+'/redirect')
.set('User-Agent','Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/12.0')
.end(function(err, res) {
should.not.exist(err);
res.should.have.status(200);
res.text.should.match(/Search the world's information/);
done();
});
});
it('should gain a session even with redirects', function(done) {
agent3
.get('http://127.0.0.1:'+port+'/dashboard')
.end(function(err, res) {
should.not.exist(err);
res.should.have.status(200);
res.text.should.include('dashboard')
done();
});
});
it('should gain a session even with redirects take two', function(done) {
agent4
.get('http://127.0.0.1:'+port+'/redirect3')
.end(function(err, res) {
should.not.exist(err);
res.should.have.status(200);
res.text.should.match(/Search the world's information/);
done();
});
});
// and once that works, should test that http to https redirects also work
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.