From 1ef34f289940a849550de70bff3b2f5f88310102 Mon Sep 17 00:00:00 2001 From: Jiby Jose Date: Thu, 26 Nov 2015 00:18:14 +0800 Subject: [PATCH 01/11] Use request to proxy 404 requests if configured --- lib/canned.js | 21 ++++++++++++++++----- package.json | 3 ++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/canned.js b/lib/canned.js index 087dd57..80394d3 100644 --- a/lib/canned.js +++ b/lib/canned.js @@ -9,10 +9,12 @@ var url = require('url') var cannedUtils = require('./utils') var lookup = require('./lookup') var _ = require('lodash') +var request = require('request') function Canned(dir, options) { this.logger = options.logger this.wildcard = options.wildcard || 'any' + this.proxy = options.proxy this.response_opts = { cors_enabled: options.cors, cors_headers: options.cors_headers @@ -224,8 +226,7 @@ Canned.prototype._responseForFile = function (httpObj, files, cb) { fs.readFile(filePath, { encoding: 'utf8' }, function (err, data) { var response if (err) { - response = new Response(getContentType('html'), '', 404, httpObj.res, that.response_opts) - cb('Not found', response) + cb('Not found', httpObj.res) } else { var _data = that.getVariableResponse(data, httpObj.content, httpObj.headers) data = _data.data @@ -243,8 +244,7 @@ Canned.prototype._responseForFile = function (httpObj, files, cb) { } }) } else { - var response = new Response(getContentType('html'), '', 404, httpObj.res, that.response_opts) - cb('Not found', response) + cb('Not found', httpObj.res) } } @@ -320,8 +320,19 @@ Canned.prototype.responder = function(body, req, res) { httpObj.path = that.dir + paths.splice(0, 1)[0]; httpObj.fname = '_' + httpObj.dname; return that.findResponse(httpObj, responseHandler); - } else { + } else if (that.proxy){ + // proxy to configured url + var proxyUrl = that.proxy + (parsedurl.path || '') + (parsedurl.query || '')+ (parsedurl.hash || ''); + that._log(' proxying request to ' + proxyUrl + '\n'); + return req.pipe(request(proxyUrl)) + .on('error', function(err) { + that._log(' proxy gave error ' + err.code + '\n'); + resp = new Response(getContentType('html'), '', 404, httpObj.res, that.response_opts) + resp.send(); + }).pipe(resp); + }else { that._log(' not found\n'); + resp = new Response(getContentType('html'), '', 404, httpObj.res, that.response_opts) } } else { that._logHTTPObject(httpObj) diff --git a/package.json b/package.json index 3283531..59e88fc 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ }, "dependencies": { "lodash": "^3.10.1", - "optimist": "^0.6.0" + "optimist": "^0.6.0", + "request": "^2.67.0" }, "devDependencies": { "jasmine-node": "^1.14.2", From e0e3886238bf906f9c583bc825c49d1411938f31 Mon Sep 17 00:00:00 2001 From: Jiby Jose Date: Thu, 26 Nov 2015 01:14:45 +0800 Subject: [PATCH 02/11] only consume request stream if we need to MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Request stream only should be consumed if we could find the path in mock files, else don’t consume as the stream will be needed to proxy the request. --- lib/canned.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/canned.js b/lib/canned.js index 80394d3..8eeceb3 100644 --- a/lib/canned.js +++ b/lib/canned.js @@ -228,6 +228,7 @@ Canned.prototype._responseForFile = function (httpObj, files, cb) { if (err) { cb('Not found', httpObj.res) } else { + that._extractRequestContent(httpObj); var _data = that.getVariableResponse(data, httpObj.content, httpObj.headers) data = _data.data var statusCode = _data.statusCode @@ -278,14 +279,13 @@ Canned.prototype.respondWithAny = function (httpObj, files, cb) { }) } -Canned.prototype.responder = function(body, req, res) { +Canned.prototype.responder = function(req, res) { var responseHandler var httpObj = {} var that = this var parsedurl = url.parse(req.url) httpObj.headers = req.headers httpObj.accept = (req.headers && req.headers.accept) ? req.headers.accept.trim().split(',') : [] - httpObj.content = body httpObj.pathname = parsedurl.pathname.split('/') httpObj.dname = httpObj.pathname.pop() httpObj.fname = '_' + httpObj.dname @@ -293,6 +293,7 @@ Canned.prototype.responder = function(body, req, res) { httpObj.query = parsedurl.query httpObj.method = req.method.toLowerCase() httpObj.res = res + httpObj.req = req httpObj.ctype = '' this._log('request: ' + httpObj.method + ' ' + req.url) @@ -368,40 +369,45 @@ Canned.prototype.findResponse = function(httpObj, cb) { }) } -Canned.prototype.responseFilter = function (req, res) { +Canned.prototype._extractRequestContent = function (httpObj) { var that = this var body = '' // assemble response body if GET/POST/PUT - switch(req.method) { + switch(httpObj.req.method) { case 'PUT': case 'POST': - req.on('data', function (data) { + httpObj.req.on('data', function (data) { body += data }) - req.on('end', function () { + httpObj.req.on('end', function () { var responderBody = querystring.parse(body); - if (isContentTypeJson(req)) { + if (isContentTypeJson(httpObj.req)) { try { responderBody = JSON.parse(body) } catch (e) { that._log('Invalid json content') } } - that.responder(responderBody, req, res) + httpObj.content = responderBody; }) break case 'GET': - var query = url.parse(req.url).query + var query = url.parse(httpObj.req.url).query if (query && query.length > 0) { body = querystring.parse(query) } - that.responder(body, req, res) + httpObj.content = body; break default: - that.responder(body, req, res) + httpObj.content = body; break } } +Canned.prototype.responseFilter = function (req, res) { + var that = this + that.responder(req, res); +} + module.exports = Canned; \ No newline at end of file From 94c958c308dcdc6a767cff60e309c914e85b7808 Mon Sep 17 00:00:00 2001 From: Jiby Jose Date: Thu, 26 Nov 2015 01:41:15 +0800 Subject: [PATCH 03/11] refactor code --- lib/canned.js | 72 ++++++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/lib/canned.js b/lib/canned.js index 8eeceb3..30da7eb 100644 --- a/lib/canned.js +++ b/lib/canned.js @@ -228,20 +228,21 @@ Canned.prototype._responseForFile = function (httpObj, files, cb) { if (err) { cb('Not found', httpObj.res) } else { - that._extractRequestContent(httpObj); - var _data = that.getVariableResponse(data, httpObj.content, httpObj.headers) - data = _data.data - var statusCode = _data.statusCode - var content = that.sanatizeContent(data, fileObject) - - if (content !== false) { - response = new Response(_data.contentType || getContentType(fileObject.mimetype), content, statusCode, httpObj.res, that.response_opts, _data.customHeaders) - cb(null, response) - } else { - content = 'Internal Server error invalid input file' - response = new Response(getContentType('html'), content, 500, httpObj.res, that.response_opts) - cb(null, response) - } + that._extractRequestContent(httpObj.req, function (content) { + var _data = that.getVariableResponse(data, content, httpObj.headers) + data = _data.data + var statusCode = _data.statusCode + content = that.sanatizeContent(data, fileObject) + + if (content !== false) { + response = new Response(_data.contentType || getContentType(fileObject.mimetype), content, statusCode, httpObj.res, that.response_opts, _data.customHeaders) + cb(null, response) + } else { + content = 'Internal Server error invalid input file' + response = new Response(getContentType('html'), content, 500, httpObj.res, that.response_opts) + cb(null, response) + } + }); } }) } else { @@ -322,15 +323,7 @@ Canned.prototype.responder = function(req, res) { httpObj.fname = '_' + httpObj.dname; return that.findResponse(httpObj, responseHandler); } else if (that.proxy){ - // proxy to configured url - var proxyUrl = that.proxy + (parsedurl.path || '') + (parsedurl.query || '')+ (parsedurl.hash || ''); - that._log(' proxying request to ' + proxyUrl + '\n'); - return req.pipe(request(proxyUrl)) - .on('error', function(err) { - that._log(' proxy gave error ' + err.code + '\n'); - resp = new Response(getContentType('html'), '', 404, httpObj.res, that.response_opts) - resp.send(); - }).pipe(resp); + return that._proxyRequest(httpObj) }else { that._log(' not found\n'); resp = new Response(getContentType('html'), '', 404, httpObj.res, that.response_opts) @@ -346,6 +339,20 @@ Canned.prototype.responder = function(req, res) { } +Canned.prototype._proxyRequest = function (httpObj) { + var that = this; + var parsedurl = url.parse(httpObj.req.url) + // proxy to configured url + var proxyUrl = that.proxy + (parsedurl.path || '') + (parsedurl.query || '')+ (parsedurl.hash || ''); + that._log(' proxying request to ' + proxyUrl + '\n'); + return httpObj.req.pipe(request(proxyUrl)) + .on('error', function(err) { + that._log(' proxy gave error ' + err.code + '\n'); + var resp = new Response(getContentType('html'), '', 404, httpObj.res, that.response_opts) + resp.send(); + }).pipe(httpObj.res); +} + Canned.prototype.findResponse = function(httpObj, cb) { var that = this; fs.readdir(httpObj.path, function (err, files) { @@ -369,38 +376,37 @@ Canned.prototype.findResponse = function(httpObj, cb) { }) } -Canned.prototype._extractRequestContent = function (httpObj) { +Canned.prototype._extractRequestContent = function (req, callback) { var that = this var body = '' - // assemble response body if GET/POST/PUT - switch(httpObj.req.method) { + switch(req.method) { case 'PUT': case 'POST': - httpObj.req.on('data', function (data) { + req.on('data', function (data) { body += data }) - httpObj.req.on('end', function () { + req.on('end', function () { var responderBody = querystring.parse(body); - if (isContentTypeJson(httpObj.req)) { + if (isContentTypeJson(req)) { try { responderBody = JSON.parse(body) } catch (e) { that._log('Invalid json content') } } - httpObj.content = responderBody; + callback(responderBody); }) break case 'GET': - var query = url.parse(httpObj.req.url).query + var query = url.parse(req.url).query if (query && query.length > 0) { body = querystring.parse(query) } - httpObj.content = body; + callback(body) break default: - httpObj.content = body; + callback(body) break } } From 0b6d68c8ca6fc28230b1737bc0fce8a627e81205 Mon Sep 17 00:00:00 2001 From: Jiby Jose Date: Thu, 26 Nov 2015 01:49:27 +0800 Subject: [PATCH 04/11] remove dead code, fix typo in comment --- index.js | 2 +- lib/canned.js | 9 +-------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index eb3c1cc..c0c2b53 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,7 @@ var canned = function (dir, options) { if (!options) options = {} dir = path.relative(process.cwd(), dir) var c = new Canned(dir, options) - return c.responseFilter.bind(c) + return c.responder.bind(c) } module.exports = canned diff --git a/lib/canned.js b/lib/canned.js index 30da7eb..d3bf58a 100644 --- a/lib/canned.js +++ b/lib/canned.js @@ -233,7 +233,6 @@ Canned.prototype._responseForFile = function (httpObj, files, cb) { data = _data.data var statusCode = _data.statusCode content = that.sanatizeContent(data, fileObject) - if (content !== false) { response = new Response(_data.contentType || getContentType(fileObject.mimetype), content, statusCode, httpObj.res, that.response_opts, _data.customHeaders) cb(null, response) @@ -342,7 +341,6 @@ Canned.prototype.responder = function(req, res) { Canned.prototype._proxyRequest = function (httpObj) { var that = this; var parsedurl = url.parse(httpObj.req.url) - // proxy to configured url var proxyUrl = that.proxy + (parsedurl.path || '') + (parsedurl.query || '')+ (parsedurl.hash || ''); that._log(' proxying request to ' + proxyUrl + '\n'); return httpObj.req.pipe(request(proxyUrl)) @@ -379,7 +377,7 @@ Canned.prototype.findResponse = function(httpObj, cb) { Canned.prototype._extractRequestContent = function (req, callback) { var that = this var body = '' - // assemble response body if GET/POST/PUT + // assemble request body if GET/POST/PUT switch(req.method) { case 'PUT': case 'POST': @@ -411,9 +409,4 @@ Canned.prototype._extractRequestContent = function (req, callback) { } } -Canned.prototype.responseFilter = function (req, res) { - var that = this - that.responder(req, res); -} - module.exports = Canned; \ No newline at end of file From dfa1600043d3f530d440f9d5891cbcee91e95c11 Mon Sep 17 00:00:00 2001 From: Jiby Jose Date: Thu, 26 Nov 2015 02:23:20 +0800 Subject: [PATCH 05/11] add tests for proxy feature --- spec/canned.spec.js | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/spec/canned.spec.js b/spec/canned.spec.js index 860eb4d..9b1a62b 100644 --- a/spec/canned.spec.js +++ b/spec/canned.spec.js @@ -702,4 +702,56 @@ describe('canned', function () { }) }) + describe("proxy requests for unknown paths", function () { + it('should return 404 if path unknown and proxy not configured', function (done) { + req.url = '/unkown_path' + res.end = function (content) { + expect(res.statusCode).toEqual(404); + done() + } + can(req, res) + }) + + it('should return mock if path known and proxy configured', function (done) { + var can = canned('./spec/test_responses', { + proxy: 'http://localhost:9615' + }) + + req.url = '/a' + res.end = function (content) { + expect(res.statusCode).toEqual(200); + done() + } + can(req, res) + }) + + it('should proxy request if path unknown and proxy is configured', function (done) { + var proxy = require('http').createServer(function (req, res) { + res.writeHead(200, {'Content-Type': 'text/plain'}); + res.end('OK'); + req.on('data', function (data) { + expect(data.toString()).toEqual('test'); + }) + }).listen(9615); + + var can = canned('./spec/test_responses', { + proxy: 'http://localhost:9615' + }) + + var req = new require('stream').Readable(); + req._read = function noop() {}; + req.push('test'); + req.method = 'POST' + req.url = '/unkown_path' + + var res = new require('stream').Writable(); + res._write = function noop(data) { + expect(data.toString()).toEqual('OK'); + proxy.close() + done() + }; + can(req, res) + }) + }) + }) From 933f9967e874514410a0c49d3a9c1d233f4e5f89 Mon Sep 17 00:00:00 2001 From: Jiby Jose Date: Thu, 26 Nov 2015 02:33:29 +0800 Subject: [PATCH 06/11] honour proxy option in command line binary as well --- bin/canned | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/bin/canned b/bin/canned index 6b83918..8a5f8b0 100755 --- a/bin/canned +++ b/bin/canned @@ -14,6 +14,8 @@ var canned = require('../index') .describe('cors', 'disable cors support') .default('headers', false) .describe('headers', 'add custom headers allowed in cors requests') + .default('proxy', '') + .describe('proxy', 'proxy unknown paths to this domain') .default('h', false) .alias('h', 'help') .describe('h', 'show the help') @@ -29,6 +31,7 @@ var dir = '' , port = argv.p , cors = argv.cors , cors_headers = argv.headers +, proxy = argv.proxy , logger , cannedDir , wildcard = argv.wildcard @@ -42,6 +45,12 @@ if (argv.q) { process.stdout.write('starting canned on port ' + port + ' for ' + cannedDir + '\n') } -var can = canned(dir, { logger: logger, cors: cors, cors_headers: cors_headers, wildcard: wildcard}) +var can = canned(dir, { + logger: logger, + cors: cors, + cors_headers: cors_headers, + wildcard: wildcard, + proxy: proxy +}) http.createServer(can).listen(port) From 3899fd417da54da3dc38ec9ac0a505d0035c517e Mon Sep 17 00:00:00 2001 From: Jiby Jose Date: Thu, 26 Nov 2015 14:40:18 +0800 Subject: [PATCH 07/11] update changeling --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2d0d405..2b029a0 100644 --- a/README.md +++ b/README.md @@ -295,6 +295,7 @@ Release History --------------- ### next * fix improper handling of carriage return in windows #79 (@git-jiby-me) +* support to proxy unknown paths to another domain #56 (@git-jiby-me) ### 0.3.7 * The regex for matching request, was not considering arrays in the request JSON From 86ae63e103527a1ce75bbd08fbd33cc657b98f2e Mon Sep 17 00:00:00 2001 From: Jiby Jose Date: Tue, 1 Dec 2015 08:56:05 +0800 Subject: [PATCH 08/11] modularise proxyResponse --- bin/canned | 2 +- lib/canned.js | 36 ++++++++++++++---------------------- lib/proxyResponse.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 23 deletions(-) create mode 100644 lib/proxyResponse.js diff --git a/bin/canned b/bin/canned index 8a5f8b0..63984c8 100755 --- a/bin/canned +++ b/bin/canned @@ -14,7 +14,7 @@ var canned = require('../index') .describe('cors', 'disable cors support') .default('headers', false) .describe('headers', 'add custom headers allowed in cors requests') - .default('proxy', '') + .default('proxy', false) .describe('proxy', 'proxy unknown paths to this domain') .default('h', false) .alias('h', 'help') diff --git a/lib/canned.js b/lib/canned.js index d3bf58a..d6c19c7 100644 --- a/lib/canned.js +++ b/lib/canned.js @@ -5,7 +5,6 @@ var fs = require('fs') var util = require('util') var Response = require('./response') var querystring = require('querystring') -var url = require('url') var cannedUtils = require('./utils') var lookup = require('./lookup') var _ = require('lodash') @@ -226,9 +225,9 @@ Canned.prototype._responseForFile = function (httpObj, files, cb) { fs.readFile(filePath, { encoding: 'utf8' }, function (err, data) { var response if (err) { - cb('Not found', httpObj.res) + cb(new Error('Not found')) } else { - that._extractRequestContent(httpObj.req, function (content) { + that._extractRequestContent(httpObj.req, function (err, content) { var _data = that.getVariableResponse(data, content, httpObj.headers) data = _data.data var statusCode = _data.statusCode @@ -245,7 +244,7 @@ Canned.prototype._responseForFile = function (httpObj, files, cb) { } }) } else { - cb('Not found', httpObj.res) + cb(new Error('Not found')) } } @@ -314,6 +313,11 @@ Canned.prototype.responder = function(req, res) { var paths = lookup(httpObj.pathname.join('/'), that.wildcard); paths.splice(0,1); // The first path is the default + proxyErrorHandler = function (err) { + that._log(' proxy gave error ' + err.code + '\n'); + resp = new Response(getContentType('html'), '', 404, httpObj.res, that.response_opts) + resp.send(); + } responseHandler = function (err, resp) { if (err) { // Try more paths, if there are any still @@ -322,7 +326,8 @@ Canned.prototype.responder = function(req, res) { httpObj.fname = '_' + httpObj.dname; return that.findResponse(httpObj, responseHandler); } else if (that.proxy){ - return that._proxyRequest(httpObj) + that._log(' proxying request to ' + proxyUrl + '\n'); + resp = new ProxyResponse(that.proxy, httpObj.req, httpObj.res, proxyErrorHandler) }else { that._log(' not found\n'); resp = new Response(getContentType('html'), '', 404, httpObj.res, that.response_opts) @@ -338,19 +343,6 @@ Canned.prototype.responder = function(req, res) { } -Canned.prototype._proxyRequest = function (httpObj) { - var that = this; - var parsedurl = url.parse(httpObj.req.url) - var proxyUrl = that.proxy + (parsedurl.path || '') + (parsedurl.query || '')+ (parsedurl.hash || ''); - that._log(' proxying request to ' + proxyUrl + '\n'); - return httpObj.req.pipe(request(proxyUrl)) - .on('error', function(err) { - that._log(' proxy gave error ' + err.code + '\n'); - var resp = new Response(getContentType('html'), '', 404, httpObj.res, that.response_opts) - resp.send(); - }).pipe(httpObj.res); -} - Canned.prototype.findResponse = function(httpObj, cb) { var that = this; fs.readdir(httpObj.path, function (err, files) { @@ -374,7 +366,7 @@ Canned.prototype.findResponse = function(httpObj, cb) { }) } -Canned.prototype._extractRequestContent = function (req, callback) { +Canned.prototype._extractRequestContent = function (req, cb) { var that = this var body = '' // assemble request body if GET/POST/PUT @@ -393,7 +385,7 @@ Canned.prototype._extractRequestContent = function (req, callback) { that._log('Invalid json content') } } - callback(responderBody); + cb(null, responderBody); }) break case 'GET': @@ -401,10 +393,10 @@ Canned.prototype._extractRequestContent = function (req, callback) { if (query && query.length > 0) { body = querystring.parse(query) } - callback(body) + cb(null, body) break default: - callback(body) + cb(null, body) break } } diff --git a/lib/proxyResponse.js b/lib/proxyResponse.js new file mode 100644 index 0000000..955aabc --- /dev/null +++ b/lib/proxyResponse.js @@ -0,0 +1,34 @@ +"use strict"; +var fs = require('fs') +var request = require('request'); +var url = require('url'); + +function ProxyResponse(proxy, req, res, errorHandler) { + var that = this; + that.proxyUrl = buildProxyUrl(req.url, proxy); + that.req = req; + that.res = res; + that.errorHandler = errorHandler; +} + +ProxyResponse.prototype.send = function () { + var that = this; + var proxy = request(that.proxyUrl); + that.req.pipe(proxy) + .on('error', function(err) { + that.errorHandler() + }) + .pipe(that.res); +} + +function buildProxyUrl(originalUrl, proxy) { + var parsedurl = url.parse(originalUrl), proxyUrl; + proxyUrl = proxy + + (parsedurl.path || '') + + (parsedurl.query || '')+ + (parsedurl.hash || ''); + return proxyUrl; + +} + +module.exports = ProxyResponse From 8e816acefb9709f328db00d14079cabd6a884816 Mon Sep 17 00:00:00 2001 From: Jiby Jose Date: Tue, 1 Dec 2015 11:04:54 +0800 Subject: [PATCH 09/11] fix some errors --- lib/canned.js | 7 ++++--- lib/proxyResponse.js | 20 ++++++++++---------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/canned.js b/lib/canned.js index d6c19c7..5476224 100644 --- a/lib/canned.js +++ b/lib/canned.js @@ -4,6 +4,7 @@ var url = require('url') var fs = require('fs') var util = require('util') var Response = require('./response') +var ProxyResponse = require('./proxyResponse') var querystring = require('querystring') var cannedUtils = require('./utils') var lookup = require('./lookup') @@ -279,7 +280,7 @@ Canned.prototype.respondWithAny = function (httpObj, files, cb) { } Canned.prototype.responder = function(req, res) { - var responseHandler + var responseHandler, proxyErrorHandler var httpObj = {} var that = this var parsedurl = url.parse(req.url) @@ -315,7 +316,7 @@ Canned.prototype.responder = function(req, res) { paths.splice(0,1); // The first path is the default proxyErrorHandler = function (err) { that._log(' proxy gave error ' + err.code + '\n'); - resp = new Response(getContentType('html'), '', 404, httpObj.res, that.response_opts) + var resp = new Response(getContentType('html'), '', 404, httpObj.res, that.response_opts) resp.send(); } responseHandler = function (err, resp) { @@ -326,7 +327,7 @@ Canned.prototype.responder = function(req, res) { httpObj.fname = '_' + httpObj.dname; return that.findResponse(httpObj, responseHandler); } else if (that.proxy){ - that._log(' proxying request to ' + proxyUrl + '\n'); + that._log(' proxying request to ' + that.proxy + '\n'); resp = new ProxyResponse(that.proxy, httpObj.req, httpObj.res, proxyErrorHandler) }else { that._log(' not found\n'); diff --git a/lib/proxyResponse.js b/lib/proxyResponse.js index 955aabc..3843e71 100644 --- a/lib/proxyResponse.js +++ b/lib/proxyResponse.js @@ -3,6 +3,16 @@ var fs = require('fs') var request = require('request'); var url = require('url'); +function buildProxyUrl(originalUrl, proxy) { + var parsedurl = url.parse(originalUrl), proxyUrl; + proxyUrl = proxy + + (parsedurl.path || '') + + (parsedurl.query || '')+ + (parsedurl.hash || ''); + return proxyUrl; + +} + function ProxyResponse(proxy, req, res, errorHandler) { var that = this; that.proxyUrl = buildProxyUrl(req.url, proxy); @@ -21,14 +31,4 @@ ProxyResponse.prototype.send = function () { .pipe(that.res); } -function buildProxyUrl(originalUrl, proxy) { - var parsedurl = url.parse(originalUrl), proxyUrl; - proxyUrl = proxy + - (parsedurl.path || '') + - (parsedurl.query || '')+ - (parsedurl.hash || ''); - return proxyUrl; - -} - module.exports = ProxyResponse From 58e2074f7064fda0b21cc1d0ff22ed664ee25909 Mon Sep 17 00:00:00 2001 From: Jiby Jose Date: Sat, 5 Dec 2015 13:10:16 +0100 Subject: [PATCH 10/11] add documentation for proxy --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 2b029a0..f64ea10 100644 --- a/README.md +++ b/README.md @@ -203,6 +203,23 @@ similar. Wildcards can be specified on the command line via canned --wildcard iamawildcard +Proxy unknown requests +---------------------- + +You can configure canned to forward requests that dont have a response defined to a proxy domain, this is helpful when you want to mock some api, but forward the not defined ones to the actual API server. + +Proxy can be specified on the command line via + +canned --proxy http://api.domain.com + +Proxy can also be configured when using canned programatically by passing `proxy` as an option, for example +``` +canned(apiDir, { + proxy: 'http://api.domain.com', + ... +}); +``` + How about some docs inside for the responses? --------------------------------------------- From 24ff7dffe87411d6a3e5a9f703a6f4cf4e24217e Mon Sep 17 00:00:00 2001 From: Jiby Jose Date: Sat, 5 Dec 2015 13:11:31 +0100 Subject: [PATCH 11/11] update readme formatting --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f64ea10..22ff941 100644 --- a/README.md +++ b/README.md @@ -200,8 +200,9 @@ directories, so that if for given a request like: you don't have a file in `./canned/api/users/1/profile/index.get.json` then it would look for a file in `./canned/api/users/any/index.get.json` or similar. Wildcards can be specified on the command line via - - canned --wildcard iamawildcard +``` +canned --wildcard iamawildcard +``` Proxy unknown requests ---------------------- @@ -209,8 +210,9 @@ Proxy unknown requests You can configure canned to forward requests that dont have a response defined to a proxy domain, this is helpful when you want to mock some api, but forward the not defined ones to the actual API server. Proxy can be specified on the command line via - +``` canned --proxy http://api.domain.com +``` Proxy can also be configured when using canned programatically by passing `proxy` as an option, for example ```