|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var assert = require('assert'); |
| 4 | +var express = require('express'); |
| 5 | +var nock = require('nock'); |
| 6 | +var request = require('supertest'); |
| 7 | +var proxy = require('../'); |
| 8 | + |
| 9 | + |
| 10 | +function createLocalApplicationServer() { |
| 11 | + var app = express(); |
| 12 | + return app; |
| 13 | +} |
| 14 | + |
| 15 | +describe('when proxy request is a GET', function () { |
| 16 | + |
| 17 | + this.timeout(10000); |
| 18 | + |
| 19 | + var localServer; |
| 20 | + |
| 21 | + beforeEach(function () { |
| 22 | + localServer = createLocalApplicationServer(); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(function () { |
| 26 | + nock.cleanAll(); |
| 27 | + }); |
| 28 | + |
| 29 | + var testCases = [ |
| 30 | + { name: 'form encoded', encoding: 'application/x-www-form-urlencoded' }, |
| 31 | + { name: 'JSON encoded', encoding: 'application/json' } |
| 32 | + ]; |
| 33 | + |
| 34 | + testCases.forEach(function (test) { |
| 35 | + it('should deliver the get query when ' + test.name, function (done) { |
| 36 | + var nockedPostWithEncoding = nock('http://127.0.0.1:12345') |
| 37 | + .get('/') |
| 38 | + .query({ name: 'tobi' }) |
| 39 | + .matchHeader('Content-Type', test.encoding) |
| 40 | + .reply(200, { |
| 41 | + name: 'tobi' |
| 42 | + }); |
| 43 | + |
| 44 | + localServer.use('/proxy', proxy('http://127.0.0.1:12345')); |
| 45 | + localServer.use(function (req, res) { res.sendStatus(200); }); |
| 46 | + localServer.use(function (err, req, res, next) { throw new Error(err, req, res, next); }); |
| 47 | + |
| 48 | + request(localServer) |
| 49 | + .get('/proxy') |
| 50 | + .query({ name: 'tobi' }) |
| 51 | + .set('Content-Type', test.encoding) |
| 52 | + .expect(function (res) { |
| 53 | + assert(res.body.name === 'tobi'); |
| 54 | + nockedPostWithEncoding.done(); |
| 55 | + }) |
| 56 | + .end(done); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should deliver the get body when ' + test.name, function (done) { |
| 60 | + var nockedPostWithEncoding = nock('http://127.0.0.1:12345') |
| 61 | + .get('/', { name: 'tobi' }) |
| 62 | + .matchHeader('Content-Type', test.encoding) |
| 63 | + .reply(200, { |
| 64 | + name: 'tobi' |
| 65 | + }); |
| 66 | + |
| 67 | + localServer.use('/proxy', proxy('http://127.0.0.1:12345')); |
| 68 | + localServer.use(function (req, res) { res.sendStatus(200); }); |
| 69 | + localServer.use(function (err, req, res, next) { throw new Error(err, req, res, next); }); |
| 70 | + |
| 71 | + request(localServer) |
| 72 | + .get('/proxy') |
| 73 | + .send({ name: 'tobi' }) |
| 74 | + .set('Content-Type', test.encoding) |
| 75 | + .expect(function (res) { |
| 76 | + assert(res.body.name === 'tobi'); |
| 77 | + nockedPostWithEncoding.done(); |
| 78 | + }) |
| 79 | + .end(done); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should deliver empty string get body', function (done) { |
| 84 | + var nockedPostWithoutBody = nock('http://127.0.0.1:12345') |
| 85 | + .get('/', '') |
| 86 | + .matchHeader('Content-Type', 'application/json') |
| 87 | + .reply(200, { |
| 88 | + name: 'get with string body' |
| 89 | + }); |
| 90 | + |
| 91 | + localServer.use('/proxy', proxy('http://127.0.0.1:12345')); |
| 92 | + localServer.use(function (req, res) { res.sendStatus(200); }); |
| 93 | + localServer.use(function (err, req, res, next) { throw new Error(err, req, res, next); }); |
| 94 | + |
| 95 | + request(localServer) |
| 96 | + .get('/proxy') |
| 97 | + .send() |
| 98 | + .set('Content-Type', 'application/json') |
| 99 | + .expect(function (res) { |
| 100 | + assert(res.body.name === 'get with string body'); |
| 101 | + nockedPostWithoutBody.done(); |
| 102 | + }) |
| 103 | + .end(done); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should deliver empty object get body', function (done) { |
| 107 | + var nockedPostWithoutBody = nock('http://127.0.0.1:12345') |
| 108 | + .get('/', {}) |
| 109 | + .matchHeader('Content-Type', 'application/json') |
| 110 | + .reply(200, { |
| 111 | + name: 'get with object body' |
| 112 | + }); |
| 113 | + |
| 114 | + localServer.use('/proxy', proxy('http://127.0.0.1:12345')); |
| 115 | + localServer.use(function (req, res) { res.sendStatus(200); }); |
| 116 | + localServer.use(function (err, req, res, next) { throw new Error(err, req, res, next); }); |
| 117 | + |
| 118 | + request(localServer) |
| 119 | + .get('/proxy') |
| 120 | + .send({}) |
| 121 | + .set('Content-Type', 'application/json') |
| 122 | + .expect(function (res) { |
| 123 | + assert(res.body.name === 'get with object body'); |
| 124 | + nockedPostWithoutBody.done(); |
| 125 | + }) |
| 126 | + .end(done); |
| 127 | + }); |
| 128 | + |
| 129 | +}); |
0 commit comments