From 17e1548dda3967f1aa48e165353d8dd0e7566f84 Mon Sep 17 00:00:00 2001 From: Sam Verschueren Date: Fri, 24 Jul 2020 20:54:58 +0200 Subject: [PATCH] Add tests - fixes #1 --- .gitignore | 1 + package.json | 24 ++++++++++- source/test.ts | 103 +++++++++++++++++++++++++++++++++++++++++++++ tsconfig.json | 3 ++ tsconfig.spec.json | 10 +++++ 5 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 source/test.ts create mode 100644 tsconfig.spec.json diff --git a/.gitignore b/.gitignore index f06235c..7aebd30 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules dist +.nyc_output diff --git a/package.json b/package.json index 3880021..78b2abe 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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" + ] } } diff --git a/source/test.ts b/source/test.ts new file mode 100644 index 0000000..cccda2e --- /dev/null +++ b/source/test.ts @@ -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: '🌈' + }); +}); diff --git a/tsconfig.json b/tsconfig.json index 4064763..9031139 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,5 +11,8 @@ }, "include": [ "source" + ], + "exclude": [ + "source/test.ts" ] } diff --git a/tsconfig.spec.json b/tsconfig.spec.json new file mode 100644 index 0000000..dd068f1 --- /dev/null +++ b/tsconfig.spec.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "sourceMap": true + }, + "include": [ + "source" + ], + "exclude": [] +}