From 5c4d261f74479d91ed4076f540319f2a2f5fcabe Mon Sep 17 00:00:00 2001 From: Alex Anderson <191496+alxndrsn@users.noreply.github.com> Date: Mon, 7 Oct 2024 09:13:40 +0300 Subject: [PATCH] test-nginx: assert x-forwarded-proto header is always https (#742) --- test/mock-http-server/index.js | 2 ++ test/test-nginx.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/test/mock-http-server/index.js b/test/mock-http-server/index.js index e04e9f42..c39b716e 100644 --- a/test/mock-http-server/index.js +++ b/test/mock-http-server/index.js @@ -14,6 +14,8 @@ app.get('/reset', withStdLogging((req, res) => { res.json('OK'); })); +app.get('/v1/reflect-headers', withStdLogging((req, res) => res.json(req.headers))); + app.get('/*', ok('GET')); app.post('/*', ok('POST')); // TODO add more methods as required diff --git a/test/test-nginx.js b/test/test-nginx.js index f317d14e..3731e027 100644 --- a/test/test-nginx.js +++ b/test/test-nginx.js @@ -96,6 +96,34 @@ describe('nginx config', () => { { method:'GET', path:'/v1/some/central-backend/path' }, ); }); + + it('should set x-forwarded-proto header to "https"', async () => { + // when + const res = await fetch(`https://localhost:9001/v1/reflect-headers`); + // then + assert.equal(res.status, 200); + + // when + const body = await res.json(); + // then + assert.equal(body['x-forwarded-proto'], 'https'); + }); + + it('should override supplied x-forwarded-proto header', async () => { + // when + const res = await fetch(`https://localhost:9001/v1/reflect-headers`, { + headers: { + 'x-forwarded-proto': 'http', + }, + }); + // then + assert.equal(res.status, 200); + + // when + const body = await res.json(); + // then + assert.equal(body['x-forwarded-proto'], 'https'); + }); }); function fetchHttp(path, options) {