Skip to content
This repository has been archived by the owner on Feb 19, 2023. It is now read-only.

Commit

Permalink
Fix test/filter.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ithinkihaveacat committed Feb 18, 2013
1 parent 912daf6 commit a590000
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 54 deletions.
18 changes: 10 additions & 8 deletions lib/fishback.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,9 @@ function Proxy(cache, client) {
cache = cache || new CacheLocal();
client = client || new Client();

var reqFilter = this.reqFilter = [];
var resFilter = this.resFilter = [];

require('http').Server.call(this);

this.on('request', function (req1, res1) {
Expand All @@ -380,6 +383,10 @@ function Proxy(cache, client) {
});
}

reqFilter.forEach(function (fn) {
fn(req1);
});

if (req1.method === 'GET') {
req1.on('end', function () {
req1.complete = true;
Expand All @@ -388,6 +395,9 @@ function Proxy(cache, client) {
responseProxy(res2, res1);
} else {
client.find(req1, function (res2) {
resFilter.forEach(function (fn) {
fn(res2);
});
cache.add(res2);
responseProxy(res2, res1);
})
Expand All @@ -407,14 +417,6 @@ function Proxy(cache, client) {

util.inherits(Proxy, require('http').Server);

Proxy.prototype.addResFilter = function (fn) {
this.resFilter.push(fn);
};

Proxy.prototype.addReqFilter = function (fn) {
this.reqFilter.push(fn);
};

function Client(backend_host, backend_port, http) {
this.backend_host = backend_host;
this.backend_port = backend_port;
Expand Down
2 changes: 1 addition & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ test:
node utils.js
node simple.js
node cache.js
#node filter.js
node filter.js
1 change: 0 additions & 1 deletion test/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ var expected_hit = [

function (callback) {
proxy.close();
callback();
}

]);
Expand Down
92 changes: 49 additions & 43 deletions test/filter.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,67 @@
// Tests for the filtering capability

var lib = require("./lib");
var fishback = require("../lib/fishback");
var http = require("http");
var assert = require("assert");

//require("fishback").setVerbose(true);

var response = { headers: { "cache-control": "max-age=60, private" }, body: "Hello, World" };

var expected = response;
expected.headers["foo"] = "bar";
expected.headers["cache-control"] = "max-age=60, public";
var expected = { headers: { "foo": "bar", "cache-control": "max-age=60, public" }, body: "Hello, World" };

function callback(service) {
[lib.getCacheLocal].forEach(function (callback) {

service.proxy.addReqFilter(function (req) {
req.url = "http://localhost:" + service.server_port + req.url;
});
service.proxy.addResFilter(function (res) {
res.headers["foo"] = "bar";
res.headers["cache-control"] = res.headers["cache-control"].replace(/\bprivate\b/, "public");
});
callback(function (cache) {

var proxy = new fishback.Proxy(cache, lib.getMockClient(response));

lib.step([

function (callback) {

var options = {
host: '127.0.0.1',
port: service.proxy_port,
path: '/'
};

http.get(options, function(res) {
var actual = { statusCode: null, headers: { }, body: "" };
actual.statusCode = res.statusCode;
actual.headers = res.headers;
res.on('data', function(chunk) {
actual.body += chunk;
});
res.on('end', function() {
lib.responseEqual(actual, response);
callback();
});
proxy.listen(lib.PROXY_PORT, function () {

proxy.reqFilter.push(function (req) {
req.url = "/404";
});

},

function (callback) {
service.shutdown();
}
proxy.resFilter.push(function (res) {
res.headers["foo"] = "bar";
res.headers["cache-control"] = res.headers["cache-control"].replace(/\bprivate\b/, "public");
});

]);

}
lib.step([

function (callback) {

var options = {
host: '0.0.0.0',
port: lib.PROXY_PORT,
path: '/'
};

http.get(options, function(res) {
var actual = { statusCode: null, headers: { }, body: "" };
actual.statusCode = res.statusCode;
assert.equal(res.statusCode, 404);
actual.headers = res.headers;
res.on('data', function(chunk) {
actual.body += chunk;
});
res.on('end', function() {
lib.responseEqual(actual, expected);
callback();
});
});

},

function (callback) {
proxy.close();
}

]);

});

[lib.getCacheLocal].forEach(function (cache) {
cache(function (c) {
lib.createService(c, response, callback);
});

});
2 changes: 1 addition & 1 deletion test/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,10 @@ function getMockClient(response) {
find: function(req, callback) {
var entry = new (require('events').EventEmitter);
entry.url = req.url;
entry.statusCode = 200;
entry.method = req.method;
entry.headers = response.headers;
entry.headers["x-cache"] = "MISS";
entry.statusCode = req.url == "/404" ? 404 : 200;
callback(entry);
entry.emit('data', response.body);
entry.emit('end');
Expand Down

0 comments on commit a590000

Please sign in to comment.