diff --git a/example/rewrite/.local-web-server.json b/example/rewrite/.local-web-server.json index 9e5755c..ae46592 100644 --- a/example/rewrite/.local-web-server.json +++ b/example/rewrite/.local-web-server.json @@ -2,10 +2,6 @@ "rewrite": [ { "from": "/css/*", "to": "/build/styles/$1" }, { "from": "/npm/*", "to": "http://registry.npmjs.org/$1" }, - { "from": "/:user/repos/:name", "to": "https://api.github.com/repos/:user/:name" }, - { - "from": "/lloyd/*", - "to": "http://104.131.40.69/$1" - } + { "from": "/:user/repos/:name", "to": "https://api.github.com/repos/:user/:name" } ] } diff --git a/lib/local-web-server.js b/lib/local-web-server.js index 9db986b..8db7cd9 100644 --- a/lib/local-web-server.js +++ b/lib/local-web-server.js @@ -84,9 +84,6 @@ function localWebServer (options) { /* pretty print JSON */ app.use(json()) - /* request body parser */ - app.use(bodyParser()) - /* rewrite rules */ if (options.rewrite && options.rewrite.length) { options.rewrite.forEach(route => { @@ -104,6 +101,9 @@ function localWebServer (options) { }) } + /* must come after rewrite. See https://github.com/nodejitsu/node-http-proxy/issues/180. */ + app.use(bodyParser()) + /* path blacklist */ if (options.forbid.length) { debug('forbid', options.forbid.join(', ')) diff --git a/lib/middleware.js b/lib/middleware.js index da1e5e3..a5359ca 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -50,6 +50,7 @@ function proxyRequest (route, app) { proxy.once('proxyReq', function (proxyReq) { proxyReq.path = url.parse(route.new).path }) + proxy.web(this.req, this.res, { target: route.new }) } } diff --git a/test/rewrite-proxy.js b/test/rewrite-proxy.js new file mode 100644 index 0000000..3e884e2 --- /dev/null +++ b/test/rewrite-proxy.js @@ -0,0 +1,89 @@ +'use strict' +const test = require('tape') +const request = require('req-then') +const localWebServer = require('../') +const http = require('http') + +function launchServer (app, options) { + options = options || {} + const path = `http://localhost:8100${options.path || '/'}` + const server = http.createServer(app.callback()) + return server.listen(options.port || 8100, () => { + const req = request(path, options.reqOptions) + if (options.onSuccess) req.then(options.onSuccess) + if (!options.leaveOpen) req.then(() => server.close()) + req.catch(err => console.error('LAUNCH ERROR', err.stack)) + }) +} + +function checkResponse (t, status, body) { + return function (response) { + if (status) t.strictEqual(response.res.statusCode, status) + if (body) t.ok(body.test(response.data)) + } +} + +test('rewrite: proxy', function (t) { + t.plan(2) + const app = localWebServer({ + log: { format: 'none' }, + static: { root: __dirname + '/fixture/rewrite' }, + rewrite: [ { from: '/test/*', to: 'http://registry.npmjs.org/$1' } ] + }) + launchServer(app, { path: '/test/', onSuccess: response => { + t.strictEqual(response.res.statusCode, 200) + t.ok(/db_name/.test(response.data)) + }}) +}) + +test('rewrite: proxy, POST', function (t) { + t.plan(1) + const app = localWebServer({ + log: { format: 'none' }, + static: { root: __dirname + '/fixture/rewrite' }, + rewrite: [ { from: '/test/*', to: 'http://registry.npmjs.org/' } ] + }) + const server = http.createServer(app.callback()) + server.listen(8100, () => { + request('http://localhost:8100/test/', { data: {} }) + .then(checkResponse(t, 405)) + .then(server.close.bind(server)) + }) +}) + +test('rewrite: proxy, two url tokens', function (t) { + t.plan(2) + const app = localWebServer({ + log: { format: 'none' }, + rewrite: [ { from: '/:package/:version', to: 'http://registry.npmjs.org/:package/:version' } ] + }) + launchServer(app, { path: '/command-line-args/1.0.0', onSuccess: response => { + t.strictEqual(response.res.statusCode, 200) + t.ok(/command-line-args/.test(response.data)) + }}) +}) + +test('rewrite: proxy with port', function (t) { + t.plan(2) + const one = localWebServer({ + log: { format: 'none' }, + static: { root: __dirname + '/fixture/one' } + }) + const two = localWebServer({ + log: { format: 'none' }, + static: { root: __dirname + '/fixture/spa' }, + rewrite: [ { from: '/test/*', to: 'http://localhost:9000/$1' } ] + }) + const server1 = http.createServer(one.callback()) + const server2 = http.createServer(two.callback()) + server1.listen(9000, () => { + server2.listen(8100, () => { + request('http://localhost:8100/test/file.txt').then(response => { + t.strictEqual(response.res.statusCode, 200) + t.ok(/one/.test(response.data)) + server1.close() + server2.close() + }) + }) + }) +}) diff --git a/test/test.js b/test/test.js index 22dc2fb..c67f158 100644 --- a/test/test.js +++ b/test/test.js @@ -143,56 +143,6 @@ test('rewrite: local', function (t) { }}) }) -test('rewrite: proxy', function (t) { - t.plan(2) - const app = localWebServer({ - log: { format: 'none' }, - static: { root: __dirname + '/fixture/rewrite' }, - rewrite: [ { from: '/test/*', to: 'http://registry.npmjs.org/$1' } ] - }) - launchServer(app, { path: '/test/', onSuccess: response => { - t.strictEqual(response.res.statusCode, 200) - t.ok(/db_name/.test(response.data)) - }}) -}) - -test('rewrite: proxy, two url tokens', function (t) { - t.plan(2) - const app = localWebServer({ - log: { format: 'none' }, - rewrite: [ { from: '/:package/:version', to: 'http://registry.npmjs.org/:package/:version' } ] - }) - launchServer(app, { path: '/command-line-args/1.0.0', onSuccess: response => { - t.strictEqual(response.res.statusCode, 200) - t.ok(/command-line-args/.test(response.data)) - }}) -}) - -test('rewrite: proxy with port', function (t) { - t.plan(2) - const one = localWebServer({ - log: { format: 'none' }, - static: { root: __dirname + '/fixture/one' } - }) - const two = localWebServer({ - log: { format: 'none' }, - static: { root: __dirname + '/fixture/spa' }, - rewrite: [ { from: '/test/*', to: 'http://localhost:9000/$1' } ] - }) - const server1 = http.createServer(one.callback()) - const server2 = http.createServer(two.callback()) - server1.listen(9000, () => { - server2.listen(8100, () => { - request('http://localhost:8100/test/file.txt').then(response => { - t.strictEqual(response.res.statusCode, 200) - t.ok(/one/.test(response.data)) - server1.close() - server2.close() - }) - }) - }) -}) - test('mock: simple response', function (t) { t.plan(2) const app = localWebServer({