Permalink
Browse files

Renamed mock references to tamper as part of project rename.

  • Loading branch information...
1 parent 6648d28 commit 7f05e02d860e2ef4f8192bf5b49ea82a3d56b1a3 @tsyd committed Feb 6, 2011
Showing with 30 additions and 30 deletions.
  1. +8 −8 README.md
  2. +6 −6 example.js
  3. +16 −16 lib/{proxy-mock.js → proxy-tamper.js}
View
@@ -1,31 +1,31 @@
-# proxy-mock
+# proxy-tamper
-A HTTP proxy library for node.js that allows for selective requests to be mocked.
+A HTTP proxy library for node.js that allows for selective requests to be tampered.
## Examples
To mock HTTP requests with a string or result of a function call, specify a regular expression for the URL and a string or function:
- var proxy = require('./lib/proxy-mock').start({ port: 8080 });
+ var proxy = require('./lib/proxy-tamper').start({ port: 8080 });
- proxy.mock(/test/, 'mocked');
+ proxy.tamper(/test/, 'tampered');
It is possible to manipulate the original request before it's executed over the proxy. The request object has access to `request.url`, `request.headers`, and `request.method`:
- proxy.mock(/translate\.google\..*?\/translate_a\/t/, function (request) {
+ proxy.tamper(/translate\.google\..*?\/translate_a\/t/, function (request) {
// disallow translations
request.url = request.url.replace(/hl=../, 'hl=en').replace(/tl=../, 'tl=en')
.replace(/sl=../, 'sl=en').replace(/text=.*/, 'text=No+translation+for+you!');
});
It is also possible to modify the response before proxying it back to the original request by specifying an `onResponse` handler:
- proxy.mock(/tsyd\.net\/$/, function (request) {
+ proxy.tamper(/tsyd\.net\/$/, function (request) {
request.onResponse(function (response) {
- // called when we have the response from the mocked url
+ // called when we have the response from the tampered url
response.body = reverseHeadings(response.body); // reverseHeadings defined elsewhere
- response.headers['server'] = 'proxy-mock 1337';
+ response.headers['server'] = 'proxy-tamper 1337';
// the onResponse handler must complete the response
response.complete();
View
@@ -1,20 +1,20 @@
-var proxy = require('./lib/proxy-mock');
+var proxy = require('./lib/proxy-tamper');
proxy.start({ port: 8080 }, function (p) {
- p.mock(/\/test/, 'mocked');
+ p.tamper(/\/test/, 'tampered');
- p.mock(/translate\.google\..*?\/translate_a\/t/, function (request) {
+ p.tamper(/translate\.google\..*?\/translate_a\/t/, function (request) {
// disallow translations
request.url = request.url.replace(/hl=../, 'hl=en').replace(/tl=../, 'tl=en')
.replace(/sl=../, 'sl=en').replace(/text=.*/, 'text=No+translation+for+you!');
});
- p.mock(/tsyd\.net\/$/, function (request) {
+ p.tamper(/tsyd\.net\/$/, function (request) {
request.onResponse(function (response) {
- // called when we have the response from the mocked url
+ // called when we have the response from the tampered url
response.body = reverseHeadings(response.body);
- response.headers['server'] = 'proxy-mock 1337';
+ response.headers['server'] = 'proxy-tamper 1337';
// the onResponse handler must complete the response
response.complete();
@@ -1,19 +1,19 @@
var http = require('http');
-var ProxyMock = function (options) {
+var ProxyTamper = function (options) {
var _patterns = [];
var _server = http.createServer(function (req, resp) {
var proxy = null;
var proxyReq = null;
- var mockBody = null;
+ var tamperBody = null;
var buffers = [];
_patterns.forEach(function (p) {
if (req.url.search(p.pattern) != -1) {
- switch (p.mock.constructor.name) {
+ switch (p.tamper.constructor.name) {
case 'Function':
- switch (parseInt(p.mock.length)) {
+ switch (parseInt(p.tamper.length)) {
case 1:
var reqProxyObj = {
method: req.method, url: req.url, headers: req.headers,
@@ -22,29 +22,29 @@ var ProxyMock = function (options) {
}
};
- p.mock.call(null, reqProxyObj);
+ p.tamper.call(null, reqProxyObj);
req.url = reqProxyObj.url;
req.headers = reqProxyObj.headers;
req.method = reqProxyObj.method;
break;
case 0:
default:
- mockBody = p.mock.call(null, null);
+ tamperBody = p.tamper.call(null, null);
break;
}
break;
case 'String':
- mockBody = p.mock; break;
+ tamperBody = p.tamper; break;
default:
- throw new Error('Mock must be a function or string but was a '
- + p.mock.constructor.name + '.'); break;
+ throw new Error('Tamper object must be a function or string but was a '
+ + p.tamper.constructor.name + '.'); break;
}
}
});
- if (!resp._onResultHandler && mockBody) {
+ if (!resp._onResultHandler && tamperBody) {
resp.writeHead(200, {});
- resp.write(mockBody, 'utf8');
+ resp.write(tamperBody, 'utf8');
resp.end();
return;
}
@@ -99,14 +99,14 @@ var ProxyMock = function (options) {
}).listen(options.port);
return {
- mock: function (pattern, obj) {
- _patterns.unshift({'pattern': pattern, 'mock': obj});
+ tamper: function (pattern, obj) {
+ _patterns.unshift({ pattern: pattern, tamper: obj });
}
};
}
exports.start = function (options, block) {
- var proxyMock = new ProxyMock(options);
- block.call(proxyMock, proxyMock);
- return proxyMock;
+ var proxyTamper = new ProxyTamper(options);
+ block.call(proxyTamper, proxyTamper);
+ return proxyTamper;
};

0 comments on commit 7f05e02

Please sign in to comment.