diff --git a/.eslintrc b/.eslintrc index 96db0ca..dd919cd 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,4 +1,7 @@ { + "parserOptions": { + "ecmaVersion": 2018 + }, "env": { "browser": false, "es6": true, @@ -19,12 +22,21 @@ "no-eval": 2, "no-unused-vars": 2, "no-lonely-if": 2, - "quotes": [2, "single"], - "strict": [2, "global"], - "prettier/prettier": [2, { - "singleQuote": true, - "tabWidth": 4 - }] + "quotes": [ + 2, + "single" + ], + "strict": [ + 2, + "global" + ], + "prettier/prettier": [ + 2, + { + "singleQuote": true, + "tabWidth": 4 + } + ] }, "globals": { "require": false, diff --git a/lib/parse-environment.js b/lib/parse-environment.js new file mode 100644 index 0000000..1ef5e5d --- /dev/null +++ b/lib/parse-environment.js @@ -0,0 +1,9 @@ +'use strict'; + +module.exports = url => { + const percentRegEx = /\{([^}]+)\}/; + const matches = percentRegEx.exec(url); + return matches != null && Object.keys(process.env).includes(matches[1]) + ? url.replace(matches[0], process.env[matches[1]]) + : url; +}; diff --git a/lib/request-fragment.js b/lib/request-fragment.js index 63909fa..3aa0566 100644 --- a/lib/request-fragment.js +++ b/lib/request-fragment.js @@ -5,6 +5,7 @@ const https = require('https'); const url = require('url'); const { globalTracer, FORMAT_HTTP_HEADERS } = require('opentracing'); const tracer = globalTracer(); +const parseEnvironmentVariables = require('./parse-environment'); // By default tailor supports gzipped response from fragments const requiredHeaders = { @@ -30,7 +31,7 @@ module.exports = filterHeaders => ( span = null ) => new Promise((resolve, reject) => { - const parsedUrl = url.parse(fragmentUrl); + const parsedUrl = url.parse(parseEnvironmentVariables(fragmentUrl)); const options = Object.assign( { headers: Object.assign( diff --git a/tests/parse-environment.js b/tests/parse-environment.js new file mode 100644 index 0000000..4691be7 --- /dev/null +++ b/tests/parse-environment.js @@ -0,0 +1,54 @@ +'use strict'; +const assert = require('assert'); +const parseEnvironmentVariables = require('../lib/parse-environment'); + +describe('Parse Environment', () => { + it('should return the same url when no brackets found', () => { + const fragmentUrl = 'https://test.zalando.de/#/blub/12'; + assert.strictEqual( + parseEnvironmentVariables(fragmentUrl), + fragmentUrl, + 'The url is not the same anymore' + ); + }); + + it('should return the same url when only one bracket is found', () => { + let fragmentUrl = 'https://{test.zalando.de/#/blub/12'; + assert.strictEqual( + parseEnvironmentVariables(fragmentUrl), + fragmentUrl, + 'The url is not the same anymore' + ); + + fragmentUrl = 'https://test.zalando.de}/#/blub/12'; + assert.strictEqual( + parseEnvironmentVariables(fragmentUrl), + fragmentUrl, + 'The url is not the same anymore' + ); + }); + + it('should return the same url when no environment variable is found', () => { + const envVarName = `TEST__ROOT_HOST_${+new Date()}`; + const fragmentUrl = `https://{${envVarName}}/#/blub/12`; + delete process.env[envVarName]; + assert.strictEqual( + parseEnvironmentVariables(fragmentUrl), + fragmentUrl, + 'The url is not the same anymore' + ); + }); + + it('should return the modified url when everything is given', () => { + const envVarName = `TEST__ROOT_HOST_${+new Date()}`; + const envVarValue = 'test.zalando.de'; + const fragmentUrl = `https://{${envVarName}}/#/blub/12`; + process.env[envVarName] = envVarValue; + assert.strictEqual( + parseEnvironmentVariables(fragmentUrl), + fragmentUrl.replace(`{${envVarName}}`, envVarValue), + 'The url is the same' + ); + delete process.env[envVarName]; + }); +}); diff --git a/tests/tailor.js b/tests/tailor.js index ecde416..a560455 100644 --- a/tests/tailor.js +++ b/tests/tailor.js @@ -124,8 +124,8 @@ describe('Tailor', () => { mockTemplate.returns(false); getResponse('http://localhost:8080/missing-template') .then(response => { - assert.equal(response.statusCode, 500); - assert.equal(response.body, 'error template'); + assert.strictEqual(response.statusCode, 500); + assert.strictEqual(response.body, 'error template'); }) .then(done, done); }); @@ -134,8 +134,8 @@ describe('Tailor', () => { mockTemplate.returns('404'); getResponse('http://localhost:8080/404-template') .then(response => { - assert.equal(response.statusCode, 404); - assert.equal(response.body, 'template not found'); + assert.strictEqual(response.statusCode, 404); + assert.strictEqual(response.body, 'template not found'); }) .then(done, done); }); @@ -156,8 +156,8 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal(response.statusCode, 200); - assert.equal( + assert.strictEqual(response.statusCode, 200); + assert.strictEqual( response.body, '' + '' + @@ -182,7 +182,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -209,7 +209,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -243,11 +243,11 @@ describe('Tailor', () => { it('should stream pipe definition with loader in the head', done => { getResponse('http://localhost:8083/test') .then(response => { - assert.equal( + assert.strictEqual( response.headers.link, '; rel="preload"; as="script"; nopush; crossorigin' ); - assert.equal( + assert.strictEqual( response.body, '' + '\n' + @@ -277,8 +277,11 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal(response.statusCode, 300); - assert.equal(response.headers.location, 'https://redirect'); + assert.strictEqual(response.statusCode, 300); + assert.strictEqual( + response.headers.location, + 'https://redirect' + ); }) .then(done, done); }); @@ -304,7 +307,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal(response.statusCode, 200); + assert.strictEqual(response.statusCode, 200); assert.deepEqual(response.headers['set-cookie'], [cookie]); }) .then(done, done); @@ -364,11 +367,11 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { const headers = response.headers; - assert.equal( + assert.strictEqual( 'no-cache, no-store, must-revalidate', headers['cache-control'] ); - assert.equal('no-cache', headers['pragma']); + assert.strictEqual('no-cache', headers['pragma']); }) .then(done, done); }); @@ -402,7 +405,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.headers.link, '; rel="preload"; as="script"; nopush; crossorigin' ); @@ -424,7 +427,7 @@ describe('Tailor', () => { getResponse('http://localhost:8082/test') .then(response => { - assert.equal(response.headers.link, undefined); + assert.strictEqual(response.headers.link, undefined); }) .then(done, done); }); @@ -450,7 +453,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.headers.link, '; rel="preload"; as="script"; nopush; crossorigin,; rel="preload"; as="style"; nopush;,; rel="preload"; as="script"; nopush; crossorigin' ); @@ -473,7 +476,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.headers.link, '; rel="preload"; as="script"; nopush; crossorigin,; rel="preload"; as="style"; nopush;,; rel="preload"; as="script"; nopush;' ); @@ -500,7 +503,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' ); @@ -520,7 +523,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal(response.statusCode, 500); + assert.strictEqual(response.statusCode, 500); }) .then(done, done); }); @@ -538,7 +541,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal(response.statusCode, 500); + assert.strictEqual(response.statusCode, 500); }) .then(done, done); }); @@ -558,7 +561,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal(response.statusCode, 200); + assert.strictEqual(response.statusCode, 200); }) .then(done, done); }); @@ -578,7 +581,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal(response.statusCode, 500); + assert.strictEqual(response.statusCode, 500); }) .then(done, done); }); @@ -599,7 +602,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -629,7 +632,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -657,7 +660,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -688,7 +691,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -721,8 +724,8 @@ describe('Tailor', () => { mockContext.returns(Promise.resolve(contextObj)); getResponse('http://localhost:8080/test').then(response => { - assert.equal(response.statusCode, 200); - assert.equal( + assert.strictEqual(response.statusCode, 200); + assert.strictEqual( response.body, '' + '' + @@ -741,8 +744,8 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal(response.statusCode, 200); - assert.equal( + assert.strictEqual(response.statusCode, 200); + assert.strictEqual( response.body, '' + '' + @@ -771,7 +774,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, 'hello' ); @@ -792,7 +795,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -823,7 +826,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -853,7 +856,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -881,7 +884,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -900,7 +903,7 @@ describe('Tailor', () => { mockTemplate.returns(''); http.get('http://localhost:8080/test', () => { - assert.equal(console.warn.callCount, 1); + assert.strictEqual(console.warn.callCount, 1); console.warn.restore(); done(); }); @@ -915,7 +918,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -938,7 +941,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -961,7 +964,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -992,7 +995,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -1026,7 +1029,7 @@ describe('Tailor', () => { ); http.get('http://localhost:8080/test', response => { - assert.equal(response.statusCode, 500); + assert.strictEqual(response.statusCode, 500); done(); }); }); @@ -1052,7 +1055,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -1086,7 +1089,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -1116,7 +1119,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -1141,7 +1144,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -1189,7 +1192,7 @@ describe('Tailor', () => { getResponse('http://localhost:8081/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -1237,7 +1240,7 @@ describe('Tailor', () => { getResponse('http://localhost:8081/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -1285,7 +1288,7 @@ describe('Tailor', () => { getResponse('http://localhost:8081/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -1317,7 +1320,7 @@ describe('Tailor', () => { getResponse('http://localhost:8081/test') .then(response => { - assert.equal( + assert.strictEqual( response.body, '' + '' + @@ -1351,8 +1354,8 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(() => { const { tags } = traceResults(); - assert.equal(tags.length, 1); - assert.deepEqual(tags[0], { + assert.strictEqual(tags.length, 1); + assert.deepStrictEqual(tags[0], { 'http.url': '/test', 'span.kind': 'server' }); @@ -1365,13 +1368,13 @@ describe('Tailor', () => { getResponse('http://localhost:8080/error') .then(() => { const { tags, logs } = traceResults(); - assert.deepEqual(tags[0], { + assert.deepStrictEqual(tags[0], { 'http.url': '/error', 'span.kind': 'server', error: true, 'http.status_code': 500 }); - assert.equal(logs.length, 1); + assert.strictEqual(logs.length, 1); }) .then(done, done); }); @@ -1389,12 +1392,11 @@ describe('Tailor', () => { .then(() => { const { tags } = traceResults(); // Tailor should return error - assert.equal(tags[0].error, true); + assert.strictEqual(tags[0].error, true); // Primary fragment error - assert.deepEqual(tags[1], { + assert.deepStrictEqual(tags[1], { error: true, primary: true, - foo: 'bar', 'span.kind': 'client', 'http.url': 'https://fragment/1', fallback: false, @@ -1423,7 +1425,7 @@ describe('Tailor', () => { getResponse('http://localhost:8080/test') .then(() => { const { tags } = traceResults(); - assert.deepEqual(tags[1], { + assert.deepStrictEqual(tags[1], { 'span.kind': 'client', [Tags.HTTP_URL]: 'https://fragment/1', id: 'test',