Skip to content

Commit

Permalink
Fix signing other http methods than GET - fixes #7 (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-xo7 authored and SamVerschueren committed Dec 14, 2020
1 parent 8db1ca6 commit c364230
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const got4aws = (awsOptions: GotAWSOptions = {}) => {
const request = {
protocol: url.protocol,
host: url.host,
method: options.method,
path: url.pathname + url.search,
headers: signingHeaders,
body: options.json ? JSON.stringify(options.json) : options.body,
Expand Down
52 changes: 52 additions & 0 deletions source/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import test from 'ava';
import * as nock from 'nock';
import * as sinon from 'sinon';
import {Response} from 'got';
import * as aws4 from 'aws4';
import got4aws from '.';

process.env.AWS_ACCESS_KEY_ID = 'unicorn';
Expand All @@ -22,6 +23,10 @@ test.before(() => {
.reply(200, '{"unicorn":"🦄"}')
.get('/resource?unicorn=rainbow')
.reply(200, '{"unicorn":"rainbow"}')
.post('/resource')
.reply(204)
.patch('/resource')
.reply(204)
.persist();

nock('https://rainbow.execute-api.eu-west-1.amazonaws.com')
Expand Down Expand Up @@ -120,3 +125,50 @@ test('infer service and region', async t => {
rainbow: '🌈'
});
});

test('use POST method', async t => {
const got = got4aws();
const payload = {test: 'yellow'};
const result = await got.post('http://www.example.com/resource', {json: payload});

const request = {
method: 'POST',
host: 'www.example.com',
path: '/resource',
headers: {
Accept: 'application/json',
'Accept-Encoding': 'gzip, deflate, br',
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
};
const sign = aws4.sign(request);

t.deepEqual(extractHeaders(result), {
'X-Amz-Date': sign.headers['X-Amz-Date'],
Authorization: sign.headers.Authorization
});
t.is(result.statusCode, 204);
});

test('use PATCH method', async t => {
const got = got4aws();
const result = await got.patch('http://www.example.com/resource');

const request = {
method: 'PATCH',
host: 'www.example.com',
path: '/resource',
headers: {
Accept: 'application/json',
'Accept-Encoding': 'gzip, deflate, br'
}
};
const sign = aws4.sign(request);

t.deepEqual(extractHeaders(result), {
'X-Amz-Date': sign.headers['X-Amz-Date'],
Authorization: sign.headers.Authorization
});
t.is(result.statusCode, 204);
});

0 comments on commit c364230

Please sign in to comment.