From 14749270424129b013d0fda4bfdedeebfbb8aade Mon Sep 17 00:00:00 2001 From: David Luecke Date: Wed, 25 Oct 2017 12:52:05 -0700 Subject: [PATCH] Bring back makeUrl (#62) --- packages/commons/lib/utils.js | 13 ++++++ packages/commons/test/utils.test.js | 63 ++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/packages/commons/lib/utils.js b/packages/commons/lib/utils.js index f44d74d93..29c906e16 100644 --- a/packages/commons/lib/utils.js +++ b/packages/commons/lib/utils.js @@ -134,3 +134,16 @@ exports.isPromise = function isPromise (result) { return _.isObject(result) && typeof result.then === 'function'; }; + +exports.makeUrl = function makeUrl (path, app = {}) { + const get = typeof app.get === 'function' ? app.get.bind(app) : () => {}; + const env = get('env') || process.env.NODE_ENV; + const host = get('host') || process.env.HOST_NAME || 'localhost'; + const protocol = (env === 'development' || env === 'test' || (env === undefined)) ? 'http' : 'https'; + const PORT = get('port') || process.env.PORT || 3030; + const port = (env === 'development' || env === 'test' || (env === undefined)) ? `:${PORT}` : ''; + + path = path || ''; + + return `${protocol}://${host}${port}/${exports.stripSlashes(path)}`; +}; diff --git a/packages/commons/test/utils.test.js b/packages/commons/test/utils.test.js index 1bbbd7a62..45f36aac0 100644 --- a/packages/commons/test/utils.test.js +++ b/packages/commons/test/utils.test.js @@ -7,7 +7,8 @@ const { sorter, stripSlashes, select, - isPromise + isPromise, + makeUrl } = require('../lib/utils'); describe('@feathersjs/commons utils', () => { @@ -269,4 +270,64 @@ describe('@feathersjs/commons utils', () => { ]); }); }); + + describe('makeUrl', function () { + let mockApp; + + beforeEach(() => { + mockApp = { env: 'development' }; + mockApp.get = (value) => { + switch (value) { + case 'port': + return 3030; + case 'host': + return 'feathersjs.com'; + case 'env': + return mockApp.env; + } + }; + }); + + it('when in development mode returns the correct url', () => { + const uri = makeUrl('test', mockApp); + expect(uri).to.equal('http://feathersjs.com:3030/test'); + }); + + it('when in test mode returns the correct url', () => { + mockApp.env = 'test'; + const uri = makeUrl('test', mockApp); + expect(uri).to.equal('http://feathersjs.com:3030/test'); + }); + + it('when in production mode returns the correct url', () => { + mockApp.env = 'production'; + const uri = makeUrl('test', mockApp); + expect(uri).to.equal('https://feathersjs.com/test'); + }); + + it('when path is not provided returns a default url', () => { + const uri = makeUrl(null, mockApp); + expect(uri).to.equal('http://feathersjs.com:3030/'); + }); + + it('when app is not defined returns the correct url', () => { + const uri = makeUrl('test'); + expect(uri).to.equal('http://localhost:3030/test'); + }); + + it('strips leading slashes on path', () => { + const uri = makeUrl('/test'); + expect(uri).to.equal('http://localhost:3030/test'); + }); + + it('strips trailing slashes on path', () => { + const uri = makeUrl('test/'); + expect(uri).to.equal('http://localhost:3030/test'); + }); + + it('works with query strings', () => { + const uri = makeUrl('test?admin=true'); + expect(uri).to.equal('http://localhost:3030/test?admin=true'); + }); + }); });