Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passthrough #68

Merged
merged 2 commits into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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