-
Notifications
You must be signed in to change notification settings - Fork 46
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
Proxy Requests for Unknown Paths #56 #84
base: master
Are you sure you want to change the base?
Changes from all commits
1ef34f2
e0e3886
94c958c
0b6d68c
dfa1600
933f996
3899fd4
86ae63e
8e816ac
58e2074
24ff7df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"use strict"; | ||
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); | ||
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); | ||
} | ||
|
||
module.exports = ProxyResponse |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really like the test making use of an actual server makes this some much more useful! 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sideshowcoder glad you like it, it was almost impossible to test it otherwise |
||
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) | ||
}) | ||
}) | ||
|
||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
after the https://github.com/sideshowcoder/canned/blob/feature-56_proxy_request/README.md#variable-responses section you could add a new section to the README detailing how the proxy stuff works, this would need to contain some examples on how to use it with the commands to enter and the flow of a request through canned with a proxy enabled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sideshowcoder yep i will do that, but give me till this weekend, because i am travelling till wednesday