mirror of
https://github.com/feathersjs/feathers.git
synced 2026-01-25 15:23:13 +00:00
Bring back makeUrl (#62)
This commit is contained in:
parent
af8afb8b28
commit
1474927042
@ -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)}`;
|
||||
};
|
||||
|
||||
@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user