build: update example dependencies
[express.git] / test / req.protocol.js
blob453ad11ca4aa058f4ee968900650da8fc2c7ea03
2 var express = require('../')
3   , request = require('supertest');
5 describe('req', function(){
6   describe('.protocol', function(){
7     it('should return the protocol string', function(done){
8       var app = express();
10       app.use(function(req, res){
11         res.end(req.protocol);
12       });
14       request(app)
15       .get('/')
16       .expect('http', done);
17     })
19     describe('when "trust proxy" is enabled', function(){
20       it('should respect X-Forwarded-Proto', function(done){
21         var app = express();
23         app.enable('trust proxy');
25         app.use(function(req, res){
26           res.end(req.protocol);
27         });
29         request(app)
30         .get('/')
31         .set('X-Forwarded-Proto', 'https')
32         .expect('https', done);
33       })
35       it('should default to the socket addr if X-Forwarded-Proto not present', function(done){
36         var app = express();
38         app.enable('trust proxy');
40         app.use(function(req, res){
41           req.connection.encrypted = true;
42           res.end(req.protocol);
43         });
45         request(app)
46         .get('/')
47         .expect('https', done);
48       })
50       it('should ignore X-Forwarded-Proto if socket addr not trusted', function(done){
51         var app = express();
53         app.set('trust proxy', '10.0.0.1');
55         app.use(function(req, res){
56           res.end(req.protocol);
57         });
59         request(app)
60         .get('/')
61         .set('X-Forwarded-Proto', 'https')
62         .expect('http', done);
63       })
65       it('should default to http', function(done){
66         var app = express();
68         app.enable('trust proxy');
70         app.use(function(req, res){
71           res.end(req.protocol);
72         });
74         request(app)
75         .get('/')
76         .expect('http', done);
77       })
79       describe('when trusting hop count', function () {
80         it('should respect X-Forwarded-Proto', function (done) {
81           var app = express();
83           app.set('trust proxy', 1);
85           app.use(function (req, res) {
86             res.end(req.protocol);
87           });
89           request(app)
90           .get('/')
91           .set('X-Forwarded-Proto', 'https')
92           .expect('https', done);
93         })
94       })
95     })
97     describe('when "trust proxy" is disabled', function(){
98       it('should ignore X-Forwarded-Proto', function(done){
99         var app = express();
101         app.use(function(req, res){
102           res.end(req.protocol);
103         });
105         request(app)
106         .get('/')
107         .set('X-Forwarded-Proto', 'https')
108         .expect('http', done);
109       })
110     })
111   })