Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

Possibility to switch between different environments #340

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"parserOptions": {
"ecmaVersion": 2018
},
"env": {
"browser": false,
"es6": true,
Expand All @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions lib/parse-environment.js
Original file line number Diff line number Diff line change
@@ -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;
};
3 changes: 2 additions & 1 deletion lib/request-fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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(
Expand Down
54 changes: 54 additions & 0 deletions tests/parse-environment.js
Original file line number Diff line number Diff line change
@@ -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];
});
});
Loading