Skip to content

Commit

Permalink
fix(API): query string for non signed requests (#801)
Browse files Browse the repository at this point in the history
* Fixes: query string for non signed requests
  • Loading branch information
elorzafe committed Jun 4, 2018
1 parent 0dad34c commit 6746368
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 14 deletions.
54 changes: 53 additions & 1 deletion packages/aws-amplify/__tests__/API/API-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ describe('API test', () => {
"method": "GET",
"path": "/",
"signerServiceInfo": undefined,
"url": "https://www.amazonaws.compath"
"url": "https://www.amazonaws.compath/"
} , undefined);

});
Expand Down Expand Up @@ -486,6 +486,58 @@ describe('API test', () => {
expect(spyonSigner).toBeCalledWith( expectedParams, creds2 , { region: 'us-east-1', service: 'execute-api'});
});

test('query-string on init-custom-auth', async () => {
const resp = {data: [{name: 'Bob'}]};

const options = {
aws_project_region: 'region',
aws_cloud_logic_custom
};

const api = new API(options);
const creds = {
secretAccessKey: 'secret',
accessKeyId: 'access',
sessionToken: 'token'
};

const creds2 = {
secret_key: 'secret',
access_key: 'access',
session_token: 'token'
};

const spyon = jest.spyOn(Auth, 'currentCredentials').mockImplementation(() => {
return new Promise((res, rej) => {
res(creds);
});
});

const spyon3 = jest.spyOn(RestClient.prototype, 'endpoint').mockImplementationOnce(() => {
return 'endpoint';
});
const spyonRequest = jest.spyOn(RestClient.prototype, '_request').mockImplementationOnce(() => {
return { headers: {}};
});

const spyAxios = jest.spyOn(axios, 'default').mockImplementationOnce(() => {
return new Promise((res, rej) => {
res(resp);
});
});

const init = {
queryStringParameters: {
'ke:y3': 'val:ue 3'
},
headers: {
Authorization: 'apikey'
}
}
await api.get('apiName', '/items', init);
const expectedParams = {"data": null, "headers": {"Authorization": "apikey"}, "host": undefined, "method": "GET", "path": "/", "signerServiceInfo": undefined, "url": "endpoint/items?ke%3Ay3=val%3Aue%203"};
expect(spyonRequest).toBeCalledWith( expectedParams, undefined );
});
test('query-string on init and url', async () => {
const resp = {data: [{name: 'Bob'}]};

Expand Down
27 changes: 14 additions & 13 deletions packages/aws-amplify/src/API/RestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import axios from 'axios';
import Platform from '../Common/Platform';

const logger = new Logger('RestClient'),
url = require('url');
urlLib = require('url');

/**
* HTTP Client for REST requests. Send and receive JSON data.
Expand Down Expand Up @@ -101,6 +101,16 @@ export class RestClient {

params.headers = { ...libraryHeaders, ...(custom_header),...extraParams.headers };

// Intentionally discarding search
const { search, ...parsedUrl } = urlLib.parse(url, true, true);
params.url = urlLib.format({
...parsedUrl,
query: {
...parsedUrl.query,
...(extraParams.queryStringParameters || {})
}
});

// Do not sign the request if client has added 'Authorization' header,
// which means custom authorizer.
if (typeof params.headers['Authorization'] !== 'undefined') {
Expand All @@ -112,10 +122,11 @@ export class RestClient {
// tslint:disable-next-line:align
}, {});
return this._request(params, isAllResponse);

}

return Auth.currentCredentials()
.then(credentials => this._signed({...params, ...extraParams}, credentials, isAllResponse));
.then(credentials => this._signed({ ...params, ...extraParams }, credentials, isAllResponse));
}

/**
Expand Down Expand Up @@ -219,17 +230,7 @@ export class RestClient {
private _signed(params, credentials, isAllResponse) {

const { signerServiceInfo: signerServiceInfoParams, ...otherParams } = params;

// Intentionally discarding search
const { search, ...parsedUrl } = url.parse(otherParams.url, true, true);
otherParams.url = url.format({
...parsedUrl,
query: {
...parsedUrl.query,
...(otherParams.queryStringParameters || {})
}
});


const endpoint_region: string = this._region || this._options.region;
const endpoint_service: string = this._service || this._options.service;

Expand Down

0 comments on commit 6746368

Please sign in to comment.