From adcf0b944d659548ba6e5ae2926bdb188a59e1ef Mon Sep 17 00:00:00 2001 From: Adam Bukowski Date: Fri, 12 Jan 2024 13:10:16 +0100 Subject: [PATCH] feat: add abitlity to pass the user-agent in config object --- lib/data-provider.js | 8 +- test/data-provider-test.js | 40 +++++++- test/e2e-test.js | 194 +++++++++++++------------------------ 3 files changed, 109 insertions(+), 133 deletions(-) diff --git a/lib/data-provider.js b/lib/data-provider.js index f24bbe3..fa66ff5 100644 --- a/lib/data-provider.js +++ b/lib/data-provider.js @@ -1,9 +1,6 @@ 'use strict'; const url = require('url'); -const defaultHeaders = { - Accept: 'text/html, application/xhtml+xml, application/xml', -}; module.exports = function DataProvider(config) { const pendingReqests = new Map(); @@ -12,6 +9,11 @@ module.exports = function DataProvider(config) { const baseUrl = config.baseUrl || ''; const httpClient = config.httpClient || globalThis.fetch; + const userAgent = config.userAgent || 'node-esi'; + const defaultHeaders = { + Accept: 'text/html, application/xhtml+xml, application/xml', + 'user-agent': userAgent, + }; function extendRequestOptions(src, baseOptions) { return [ diff --git a/test/data-provider-test.js b/test/data-provider-test.js index 0ecb179..b99dab5 100644 --- a/test/data-provider-test.js +++ b/test/data-provider-test.js @@ -92,10 +92,39 @@ describe('Data Provider', () => { .catch(done); }); + it('allows to configure custom user agent', (done) => { + // given + let calledUserAgent; + const userAgent = 'my-custom-agent'; + const server = http.createServer((req, res) => { + calledUserAgent = req.headers['user-agent']; + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end('stuff'); + }); + server.listen(); + + const port = server.address().port; + const dataProvider = new DataProvider({ + baseUrl: `http://localhost:${port}`, + userAgent, + }); + + // when + dataProvider + .get('/') + + // then + .then((result) => { + assert.equal(result, 'stuff'); + assert.equal(calledUserAgent, userAgent); + done(); + }) + .catch(done); + }); + it('should work with a custom http client that is fetch api compatible', (done) => { // given const baseUrl = 'http://example.com'; - const userAgent = 'my-custom-agent'; let calledUrl; let calledOptions; @@ -113,7 +142,10 @@ describe('Data Provider', () => { // when dataProvider - .get('/path', { headers: { 'user-agent': userAgent } }) + .get('/path', { + headers: { custom: 'custom-header-value' }, + optionX: 'optionX', + }) // then .then((result) => { @@ -122,8 +154,10 @@ describe('Data Provider', () => { assert.deepEqual(calledOptions, { headers: { Accept: 'text/html, application/xhtml+xml, application/xml', - 'user-agent': userAgent, + 'user-agent': 'node-esi', + custom: 'custom-header-value', }, + optionX: 'optionX', }); done(); }) diff --git a/test/e2e-test.js b/test/e2e-test.js index 6867c1e..20ebe01 100644 --- a/test/e2e-test.js +++ b/test/e2e-test.js @@ -29,10 +29,7 @@ describe('ESI processor', () => { res.end('
test
'); }); - const html = - '
'; + const html = `
`; // when const processed = ESI().process(html); @@ -58,12 +55,7 @@ describe('ESI processor', () => { } }); - const html = - '
'; + const html = `
`; // when const processed = ESI().process(html); @@ -92,12 +84,7 @@ describe('ESI processor', () => { } }); - const html = - '
'; + const html = `
`; // when const processed = ESI().process(html); @@ -124,10 +111,7 @@ describe('ESI processor', () => { res.end('
test
'); }); - const html = - "
"; + const html = `
`; // when const processed = ESI().process(html); @@ -153,12 +137,7 @@ describe('ESI processor', () => { } }); - const html = - '
"; + const html = `
`; // when const processed = ESI().process(html); @@ -176,13 +155,10 @@ describe('ESI processor', () => { // given server.addListener('request', (req, res) => { res.writeHead(200, { 'Content-Type': 'text/html' }); - res.end('
' + req.url + '
'); + res.end(`
${req.url}
`); }); - const html = - "
"; + const html = `
`; // when const processed = ESI().process(html); @@ -206,10 +182,7 @@ describe('ESI processor', () => { res.end('
test
'); }); - const html = - '
'; + const html = `
`; // when const processed = ESI().process(html); @@ -235,12 +208,7 @@ describe('ESI processor', () => { } }); - const html = - '
'; + const html = `
`; // when const processed = ESI().process(html); @@ -261,10 +229,7 @@ describe('ESI processor', () => { res.end('
test
'); }); - const html = - '
'; + const html = `
`; // when const processed = ESI().process(html); @@ -279,10 +244,7 @@ describe('ESI processor', () => { }); it('should fetch nothing on typo', (done) => { - const html = - '
'; + const html = `
`; // when const processed = ESI().process(html); @@ -297,10 +259,7 @@ describe('ESI processor', () => { }); it('should not process not properly closed tags', (done) => { - const html = - '
'; + const html = `
`; // when const processed = ESI().process(html); @@ -321,10 +280,7 @@ describe('ESI processor', () => { res.end('
test
'); }); - const html = - '
'; + const html = `
`; // when const processed = ESI().process(html); @@ -536,7 +492,7 @@ describe('ESI processor', () => { error.message, 'HTTP error 500: Internal Server Error' ); - assert.equal(src, 'http://localhost:' + port + '/error'); + assert.equal(src, `http://localhost:${port}/error`); return '
something went wrong
'; }, }).process(html); @@ -586,66 +542,24 @@ describe('ESI processor', () => { .catch(done); }); - it('should allow to disable cache', (done) => { - // given - let connectionCount = 0; - server.addListener('request', (req, res) => { - res.writeHead(200, { 'Content-Type': 'text/html' }); - if (connectionCount === 0) { - res.end('hello'); - } else { - res.end('world'); - } - connectionCount++; - }); - - const html = ''; - - // when - const esi = ESI({ - baseUrl: 'http://localhost:' + port, - cache: false, - }); - - const processed = esi.process(html); - - // then - processed - .then((response) => { - return esi.process(html); - }) - .then((response) => { - assert.equal(response, 'world'); - done(); - }) - .catch(done); - }); - it('should fetch components recursively', (done) => { // given server.addListener('request', (req, res) => { res.writeHead(200, { 'Content-Type': 'text/html' }); if (req.url === '/first') { res.end( - '' + `` ); } else if (req.url === '/second') { res.end( - '' + `` ); } else { res.end('
test
'); } }); - const html = - '
'; + const html = `
`; // when const processed = ESI().process(html); @@ -664,16 +578,11 @@ describe('ESI processor', () => { server.addListener('request', (req, res) => { res.writeHead(200, { 'Content-Type': 'text/html' }); res.end( - '' + `` ); }); - const html = - '
'; + const html = `
`; // when const processed = ESI({ @@ -701,10 +610,7 @@ describe('ESI processor', () => { } }); - const html = - '
'; + const html = `
`; // when const processed = ESI().process(html, { @@ -722,6 +628,49 @@ describe('ESI processor', () => { .catch(done); }); + it('should pass a default user-agent header to the server', (done) => { + // given + server.addListener('request', (req, res) => { + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(req.headers['user-agent']); + }); + + const html = `
`; + + // when + const processed = ESI().process(html); + + // then + processed + .then((response) => { + assert.equal(response, '
node-esi
'); + done(); + }) + .catch(done); + }); + + it('should pass a custom user-agent header to the server', (done) => { + // given + const userAgent = 'my-custom-agent'; + server.addListener('request', (req, res) => { + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(req.headers['user-agent']); + }); + + const html = `
`; + + // when + const processed = ESI({ userAgent }).process(html); + + // then + processed + .then((response) => { + assert.equal(response, `
${userAgent}
`); + done(); + }) + .catch(done); + }); + it('should throw appropriate error if the host was blocked', (done) => { // given server.addListener('request', (req, res) => { @@ -729,10 +678,7 @@ describe('ESI processor', () => { res.end('
test
'); }); - const html = - '
'; + const html = `
`; const esi = ESI({ allowedHosts: ['http://not-localhost'], @@ -758,10 +704,7 @@ describe('ESI processor', () => { res.end('
test
'); }); - const html = - '
'; + const html = `
`; const errors = []; const esi = ESI({ @@ -789,10 +732,7 @@ describe('ESI processor', () => { res.end('
test
'); }); - const html = - '
'; + const html = `
`; const errors = []; const esi = ESI({ @@ -990,7 +930,7 @@ describe('ESI processor', () => { // when const processed = ESI({ - baseUrl: 'http://localhost:' + port, + baseUrl: `http://localhost:${port}`, }).process(html); // then