Skip to content

Commit

Permalink
Add tests - fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren committed Jul 24, 2020
1 parent 80a792f commit 17e1548
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
.nyc_output
24 changes: 22 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
"node": ">=10"
},
"scripts": {
"test": "xo",
"compile": "tsc -p ."
"pretest": "npm run compile -- -p tsconfig.spec.json",
"test": "xo && nyc ava",
"compile": "del-cli dist && tsc"
},
"files": [
"dist"
Expand All @@ -42,10 +43,29 @@
"got": "^11.3.0"
},
"devDependencies": {
"@ava/typescript": "^1.1.1",
"@sindresorhus/tsconfig": "^0.7.0",
"@types/aws4": "^1.5.1",
"@types/sinon": "^9.0.4",
"ava": "^3.10.1",
"aws-sdk": "^2.700.0",
"del-cli": "^3.0.1",
"nock": "^13.0.2",
"nyc": "^15.1.0",
"sinon": "^9.0.2",
"typescript": "^3.9.5",
"xo": "^0.32.0"
},
"ava": {
"typescript": {
"rewritePaths": {
"source/": "dist/"
}
}
},
"nyc": {
"exclude": [
"source/test.ts"
]
}
}
103 changes: 103 additions & 0 deletions source/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import test from 'ava';
import * as nock from 'nock';
import * as sinon from 'sinon';
import {Response} from 'got';
import got4aws from '.';

process.env.AWS_ACCESS_KEY_ID = 'unicorn';
process.env.AWS_SECRET_ACCESS_KEY = 'rainbow';

const extractHeaders = (response: Response) => ({
'X-Amz-Date': response.request.options.headers['X-Amz-Date'],
Authorization: response.request.options.headers.Authorization
});

let clock: sinon.SinonFakeTimers;

test.before(() => {
clock = sinon.useFakeTimers(new Date('2020-07-01T10:00:00Z'));

nock('http://www.example.com')
.get('/resource')
.reply(200, '{"unicorn":"🦄"}')
.persist();

nock('https://rainbow.execute-api.eu-west-1.amazonaws.com')
.get('/v0')
.reply(200, '{"rainbow":"🌈"}')
.persist();
});

test.after(() => {
clock.restore();
});

test('parse response as `json` by default', async t => {
const got = got4aws();

const result = await got('http://www.example.com/resource');

t.deepEqual(result.body as unknown, {
unicorn: '🦄'
});
});

test('override `responseType`', async t => {
const got = got4aws();

const result = await got('http://www.example.com/resource', {
responseType: 'text'
});

t.is(result.body, '{"unicorn":"🦄"}');
});

test('provide service option', async t => {
const got = got4aws({
service: 'execute-api'
});

const result = await got('http://www.example.com/resource');

t.deepEqual(extractHeaders(result), {
'X-Amz-Date': '20200701T100000Z',
Authorization: 'AWS4-HMAC-SHA256 Credential=unicorn/20200701/us-east-1/execute-api/aws4_request, SignedHeaders=accept;accept-encoding;host;user-agent;x-amz-date, Signature=b6c4ab33c3992fca1f90e5003be094a468eb71655f3891c98aec815b60ceaa23'
});

t.deepEqual(result.body as unknown, {
unicorn: '🦄'
});
});

test('provide region option', async t => {
const got = got4aws({
service: 'execute-api',
region: 'eu-west-1'
});

const result = await got('http://www.example.com/resource');

t.deepEqual(extractHeaders(result), {
'X-Amz-Date': '20200701T100000Z',
Authorization: 'AWS4-HMAC-SHA256 Credential=unicorn/20200701/eu-west-1/execute-api/aws4_request, SignedHeaders=accept;accept-encoding;host;user-agent;x-amz-date, Signature=2f30e9cec0e1429031f846074642b0f247b61c36e51159385057bbfb18e10fc4'
});

t.deepEqual(result.body as unknown, {
unicorn: '🦄'
});
});

test('infer service and region', async t => {
const got = got4aws();

const result = await got('https://rainbow.execute-api.eu-west-1.amazonaws.com/v0');

t.deepEqual(extractHeaders(result), {
'X-Amz-Date': '20200701T100000Z',
Authorization: 'AWS4-HMAC-SHA256 Credential=unicorn/20200701/eu-west-1/execute-api/aws4_request, SignedHeaders=accept;accept-encoding;host;user-agent;x-amz-date, Signature=f41b94660fa4d74b99058b4e6754cb28e33c2c1cf6ddfea4cc96826cd5d65a63'
});

t.deepEqual(result.body as unknown, {
rainbow: '🌈'
});
});
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
},
"include": [
"source"
],
"exclude": [
"source/test.ts"
]
}
10 changes: 10 additions & 0 deletions tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"sourceMap": true
},
"include": [
"source"
],
"exclude": []
}

0 comments on commit 17e1548

Please sign in to comment.