Skip to content

Commit 7f06edc

Browse files
committed
Add upstream route filtering method.
1 parent 2bff68d commit 7f06edc

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

src/platformsh.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,44 @@ class Config {
218218
throw new Error(`No primary route found. This isn't supposed to happen.`);
219219
}
220220

221+
/**
222+
* Returns just those routes that point to a valid upstream.
223+
*
224+
* This method is similar to routes(), but filters out redirect routes that are rarely
225+
* useful for app configuration. If desired it can also filter to just those routes
226+
* whose upstream is a given application name. To retrieve routes that point to the
227+
* current application where the code is being run, use:
228+
*
229+
* routes = config.getUpstreamRoutes(config.applicationName);
230+
*
231+
* @param {string|null} appName
232+
* The name of the upstream app on which to filter, if any.
233+
* @return {object}
234+
* An object map of route definitions.
235+
*/
236+
getUpstreamRoutes(appName = null) {
237+
// Because routes is an object/dictionary, we can't just filter it directly.
238+
// Verbose way it is.
239+
240+
const routes = this.routes();
241+
const filter = route => {
242+
return route.type === 'upstream'
243+
// On Dedicated, the upstream name sometimes is `app:http` instead of just `app`.
244+
// If no name is specified then don't bother checking.
245+
&& (!appName || appName === route.upstream.split(':')[0]);
246+
};
247+
248+
let ret = {};
249+
250+
Object.keys(routes).forEach(function(key) {
251+
if (filter(routes[key])) {
252+
ret[key] = routes[key];
253+
}
254+
});
255+
256+
return ret;
257+
}
258+
221259
/**
222260
* Returns a single route definition.
223261
*

test/tests.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,40 @@ describe("Config tests", () => {
172172
let route = c.getPrimaryRoute();
173173

174174
assert.equal(route['original_url'], 'https://www.{default}/');
175+
assert.equal(route['primary'], true);
176+
});
177+
178+
it('returns all upstream routes', () => {
179+
let c = new psh.Config(mockEnvironmentRuntime);
180+
181+
let routes = c.getUpstreamRoutes();
182+
183+
assert.equal(3, Object.keys(routes).length);
184+
assert.equal(routes['https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/']['original_url'], 'https://www.{default}/');
185+
});
186+
187+
it('returns all upstream routes for a specific app', () => {
188+
let c = new psh.Config(mockEnvironmentRuntime);
189+
190+
let routes = c.getUpstreamRoutes('app');
191+
192+
assert.equal(2, Object.keys(routes).length);
193+
assert.equal(routes['https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/']['original_url'], 'https://www.{default}/');
194+
});
195+
196+
it('returns all upstream routes for a specific app on dedicated', () => {
197+
let env = mockEnvironmentRuntime;
198+
// Simulate a Dedicated-style upstream name.
199+
let routeData = loadJsonFile('PLATFORM_ROUTES');
200+
routeData['https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/']['upstream'] = 'app:http';
201+
env['PLATFORM_ROUTES'] = encode(routeData);
202+
203+
let c = new psh.Config(env);
204+
205+
let routes = c.getUpstreamRoutes('app');
206+
207+
assert.equal(2, Object.keys(routes).length);
208+
assert.equal(routes['https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/']['original_url'], 'https://www.{default}/');
175209
});
176210

177211
it('gets a route by id', () => {

0 commit comments

Comments
 (0)