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