Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api cred refresh #118

Merged
merged 13 commits into from
Jan 18, 2018
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