Skip to content

Commit

Permalink
Merge pull request #38 from vishalvijay/fix_firebase_hosting
Browse files Browse the repository at this point in the history
Add firebase hosting header
  • Loading branch information
pbojinov authored Oct 29, 2018
2 parents 6a48072 + 5bc2a18 commit ebe9280
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ function getClientIp(req) {
return req.headers['x-client-ip'];
}

// Firebase hosting header (When forwared to cloud function)
if (is.ip(req.headers['fastly-client-ip'])) {
return req.headers['fastly-client-ip'];
}

// Load-balancers (AWS ELB) or proxies.
const xForwardedFor = getClientIpFromXForwardedFor(req.headers['x-forwarded-for']);
if (is.ip(xForwardedFor)) {
Expand Down
27 changes: 27 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,33 @@ test('x-client-ip', (t) => {
});
});

test('fastly-client-ip', (t) => {
t.plan(1);
const options = {
url: '',
headers: {
'fastly-client-ip': '59.195.114.48',
},
};
// create new server for each test so we can easily close it after the test is done
// prevents tests from hanging and competing against closing a global server
const server = new ServerFactory();
// node listens on a random port when using 0
// http://stackoverflow.com/questions/9901043/how-does-node-js-choose-random-ports
server.listen(0, serverInfo.host);
server.on('listening', () => {
// we can't make the request URL until we get the port number from the new server
options.url = `http://${serverInfo.host}:${server.address().port}`;
request(options, (error, response, found) => {
if (!error && response.statusCode === 200) {
// make sure response ip is the same as the one we passed in
t.equal(options.headers['fastly-client-ip'], found);
server.close();
}
});
});
});

test('x-forwarded-for', (t) => {
t.plan(1);
const options = {
Expand Down

0 comments on commit ebe9280

Please sign in to comment.