refactor some tests for greater readability

This commit is contained in:
Matt Hauck 2015-03-09 11:49:28 -07:00
parent 62e4b75101
commit 14415a5074

View File

@ -6,113 +6,110 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
beforeEach(function() { beforeEach(function() {
this.req = { this.req = {
headers: { headers: {
host: "x2.com" host: "ext-auto.com"
} }
}; };
this.proxyRes = { this.proxyRes = {
statusCode: 301, statusCode: 301,
headers: { headers: {
location: "http://f.com/" location: "http://backend.com/"
} }
}; };
this.options = {
target: "http://backend.com"
};
}); });
context('rewrites location host with hostRewrite', function() { context('rewrites location host with hostRewrite', function() {
beforeEach(function() { beforeEach(function() {
this.options = { this.options.hostRewrite = "ext-manual.com";
hostRewrite: "x.com"
};
}); });
[301, 302, 307, 308].forEach(function(code) { [301, 302, 307, 308].forEach(function(code) {
it('on ' + code, function() { it('on ' + code, function() {
this.proxyRes.statusCode = code; this.proxyRes.statusCode = code;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options); httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://'+this.options.hostRewrite+'/'); expect(this.proxyRes.headers.location).to.eql('http://ext-manual.com/');
}); });
}); });
it('not on 200', function() { it('not on 200', function() {
this.proxyRes.statusCode = 200; this.proxyRes.statusCode = 200;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options); httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://f.com/'); expect(this.proxyRes.headers.location).to.eql('http://backend.com/');
}); });
it('not when hostRewrite is unset', function() { it('not when hostRewrite is unset', function() {
delete this.options.hostRewrite; delete this.options.hostRewrite;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options); httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://f.com/'); expect(this.proxyRes.headers.location).to.eql('http://backend.com/');
}); });
it('takes precedence over autoRewrite', function() { it('takes precedence over autoRewrite', function() {
this.options.autoRewrite = true; this.options.autoRewrite = true;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options); httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://'+this.options.hostRewrite+'/'); expect(this.proxyRes.headers.location).to.eql('http://ext-manual.com/');
}); });
}); });
context('rewrites location host with autoRewrite', function() { context('rewrites location host with autoRewrite', function() {
beforeEach(function() { beforeEach(function() {
this.options = { this.options.autoRewrite = true;
autoRewrite: true,
};
}); });
[301, 302, 307, 308].forEach(function(code) { [301, 302, 307, 308].forEach(function(code) {
it('on ' + code, function() { it('on ' + code, function() {
this.proxyRes.statusCode = code; this.proxyRes.statusCode = code;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options); httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://'+this.req.headers.host+'/'); expect(this.proxyRes.headers.location).to.eql('http://ext-auto.com/');
}); });
}); });
it('not on 200', function() { it('not on 200', function() {
this.proxyRes.statusCode = 200; this.proxyRes.statusCode = 200;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options); httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://f.com/'); expect(this.proxyRes.headers.location).to.eql('http://backend.com/');
}); });
it('not when autoRewrite is unset', function() { it('not when autoRewrite is unset', function() {
delete this.options.autoRewrite; delete this.options.autoRewrite;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options); httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://f.com/'); expect(this.proxyRes.headers.location).to.eql('http://backend.com/');
}); });
}); });
context('rewrites location protocol with protocolRewrite', function() { context('rewrites location protocol with protocolRewrite', function() {
beforeEach(function() { beforeEach(function() {
this.options = { this.options.protocolRewrite = 'https';
protocolRewrite: 'https',
};
}); });
[301, 302, 307, 308].forEach(function(code) { [301, 302, 307, 308].forEach(function(code) {
it('on ' + code, function() { it('on ' + code, function() {
this.proxyRes.statusCode = code; this.proxyRes.statusCode = code;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options); httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('https://f.com/'); expect(this.proxyRes.headers.location).to.eql('https://backend.com/');
}); });
}); });
it('not on 200', function() { it('not on 200', function() {
this.proxyRes.statusCode = 200; this.proxyRes.statusCode = 200;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options); httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://f.com/'); expect(this.proxyRes.headers.location).to.eql('http://backend.com/');
}); });
it('not when protocolRewrite is unset', function() { it('not when protocolRewrite is unset', function() {
delete this.options.protocolRewrite; delete this.options.protocolRewrite;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options); httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://f.com/'); expect(this.proxyRes.headers.location).to.eql('http://backend.com/');
}); });
it('works together with hostRewrite', function() { it('works together with hostRewrite', function() {
this.options.hostRewrite = 'x.com' this.options.hostRewrite = 'ext-manual.com'
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options); httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('https://x.com/'); expect(this.proxyRes.headers.location).to.eql('https://ext-manual.com/');
}); });
it('works together with autoRewrite', function() { it('works together with autoRewrite', function() {
this.options.autoRewrite = true this.options.autoRewrite = true
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options); httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('https://x2.com/'); expect(this.proxyRes.headers.location).to.eql('https://ext-auto.com/');
}); });
}); });
}); });