Skip to content

Commit

Permalink
test: add tests for body-based filtering
Browse files Browse the repository at this point in the history
The filters setup for the tests perform all filtering on the client,
as that is our expected use-case.
  • Loading branch information
gjvis committed Sep 21, 2016
1 parent 3479aab commit 16ab1b6
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 8 deletions.
26 changes: 25 additions & 1 deletion test/fixtures/client/filters.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@
"path": "/echo-body",
"method": "POST",
"origin": "http://localhost:${originPort}"
},

{
"path": "/echo-body/filtered",
"method": "POST",
"origin": "http://localhost:${originPort}",
"valid": [
{
"//": "accept requests with 'proxy.*: please' in their body",
"path": "proxy.*",
"value": "please"
}
]
}
],
"public": [
Expand All @@ -21,7 +34,18 @@
{
"path": "/echo-body",
"method": "POST"
}
},

{
"path": "/echo-body/filtered",
"method": "POST",
"valid": [
{
"//": "accept requests with 'proxy.*: please' in their body",
"path": "proxy.*",
"value": "please"
}
]
}
]
}
6 changes: 3 additions & 3 deletions test/fixtures/server/filters.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
},

{
"path": "/echo-body",
"path": "/echo-body/:param?",
"method": "POST",
"origin": "http://localhost:${originPort}"
}
],
"public": [
{
"path": "/echo-param/${param}",
"path": "/*",
"method": "GET"
},

{
"path": "/echo-body",
"path": "/*",
"method": "POST"
}

Expand Down
27 changes: 25 additions & 2 deletions test/functional/client-server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ test('proxy requests originating from behind the broker client', t => {
// wait for the client to successfully connect to the server and identify itself
server.io.once('connection', socket => {
socket.once('identify', () => {
t.plan(4);
t.plan(6);

t.test('successfully broker POST', t => {
const url = `http://localhost:${clientPort}/echo-body`;
Expand All @@ -57,7 +57,8 @@ test('proxy requests originating from behind the broker client', t => {
});
});

t.test('block invalid request', t => {
// the filtering happens in the broker client
t.test('block request for non-whitelisted url', t => {
const url = `http://localhost:${clientPort}/not-allowed`;
request({ url, 'method': 'post', json: true }, (err, res, body) => {
t.equal(res.statusCode, 401, '401 statusCode');
Expand All @@ -66,6 +67,28 @@ test('proxy requests originating from behind the broker client', t => {
});
});

// the filtering happens in the broker client
t.test('allow request for valid url with valid body', t => {
const url = `http://localhost:${clientPort}/echo-body/filtered`;
const body = { proxy: { me: 'please' }};
request({ url, method: 'post', json: true, body }, (err, res) => {
t.equal(res.statusCode, 200, '200 statusCode');
t.same(res.body, body, 'body brokered');
t.end();
});
});

// the filtering happens in the broker client
t.test('block request for valid url with invalid body', t => {
const url = `http://localhost:${clientPort}/echo-body/filtered`;
const body = { proxy: { me: 'now!' }};
request({ url, 'method': 'post', json: true, body }, (err, res, body) => {
t.equal(res.statusCode, 401, '401 statusCode');
t.equal(body, 'blocked', '"blocked" body: ' + body);
t.end();
});
});

t.test('clean up', t => {
client.close();
setTimeout(() => {
Expand Down
27 changes: 25 additions & 2 deletions test/functional/server-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test('proxy requests originating from behind the broker server', t => {
// wait for the client to successfully connect to the server and identify itself
server.io.on('connection', socket => {
socket.on('identify', id => {
t.plan(6);
t.plan(8);

t.test('successfully broker POST', t => {
const url = `http://localhost:${serverPort}/broker/${id}/echo-body`;
Expand Down Expand Up @@ -74,7 +74,8 @@ test('proxy requests originating from behind the broker server', t => {
});
})

t.test('block invalid request', t => {
// the filtering happens in the broker client
t.test('block request for non-whitelisted url', t => {
const url = `http://localhost:${serverPort}/broker/${id}/not-allowed`;
request({ url, 'method': 'post', json: true }, (err, res, body) => {
t.equal(res.statusCode, 401, '401 statusCode');
Expand All @@ -83,6 +84,28 @@ test('proxy requests originating from behind the broker server', t => {
});
});

// the filtering happens in the broker client
t.test('allow request for valid url with valid body', t => {
const url = `http://localhost:${serverPort}/broker/${id}/echo-body/filtered`;
const body = { proxy: { me: 'please' }};
request({ url, method: 'post', json: true, body }, (err, res) => {
t.equal(res.statusCode, 200, '200 statusCode');
t.same(res.body, body, 'body brokered');
t.end();
});
});

// the filtering happens in the broker client
t.test('block request for valid url with invalid body', t => {
const url = `http://localhost:${serverPort}/broker/${id}/echo-body/filtered`;
const body = { proxy: { me: 'now!' }};
request({ url, 'method': 'post', json: true, body }, (err, res, body) => {
t.equal(res.statusCode, 401, '401 statusCode');
t.equal(body, 'blocked', '"blocked" body: ' + body);
t.end();
});
});

t.test('bad broker id', t => {
const url = `http://localhost:${serverPort}/broker/${id}XXX/echo-body`;
request({ url, 'method': 'post', json: true }, (err, res) => {
Expand Down

0 comments on commit 16ab1b6

Please sign in to comment.