Skip to content

Commit

Permalink
Merge pull request #68 from Shudrum/passthrough
Browse files Browse the repository at this point in the history
Passthrough
  • Loading branch information
Shudrum committed Mar 13, 2020
2 parents 74ffbb8 + 097f4b4 commit cdd8646
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nodegate",
"description": "API gateway made simple, fast and easy to configure.",
"version": "1.4.0",
"version": "1.5.0",
"author": "Julien Martin <martin.julien82@gmail.com>",
"license": "MIT",
"scripts": {
Expand Down
14 changes: 12 additions & 2 deletions services/nodegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const bodyParser = require('body-parser');
const cors = require('cors');
const express = require('express');
const request = require('request');
const { execute } = require('../entities/route');
const { getConfiguration } = require('../services/configuration');

Expand Down Expand Up @@ -44,12 +45,21 @@ const nodegate = () => {
expressApp.handle(req, res, next);
};

// TODO: app.passthrough = (route) => {};

app.beforeEach = (workers) => {
toArray(workers).forEach((worker) => beforeEach.push(worker));
};

app.passthrough = (routes) => {
toArray(routes).forEach((route) => {
expressApp[route.method.toLowerCase()](route.path, (req, res) => {
request[route.method.toLowerCase()](route.target, {
...(req.headers && { headers: req.headers }),
...(req.body && { form: req.body }),
}).pipe(res);
});
});
};

app.route = (routes) => {
toArray(routes).forEach((route) => {
expressApp[route.method.toLowerCase()](
Expand Down
56 changes: 56 additions & 0 deletions test/services/nodegate.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const nock = require('nock');
const request = require('supertest');
const WorkflowError = require('../../entities/WorkflowError');
const nodegate = require('../../services/nodegate');
Expand Down Expand Up @@ -83,6 +84,61 @@ describe('services/nodegate', () => {
});
});
});
describe('#passthrough', () => {
it('should call the target URL with the right verb', async () => {
nock('http://service.com').get('/').reply(200);
const gate = nodegate();
gate.passthrough({
method: 'get',
path: '/service',
target: 'http://service.com',
});
await request(gate).get('/service').expect(200);
});
it('should call the target URL with the headers', async () => {
nock('http://service.com', {
reqheaders: {
authorization: 'shu:drum',
},
}).get('/').reply(200);
const gate = nodegate();
gate.passthrough({
method: 'get',
path: '/service',
target: 'http://service.com',
});
await request(gate)
.get('/service')
.set('authorization', 'shu:drum')
.expect(200);
});
it('should call the target URL with the body', async () => {
nock('http://service.com').get('/', { username: 'shudrum' }).reply(200);
const gate = nodegate();
gate.passthrough({
method: 'get',
path: '/service',
target: 'http://service.com',
});
await request(gate)
.get('/service')
.send({ username: 'shudrum' })
.expect(200);
});
it('should return the raw body', async () => {
nock('http://service.com').get('/').reply(200, 'Multiline\nContent');
const gate = nodegate();
gate.passthrough({
method: 'get',
path: '/service',
target: 'http://service.com',
});
const { text } = await request(gate)
.get('/service')
.expect(200);
expect(text).toEqual('Multiline\nContent');
});
});
describe('HTTP status codes', () => {
it('should respond a 500 error in case of error', async () => {
const gate = nodegate();
Expand Down

0 comments on commit cdd8646

Please sign in to comment.