Skip to content

Commit

Permalink
Merge pull request aws-amplify#118 from powerful23/api_cred_refresh
Browse files Browse the repository at this point in the history
Api cred refresh
  • Loading branch information
powerful23 authored Jan 18, 2018
2 parents be8db91 + 7396f94 commit aaf9e0f
Show file tree
Hide file tree
Showing 15 changed files with 1,553 additions and 711 deletions.
558 changes: 558 additions & 0 deletions packages/aws-amplify/__tests__/API/API-test.ts

Large diffs are not rendered by default.

66 changes: 60 additions & 6 deletions packages/aws-amplify/__tests__/API/RestClient-unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jest.mock('axios', () => {
default: (signed_params) => {
return new Promise((res, rej) => {
res({
data: 'data';
data: 'data'
})
});
}
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('RestClient test', () => {

const restClient = new RestClient(apiOptions);

expect(await restClient.ajax('url', 'method')).toEqual('data');
expect(await restClient.ajax('url', 'method', {})).toEqual('data');
});

test('ajax with no credentials', async () => {
Expand All @@ -101,11 +101,65 @@ describe('RestClient test', () => {
const restClient = new RestClient(apiOptions);

try {
await restClient.ajax('url', 'method');
await restClient.ajax('url', 'method', {});
} catch (e) {
expect(e).toBe('credentials not set for API rest client ');
}
});

test('ajax with extraParams', async () => {
window.fetch = jest.fn().mockImplementationOnce((signed_params_url, signed_params) => {
return new Promise((res, rej) => {
res({
status: '200',
json: () => {
return signed_params.data;
}
});
});
});

const apiOptions = {
headers: {},
endpoints: {},
credentials: {
accessKeyId: 'accessKeyId',
secretAccessKey: 'secretAccessKey',
sessionToken: 'sessionToken'
}
};

const restClient = new RestClient(apiOptions);

expect(await restClient.ajax('url', 'method', {body: 'body'})).toEqual('data');
});

test('ajax with Authorization header', async () => {
window.fetch = jest.fn().mockImplementationOnce((signed_params_url, signed_params) => {
return new Promise((res, rej) => {
res({
status: '200',
json: () => {
return signed_params.data;
}
});
});
});

const apiOptions = {
headers: {},
endpoints: {},
credentials: {
accessKeyId: 'accessKeyId',
secretAccessKey: 'secretAccessKey',
sessionToken: 'sessionToken'
}
};

const restClient = new RestClient(apiOptions);

expect(await restClient.ajax('url', 'method', {headers: {Authorization: 'authorization'}})).toEqual('data');
});
});

describe('get test', () => {
Expand Down Expand Up @@ -136,7 +190,7 @@ describe('RestClient test', () => {
const restClient = new RestClient(apiOptions);

expect.assertions(2);
await restClient.get('url');
await restClient.get('url', {});

expect(spyon.mock.calls[0][0]).toBe('url');
expect(spyon.mock.calls[0][1]).toBe('GET');
Expand Down Expand Up @@ -247,7 +301,7 @@ describe('RestClient test', () => {
const restClient = new RestClient(apiOptions);

expect.assertions(2);
await restClient.del('url');
await restClient.del('url', {});

expect(spyon.mock.calls[0][0]).toBe('url');
expect(spyon.mock.calls[0][1]).toBe('DELETE');
Expand Down Expand Up @@ -283,7 +337,7 @@ describe('RestClient test', () => {
const restClient = new RestClient(apiOptions);

expect.assertions(2);
await restClient.head('url');
await restClient.head('url', {});

expect(spyon.mock.calls[0][0]).toBe('url');
expect(spyon.mock.calls[0][1]).toBe('HEAD');
Expand Down
Loading

0 comments on commit aaf9e0f

Please sign in to comment.