koa-lite/test/request/protocol.js

55 lines
1.4 KiB
JavaScript
Raw Normal View History

'use strict';
2017-05-11 03:30:32 +00:00
const assert = require('assert');
const request = require('../helpers/context').request;
describe('req.protocol', () => {
describe('when encrypted', () => {
it('should return "https"', () => {
const req = request();
req.req.socket = { encrypted: true };
2017-05-11 03:30:32 +00:00
assert.equal(req.protocol, 'https');
2015-10-12 20:36:41 +00:00
});
});
describe('when unencrypted', () => {
it('should return "http"', () => {
const req = request();
req.req.socket = {};
2017-05-11 03:30:32 +00:00
assert.equal(req.protocol, 'http');
2015-10-12 20:36:41 +00:00
});
});
describe('when X-Forwarded-Proto is set', () => {
describe('and proxy is trusted', () => {
it('should be used', () => {
const req = request();
req.app.proxy = true;
req.req.socket = {};
req.header['x-forwarded-proto'] = 'https, http';
2017-05-11 03:30:32 +00:00
assert.equal(req.protocol, 'https');
2015-10-12 20:36:41 +00:00
});
2014-07-06 08:43:14 +00:00
describe('and X-Forwarded-Proto is empty', () => {
it('should return "http"', () => {
const req = request();
2014-07-06 08:43:14 +00:00
req.app.proxy = true;
req.req.socket = {};
req.header['x-forwarded-proto'] = '';
2017-05-11 03:30:32 +00:00
assert.equal(req.protocol, 'http');
2015-10-12 20:36:41 +00:00
});
});
});
describe('and proxy is not trusted', () => {
it('should not be used', () => {
const req = request();
req.req.socket = {};
req.header['x-forwarded-proto'] = 'https, http';
2017-05-11 03:30:32 +00:00
assert.equal(req.protocol, 'http');
2015-10-12 20:36:41 +00:00
});
});
});
});