UNPKG

2.53 kBJavaScriptView Raw
1/* global describe, it, before, beforeEach, after, afterEach */
2var should = require('should');
3var through = require('through');
4var _ = require('lodash');
5var wrap = require('../wrap');
6
7var Waif = require('../');
8
9describe('headers can be configured', function() {
10 var waif = null;
11 var request = null;
12
13 before(function() {
14 waif = Waif.createInstance();
15
16 this.service = waif('local')
17 .use(requestHeaders, {'request-header': 'abc'})
18 .use(responseHeaders, {'response-header': '123'})
19 .use(_middleware)
20 .listen(3006);
21
22 waif('proxy')
23 .pipe('/path/:something/here', 'http://localhost/here/:something/path', { redirect: true })
24 .pipe('http://localhost:3006', { headers: { 'proxy-header': 'doremi'} })
25 .listen(0);
26
27 waif.start();
28
29 function _middleware() {
30 return function(req, res, next) {
31 request = req;
32 res.send({msg: 'ok'});
33 };
34 }
35 });
36
37 after(function() { waif.stop(); });
38
39 it('request headers', function(doneFn) {
40 var local = waif('local');
41
42 local('/path/here', test);
43 function test(err, resp, body) {
44 request.headers.should.have.property('request-header', 'abc');
45 doneFn();
46 }
47 });
48
49 it('response header', function(doneFn) {
50 var local = waif('local');
51
52 local('/path/here', test);
53 function test(err, resp, body) {
54 resp.headers.should.have.property('response-header', '123');
55 should.not.exist(err);
56
57 doneFn();
58 }
59 });
60
61 it('proxy header', function(doneFn) {
62 var proxy = waif('proxy');
63
64 proxy('/path/here', test);
65 function test(err, resp, body) {
66 request.headers.should.have.property('host', 'localhost');
67 request.headers.should.have.property('proxy-header', 'doremi');
68 should.not.exist(err);
69
70 doneFn();
71 }
72 });
73
74 it('proxy redirect', function(doneFn) {
75 var proxy = waif('proxy');
76 var opts = {
77 url: '/path/goes/here',
78 followRedirect: false
79 };
80 proxy(opts, test);
81 function test(err, resp, body) {
82 should.not.exist(err);
83 resp.statusCode.should.equal(301);
84 resp.headers.should.have.property('location', 'http://localhost/here/goes/path');
85
86 doneFn();
87 }
88 });
89});
90
91//// Helpers
92
93function requestHeaders(config) {
94 return function(req, res, next) {
95 _.extend(req.headers, config);
96 next();
97 };
98}
99
100function responseHeaders(config) {
101 return function(req, res, next) {
102 _.each(config, function(val, key) {
103 res.setHeader(key, val);
104 });
105 next();
106 };
107}