Permalink
Browse files

fix(configure): fail if specified endpoints are not registered

  • Loading branch information...
doktordirk committed Mar 29, 2016
1 parent e697ad9 commit 4a4442533efad889e361992776948202f741e6c0
@@ -40,7 +40,7 @@ logout(redirectUri)
authenticate(provider, redirect, userData)
signup(displayName, email, password)
getMe([criteria])
updateMe(data[,criteria])
updateMe(data[,criteria])
isAuthenticated()
getTokenPayload()
unlink(provider)
@@ -250,9 +250,9 @@ Via the above mentioned configuration virtually all aspects of the authenticatio
// If using aurelia-api:
// =====================
// This is the endpoint used for any requests made in relation to authentication (login, logout, etc.)
// This is the name of the endpoint used for any requests made in relation to authentication (login, logout, etc.). An empty string selects the default endpoint of aurelia-api.
endpoint: null,
// When authenticated, these endpoints will have the token added to the header of any requests (for authorization)
// When authenticated, these endpoints will have the token added to the header of any requests (for authorization). Accepts an array of endpoint names. An empty string selects the default endpoint of aurelia-api.
configureEndpoints: null,
@@ -321,7 +321,7 @@ platform: 'browser',
// Determines the `window` property name upon which aurelia-authentication data is stored (Default: `window.localStorage`)
storage: 'localStorage',
// Prepended to the `tokenName` when kept in storage (nothing to do with)
tokenPrefix: 'aurelia',
tokenPrefix: 'aurelia',
//OAuth provider specific related configuration
@@ -65,7 +65,11 @@ export class FetchConfig {
}
if (typeof client === 'string') {
client = this.clientConfig.getEndpoint(client).client;
let endpoint = this.clientConfig.getEndpoint(client);
if (!endpoint) {
throw new Error(`There is no '${client || 'default'}' endpoint registered.`);
}
client = endpoint.client;
} else if (client instanceof Rest) {
client = client.client;
} else if (!(client instanceof HttpClient)) {
@@ -36,8 +36,15 @@ function configure(aurelia, config) {
});
}
// Let's see if there's a configured client.
let client = clientConfig.getEndpoint(baseConfig.current.endpoint);
let client;
// Let's see if there's a configured named or default client.
if (baseConfig.current.endpoint !== null) {
client = clientConfig.getEndpoint(baseConfig.current.endpoint);
if (!client) {
throw new Error(`There is no '${baseConfig.current.endpoint || 'default'}' endpoint registered.`);
}
}
// No? Fine. Default to HttpClient. BC all the way.
if (!(client instanceof Rest)) {
@@ -17,9 +17,9 @@ export class BaseConfig {
// If using aurelia-api:
// =====================
// This is the endpoint used for any requests made in relation to authentication (login, logout, etc.)
// This is the name of the endpoint used for any requests made in relation to authentication (login, logout, etc.). An empty string selects the default endpoint of aurelia-api.
endpoint: null,
// When authenticated, these endpoints will have the token added to the header of any requests (for authorization)
// When authenticated, these endpoints will have the token added to the header of any requests (for authorization). Accepts an array of endpoint names. An empty string selects the default endpoint of aurelia-api.
configureEndpoints: null,
@@ -113,6 +113,16 @@ describe('FetchConfig', function() {
expect(client.client.baseUrl).toEqual('http://localhost:1927/');
});
it('Should not configure given client being an unknown string.', function() {
let container = getContainer();
let client = 'unknown';
let fetchConfig = container.get(FetchConfig);
let configureWithTypo = () => fetchConfig.configure(client);
expect(configureWithTypo).toThrow();
});
it('Should configure given client being a string.', function() {
let container = getContainer();
let client = 'sx/default';
@@ -71,9 +71,17 @@ describe('aurelia-authentication', function() {
let container = new Container();
let baseConfig = container.get(BaseConfig);
configure({container: container, globalResources: noop}, {endpoint: 'something'});
configure({container: container, globalResources: noop}, {baseUrl: 'something'});
expect(baseConfig.current.endpoint).toEqual('something');
expect(baseConfig.current.baseUrl).toEqual('something');
});
it('Should not allow configuration with unregistered endpoint', function() {
let container = new Container();
let configureWithTypo = () => configure({container: container, globalResources: noop}, {endpoint: 'something'});
expect(configureWithTypo).toThrow();
});
it('Should configure configured endpoints.', function() {
@@ -91,6 +99,19 @@ describe('aurelia-authentication', function() {
expect(clientTwoInterceptor).toEqual(configInterceptor);
});
it('Should configure default endpoint.', function() {
let container = getContainer();
let fetchConfig = container.get(FetchConfig);
let clientConfig = container.get(Config);
configure({container: container, globalResources: noop}, {configureEndpoints: ['']});
let clientOneInterceptor = clientConfig.getEndpoint().client.interceptors[0].toString();
let configInterceptor = fetchConfig.interceptor.toString();
expect(clientOneInterceptor).toEqual(configInterceptor);
});
it('Should set the configured endpoint as a client.', function() {
let container = getContainer();
let baseConfig = container.get(BaseConfig);

0 comments on commit 4a44425

Please sign in to comment.