Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
fix(configure): fail if specified endpoints are not registered
Browse files Browse the repository at this point in the history
  • Loading branch information
doktordirk committed Mar 31, 2016
1 parent e697ad9 commit 4a44425
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 11 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,


Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/app.fetch-httpClient.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
11 changes: 9 additions & 2 deletions src/aurelia-authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions src/baseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,


Expand Down
10 changes: 10 additions & 0 deletions test/app.fetch-httpClient.config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
25 changes: 23 additions & 2 deletions test/aurelia-authentication.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
Expand Down

0 comments on commit 4a44425

Please sign in to comment.