diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 6a1f10bd9..02da44977 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -38,8 +38,15 @@ jobs: TEST_REFRESH_TOKEN: ${{ secrets.TEST_REFRESH_TOKEN }} TEST_SCOPE: ${{ secrets.TEST_SCOPE }} CI: true - - name: push - uses: github-actions-x/commit@v2.1 + - name: Commit files + run: | + git config --local user.email "scale-bot@scaleleap.com" + git config --local user.name "Scale Bot" + git add -A test/__recordings__ + git diff-index --quiet HEAD || git commit -m "test: add Polly recordings" + - name: Push changes + uses: ad-m/github-push-action@master with: - github-token: ${{ secrets.GITHUB_TOKEN }} - commit-message: 'test: adds Polly recordings' + branch: 'refs/heads/polly-recordings' + force: true + github_token: ${{ secrets.GITHUB_TOKEN }} diff --git a/test/__recordings__/HttpClient_3032634686/download_822653945/should-throw-if-location-header-not-set_2915274416/recording.har b/test/__recordings__/HttpClient_3032634686/download_822653945/should-throw-if-location-header-not-set_2915274416/recording.har new file mode 100644 index 000000000..b8eab30a4 --- /dev/null +++ b/test/__recordings__/HttpClient_3032634686/download_822653945/should-throw-if-location-header-not-set_2915274416/recording.har @@ -0,0 +1,132 @@ +{ + "log": { + "_recordingName": "HttpClient/download/should throw if location header not set", + "creator": { + "comment": "persister:fs", + "name": "Polly.JS", + "version": "2.6.3" + }, + "entries": [ + { + "_id": "3909d7024945934b0705af99425c9a88", + "_order": 0, + "cache": {}, + "request": { + "bodySize": 0, + "cookies": [], + "headers": [ + { + "_fromType": "array", + "name": "content-type", + "value": "application/json" + }, + { + "_fromType": "array", + "name": "accept", + "value": "application/json" + }, + { + "_fromType": "array", + "name": "user-agent", + "value": "amazon-advertising-api-nodejs-sdk-2/1.0.0" + }, + { + "_fromType": "array", + "name": "amazon-advertising-api-scope", + "value": "-1" + }, + { + "_fromType": "array", + "name": "bidding_controls_on", + "value": "true" + }, + { + "_fromType": "array", + "name": "accept-encoding", + "value": "application/json" + }, + { + "_fromType": "array", + "name": "connection", + "value": "close" + }, + { + "name": "host", + "value": "advertising-api-test.amazon.com" + } + ], + "headersSize": 332, + "httpVersion": "HTTP/1.1", + "method": "GET", + "queryString": [], + "url": "https://advertising-api-test.amazon.com/profiles" + }, + "response": { + "bodySize": 120, + "content": { + "mimeType": "application/json; charset=utf-8", + "size": 120, + "text": "{\"fileSize\":518,\"location\":\"\",\"snapshotId\":\"amzn1.clicksAPI.v1.p1.5C8B19EB.7298de0e-17cd-441f-bf5c-17a27406b0d6\",\"status\":\"SUCCESS\",\"statusDetails\":\"Snapshot has been successfully generated.\"}" + }, + "cookies": [], + "headers": [ + { + "name": "server", + "value": "Server" + }, + { + "name": "date", + "value": "Mon, 21 Oct 2019 14:52:05 GMT" + }, + { + "name": "content-type", + "value": "application/json; charset=utf-8" + }, + { + "name": "content-length", + "value": "120" + }, + { + "name": "connection", + "value": "close" + }, + { + "name": "access-control-allow-headers", + "value": "Authorization, Amazon-Advertising-API-ClientId, Amazon-Advertising-API-Scope" + }, + { + "name": "vary", + "value": "Accept-Encoding,X-Amzn-CDN-Cache,X-Amzn-AX-Treatment,User-Agent" + }, + { + "name": "x-amz-rid", + "value": "QF529SXZVX51S6Y34X5B" + }, + { + "name": "location", + "value": "" + } + ], + "headersSize": 366, + "httpVersion": "HTTP/1.1", + "redirectURL": "", + "status": 200, + "statusText": "OK" + }, + "startedDateTime": "2019-10-21T14:52:03.690Z", + "time": 1556, + "timings": { + "blocked": -1, + "connect": -1, + "dns": -1, + "receive": 0, + "send": 0, + "ssl": -1, + "wait": 1556 + } + } + ], + "pages": [], + "version": "1.2" + } +} diff --git a/test/__recordings__/HttpClient_3032634686/get_1410115415/should-throw-NullError-when-response-body-is-null_2569875493/recording.har b/test/__recordings__/HttpClient_3032634686/get_1410115415/should-throw-NullError-when-response-body-is-null_2569875493/recording.har new file mode 100644 index 000000000..c820f67d2 --- /dev/null +++ b/test/__recordings__/HttpClient_3032634686/get_1410115415/should-throw-NullError-when-response-body-is-null_2569875493/recording.har @@ -0,0 +1,127 @@ +{ + "log": { + "_recordingName": "HttpClient/get/should throw NullError when response body is null", + "creator": { + "comment": "persister:fs", + "name": "Polly.JS", + "version": "2.6.3" + }, + "entries": [ + { + "_id": "4ae7ff6530e7eae10621b9b5a91d46dd", + "_order": 0, + "cache": {}, + "request": { + "bodySize": 0, + "cookies": [], + "headers": [ + { + "_fromType": "array", + "name": "content-type", + "value": "application/json" + }, + { + "_fromType": "array", + "name": "accept", + "value": "application/json" + }, + { + "_fromType": "array", + "name": "user-agent", + "value": "amazon-advertising-api-nodejs-sdk-2/1.0.0" + }, + { + "_fromType": "array", + "name": "amazon-advertising-api-scope", + "value": "-1" + }, + { + "_fromType": "array", + "name": "bidding_controls_on", + "value": "true" + }, + { + "_fromType": "array", + "name": "accept-encoding", + "value": "application/json" + }, + { + "_fromType": "array", + "name": "connection", + "value": "close" + }, + { + "name": "host", + "value": "advertising-api-test.amazon.com" + } + ], + "headersSize": 333, + "httpVersion": "HTTP/1.1", + "method": "GET", + "queryString": [], + "url": "https://advertising-api-test.amazon.com/null-body" + }, + "response": { + "bodySize": 121, + "content": { + "mimeType": "application/json; charset=utf-8", + "size": 121 + }, + "cookies": [], + "headers": [ + { + "name": "server", + "value": "Server" + }, + { + "name": "date", + "value": "Sun, 20 Oct 2019 09:42:20 GMT" + }, + { + "name": "content-type", + "value": "application/json; charset=utf-8" + }, + { + "name": "content-length", + "value": "121" + }, + { + "name": "connection", + "value": "close" + }, + { + "name": "access-control-allow-headers", + "value": "Authorization, Amazon-Advertising-API-ClientId, Amazon-Advertising-API-Scope" + }, + { + "name": "vary", + "value": "Accept-Encoding,X-Amzn-CDN-Cache,X-Amzn-AX-Treatment,User-Agent" + }, + { + "name": "x-amz-rid", + "value": "58TSNQCDM43H40RJ3MRM" + } + ], + "headersSize": 354, + "httpVersion": "HTTP/1.1", + "redirectURL": "", + "status": 200, + "statusText": "OK" + }, + "startedDateTime": "2019-10-20T09:42:20.676Z", + "time": 1280, + "timings": { + "blocked": -1, + "connect": -1, + "dns": -1, + "receive": 0, + "send": 0, + "ssl": -1, + "wait": 1280 + } + } + ], + "pages": [], + "version": "1.2" + } +} diff --git a/test/__recordings__/HttpClient_3032634686/get_1410115415/should-throw-a-ResourceNotFoundError-when-resource-is-not-found_519673699/recording.har b/test/__recordings__/HttpClient_3032634686/get_1410115415/should-throw-a-ResourceNotFoundError-when-resource-is-not-found_519673699/recording.har new file mode 100644 index 000000000..64d772ea4 --- /dev/null +++ b/test/__recordings__/HttpClient_3032634686/get_1410115415/should-throw-a-ResourceNotFoundError-when-resource-is-not-found_519673699/recording.har @@ -0,0 +1,128 @@ +{ + "log": { + "_recordingName": "HttpClient/get/should throw a ResourceNotFoundError when resource is not found", + "creator": { + "comment": "persister:fs", + "name": "Polly.JS", + "version": "2.6.3" + }, + "entries": [ + { + "_id": "2da808cdd3d89cb18c7128a19ffd1e4d", + "_order": 0, + "cache": {}, + "request": { + "bodySize": 0, + "cookies": [], + "headers": [ + { + "_fromType": "array", + "name": "content-type", + "value": "application/json" + }, + { + "_fromType": "array", + "name": "accept", + "value": "application/json" + }, + { + "_fromType": "array", + "name": "user-agent", + "value": "amazon-advertising-api-nodejs-sdk-2/1.0.0" + }, + { + "_fromType": "array", + "name": "amazon-advertising-api-scope", + "value": "-1" + }, + { + "_fromType": "array", + "name": "bidding_controls_on", + "value": "true" + }, + { + "_fromType": "array", + "name": "accept-encoding", + "value": "application/json" + }, + { + "_fromType": "array", + "name": "connection", + "value": "close" + }, + { + "name": "host", + "value": "advertising-api-test.amazon.com" + } + ], + "headersSize": 330, + "httpVersion": "HTTP/1.1", + "method": "GET", + "queryString": [], + "url": "https://advertising-api-test.amazon.com/foobar" + }, + "response": { + "bodySize": 118, + "content": { + "mimeType": "application/json; charset=utf-8", + "size": 118, + "text": "{\"code\":\"NOT_FOUND\",\"details\":\"Could not find resource for full path: https://advertising-api-test.amazon.com/foobar\"}" + }, + "cookies": [], + "headers": [ + { + "name": "server", + "value": "Server" + }, + { + "name": "date", + "value": "Mon, 21 Oct 2019 14:50:10 GMT" + }, + { + "name": "content-type", + "value": "application/json; charset=utf-8" + }, + { + "name": "content-length", + "value": "118" + }, + { + "name": "connection", + "value": "close" + }, + { + "name": "access-control-allow-headers", + "value": "Authorization, Amazon-Advertising-API-ClientId, Amazon-Advertising-API-Scope" + }, + { + "name": "vary", + "value": "Accept-Encoding,X-Amzn-CDN-Cache,X-Amzn-AX-Treatment,User-Agent" + }, + { + "name": "x-amz-rid", + "value": "QSE24QBK9VRR4DAQ9GAF" + } + ], + "headersSize": 354, + "httpVersion": "HTTP/1.1", + "redirectURL": "", + "status": 404, + "statusText": "Not Found" + }, + "startedDateTime": "2019-10-21T14:50:08.992Z", + "time": 1027, + "timings": { + "blocked": -1, + "connect": -1, + "dns": -1, + "receive": 0, + "send": 0, + "ssl": -1, + "wait": 1027 + } + } + ], + "pages": [], + "version": "1.2" + } +} diff --git a/test/__recordings__/HttpClient_3032634686/get_1410115415/should-throw-a-known-error-object-when-encountering-an-error_3874394417/recording.har b/test/__recordings__/HttpClient_3032634686/get_1410115415/should-throw-a-known-error-object-when-encountering-an-error_3874394417/recording.har new file mode 100644 index 000000000..4baab63f2 --- /dev/null +++ b/test/__recordings__/HttpClient_3032634686/get_1410115415/should-throw-a-known-error-object-when-encountering-an-error_3874394417/recording.har @@ -0,0 +1,131 @@ +{ + "log": { + "_recordingName": "HttpClient/get/should throw a known error object when encountering an error", + "creator": { + "comment": "persister:fs", + "name": "Polly.JS", + "version": "2.6.3" + }, + "entries": [ + { + "_id": "43357c92bddb7781cb64ffc7275cb1a8", + "_order": 0, + "cache": {}, + "request": { + "bodySize": 0, + "cookies": [], + "headers": [ + { + "_fromType": "array", + "name": "content-type", + "value": "application/json" + }, + { + "_fromType": "array", + "name": "accept", + "value": "application/json" + }, + { + "_fromType": "array", + "name": "user-agent", + "value": "amazon-advertising-api-nodejs-sdk-2/1.0.0" + }, + { + "_fromType": "array", + "name": "amazon-advertising-api-scope", + "value": "-1" + }, + { + "_fromType": "array", + "name": "bidding_controls_on", + "value": "true" + }, + { + "_fromType": "array", + "name": "accept-encoding", + "value": "application/json" + }, + { + "_fromType": "array", + "name": "connection", + "value": "close" + }, + { + "name": "host", + "value": "advertising-api-test.amazon.com" + } + ], + "headersSize": 345, + "httpVersion": "HTTP/1.1", + "method": "GET", + "queryString": [], + "url": "https://advertising-api-test.amazon.com/encountering-an-error" + }, + "response": { + "bodySize": 168, + "content": { + "mimeType": "application/json; charset=utf-8", + "size": 168 + }, + "cookies": [], + "headers": [ + { + "name": "server", + "value": "Server" + }, + { + "name": "date", + "value": "Tue, 22 Oct 2019 13:00:03 GMT" + }, + { + "name": "content-type", + "value": "application/json; charset=utf-8" + }, + { + "name": "content-length", + "value": "168" + }, + { + "name": "connection", + "value": "close" + }, + { + "name": "access-control-allow-headers", + "value": "Authorization, Amazon-Advertising-API-ClientId, Amazon-Advertising-API-Scope" + }, + { + "name": "x-amz-request-id", + "value": "956MS4RMQPN2K28ZGKPM" + }, + { + "name": "vary", + "value": "Accept-Encoding,X-Amzn-CDN-Cache,X-Amzn-AX-Treatment,User-Agent" + }, + { + "name": "x-amz-rid", + "value": "956MS4RMQPN2K28ZGKPM" + } + ], + "headersSize": 394, + "httpVersion": "HTTP/1.1", + "redirectURL": "", + "status": 500, + "statusText": "Internal Server Error" + }, + "startedDateTime": "2019-10-22T13:00:01.920Z", + "time": 1073, + "timings": { + "blocked": -1, + "connect": -1, + "dns": -1, + "receive": 0, + "send": 0, + "ssl": -1, + "wait": 1073 + } + } + ], + "pages": [], + "version": "1.2" + } +} diff --git a/test/__recordings__/OAuthClient_3630928931/refresh-an-existing-token-polly-passthrough_1059744169/recording.har b/test/__recordings__/OAuthClient_3630928931/refresh-an-existing-token-polly-passthrough_1059744169/recording.har new file mode 100644 index 000000000..b4dbbbc68 --- /dev/null +++ b/test/__recordings__/OAuthClient_3630928931/refresh-an-existing-token-polly-passthrough_1059744169/recording.har @@ -0,0 +1,144 @@ +{ + "log": { + "_recordingName": "OAuthClient/refresh an existing token polly:passthrough", + "creator": { + "comment": "persister:fs", + "name": "Polly.JS", + "version": "2.6.3" + }, + "entries": [ + { + "_id": "6bc0a106332134ac2ad55c87b684ae22", + "_order": 0, + "cache": {}, + "request": { + "bodySize": 650, + "cookies": [], + "headers": [ + { + "_fromType": "array", + "name": "accept-encoding", + "value": "application/json" + }, + { + "_fromType": "array", + "name": "user-agent", + "value": "amazon-advertising-api-nodejs-sdk-2/1.0.0" + }, + { + "_fromType": "array", + "name": "accept", + "value": "application/json, application/x-www-form-urlencoded" + }, + { + "_fromType": "array", + "name": "content-type", + "value": "application/x-www-form-urlencoded" + }, + { + "_fromType": "array", + "name": "content-length", + "value": "650" + }, + { + "_fromType": "array", + "name": "connection", + "value": "close" + }, + { + "name": "host", + "value": "api.amazon.com" + }, + { + "name": "authorization", + "value": "Basic x" + } + ], + "headersSize": 340, + "httpVersion": "HTTP/1.1", + "method": "POST", + "postData": { + "mimeType": "application/x-www-form-urlencoded", + "params": [], + "text": "refresh_token=x&grant_type=refresh_token" + }, + "queryString": [], + "url": "https://api.amazon.com/auth/o2/token" + }, + "response": { + "bodySize": 1318, + "content": { + "mimeType": "application/json;charset=UTF-8", + "size": 1318, + "text": "{\"access_token\":\"x\",\"expires_in\":3600,\"refresh_token\":\"x\",\"token_type\":\"bearer\"}" + }, + "cookies": [], + "headers": [ + { + "name": "server", + "value": "Server" + }, + { + "name": "date", + "value": "Sun, 20 Oct 2019 13:00:35 GMT" + }, + { + "name": "content-type", + "value": "application/json;charset=UTF-8" + }, + { + "name": "content-length", + "value": "1318" + }, + { + "name": "connection", + "value": "close" + }, + { + "name": "x-amzn-requestid", + "value": "acad2491-9a2d-4380-abc5-8b944a3e318f" + }, + { + "name": "x-amz-date", + "value": "Sun, 20 Oct 2019 13:00:35 GMT" + }, + { + "name": "cache-control", + "value": "no-cache, no-store, must-revalidate" + }, + { + "name": "pragma", + "value": "no-cache" + }, + { + "name": "vary", + "value": "Accept-Encoding,X-Amzn-CDN-Cache,X-Amzn-AX-Treatment,User-Agent" + }, + { + "name": "x-amz-rid", + "value": "1WPHQPHEWK0372H5H9PT" + } + ], + "headersSize": 415, + "httpVersion": "HTTP/1.1", + "redirectURL": "", + "status": 200, + "statusText": "OK" + }, + "startedDateTime": "2019-10-20T13:00:34.914Z", + "time": 201, + "timings": { + "blocked": -1, + "connect": -1, + "dns": -1, + "receive": 0, + "send": 0, + "ssl": -1, + "wait": 201 + } + } + ], + "pages": [], + "version": "1.2" + } +} diff --git a/test/http-client-factory.ts b/test/http-client-factory.ts index b2d66a1c7..778dc3df5 100644 --- a/test/http-client-factory.ts +++ b/test/http-client-factory.ts @@ -2,7 +2,7 @@ import { HttpClient, HttpClientAuth } from '../src/http-client' import { config } from './config' const SANDBOX = true -const SANDBOX_URI = 'https://advertising-api-test.amazon.com' +export const SANDBOX_URI = 'https://advertising-api-test.amazon.com' export const auth: HttpClientAuth = { authorizationToken: config.TEST_ACCESS_TOKEN || '', diff --git a/test/http-client.test.ts b/test/http-client.test.ts index 69043f9e8..1c80a4f33 100644 --- a/test/http-client.test.ts +++ b/test/http-client.test.ts @@ -1,17 +1,18 @@ +import HttpStatus from 'http-status-codes' import setupPolly from './polly' import { HttpClient } from '../src/http-client' import { - UnauthorizedError, NullError, ResourceNotFoundError, InvalidProgramStateError, + GenericError, } from '../src/errors' -import { httpClientFactory } from './http-client-factory' +import { httpClientFactory, SANDBOX_URI } from './http-client-factory' -describe('HttpClient', () => { - setupPolly() +const context = setupPolly() +describe('HttpClient', () => { let client: HttpClient beforeEach(() => { client = httpClientFactory() @@ -23,21 +24,60 @@ describe('HttpClient', () => { expect(Array.isArray(res)).toBeTruthy() }) - it.skip('should throw a known error object when encountering an error', () => { - return expect(client.get('profiles')).rejects.toThrow(UnauthorizedError) + it('should throw a known error object when encountering an error', () => { + const server = context.polly.server + + server.get(SANDBOX_URI + '/encountering-an-error').on('beforeResponse', (req, res) => { + res.status(HttpStatus.INTERNAL_SERVER_ERROR) + res.send('') + }) + + return expect(client.get('encountering-an-error')).rejects.toBeInstanceOf(GenericError) }) - it.skip('should throw NullError when response body is null', () => { - return expect(client.get('profiles')).rejects.toThrow(NullError) + it('should throw NullError when response body is null', () => { + const server = context.polly.server + + server.get(SANDBOX_URI + '/null-body').on('beforeResponse', (req, res) => { + res.status(HttpStatus.OK) + res.send('') + }) + + return expect(client.get('null-body')).rejects.toThrow(NullError) }) it('should throw a ResourceNotFoundError when resource is not found', () => { + const server = context.polly.server + + server.get(SANDBOX_URI + '/foobar').on('beforeResponse', (req, res) => { + res.status(HttpStatus.NOT_FOUND) + res.send({ + code: 'NOT_FOUND', + details: + 'Could not find resource for full path: https://advertising-api-test.amazon.com/foobar', + }) + }) + return expect(client.get('foobar')).rejects.toThrow(ResourceNotFoundError) }) }) - describe.skip('download', () => { + describe('download', () => { it('should throw if location header not set', async () => { + const server = context.polly.server + + server.get(SANDBOX_URI + '/profiles').on('beforeResponse', (req, res) => { + res.setHeader('Location', '') + res.status(HttpStatus.OK) + res.send({ + snapshotId: 'amzn1.clicksAPI.v1.p1.5C8B19EB.7298de0e-17cd-441f-bf5c-17a27406b0d6', + status: 'SUCCESS', + statusDetails: 'Snapshot has been successfully generated.', + location: '', + fileSize: 518, + }) + }) + const promise = client.download('profiles') return expect(promise).rejects.toThrowError(InvalidProgramStateError) }) diff --git a/test/o-auth-client.test.ts b/test/o-auth-client.test.ts index acb1edf1c..7bd74188d 100644 --- a/test/o-auth-client.test.ts +++ b/test/o-auth-client.test.ts @@ -1,35 +1,14 @@ import { OAuthClient } from '../src/o-auth-client' import { Token } from 'client-oauth2' import { config } from './config' -import { PollyJS } from './pollyjs/polly' +import setupPolly from './polly' import { parse, stringify } from 'querystring' const URI = 'https://example.com' const PLACEHOLDER = 'x' -const pollyJs = new PollyJS('RefreshToken') -const server = pollyJs.getPollyServer() -const polly = pollyJs.getPollyInstance() - -server.post('https://api.amazon.com/auth/o2/token').on('beforeResponse', (req, res) => { - /* eslint-disable @typescript-eslint/camelcase */ - req.body = stringify( - Object.assign(parse(req.body), { - refresh_token: PLACEHOLDER, - }), - ) - - req.setHeader('authorization', `Basic ${PLACEHOLDER}`) - - res.body = JSON.stringify( - Object.assign(JSON.parse(res.body), { - access_token: PLACEHOLDER, - refresh_token: PLACEHOLDER, - }), - ) - /* eslint-enable @typescript-eslint/camelcase */ -}) describe(OAuthClient.name, () => { + const context = setupPolly() let client: OAuthClient beforeEach(() => { @@ -38,6 +17,27 @@ describe(OAuthClient.name, () => { clientSecret: 'foo', redirectUri: URI, }) + + context.polly.server + .post('https://api.amazon.com/auth/o2/token') + .on('beforeResponse', (req, res) => { + /* eslint-disable @typescript-eslint/camelcase */ + req.body = stringify( + Object.assign(parse(req.body), { + refresh_token: PLACEHOLDER, + }), + ) + + req.setHeader('authorization', `Basic ${PLACEHOLDER}`) + + res.body = JSON.stringify( + Object.assign(JSON.parse(res.body), { + access_token: PLACEHOLDER, + refresh_token: PLACEHOLDER, + }), + ) + /* eslint-enable @typescript-eslint/camelcase */ + }) }) it('should provide a correct uri', () => { @@ -73,7 +73,5 @@ describe(OAuthClient.name, () => { expect(res.accessToken).toBe(PLACEHOLDER) expect(res.refreshToken).toBe(PLACEHOLDER) expect(res.tokenType).toBe('bearer') - - await polly.stop() }) }) diff --git a/test/pollyjs/polly.ts b/test/pollyjs/polly.ts deleted file mode 100644 index 6250237a5..000000000 --- a/test/pollyjs/polly.ts +++ /dev/null @@ -1,35 +0,0 @@ -import path from 'path' -import { Polly, PollyServer } from '@pollyjs/core' -import NodeHttpAdapter from '@pollyjs/adapter-node-http' -import FSPersister from '@pollyjs/persister-fs' -import { config } from '../config' - -Polly.register(FSPersister) -Polly.register(NodeHttpAdapter) - -export class PollyJS { - recordingName: string - polly: Polly - - constructor(recordingName: string) { - this.recordingName = recordingName - this.polly = new Polly(this.recordingName, { - mode: config.POLLY_MODE, - adapters: ['node-http'], - persister: 'fs', - persisterOptions: { - fs: { - recordingsDir: path.resolve(__dirname, '../__recordings__'), - }, - }, - }) - } - - getPollyInstance(): Polly { - return this.polly - } - - getPollyServer(): PollyServer { - return this.polly.server - } -} diff --git a/test/pollyjs/pollyjs.test.ts b/test/pollyjs/pollyjs.test.ts deleted file mode 100644 index a1865ce4d..000000000 --- a/test/pollyjs/pollyjs.test.ts +++ /dev/null @@ -1,47 +0,0 @@ -import fetch from 'cross-fetch' -import { PollyJS } from './polly' - -const pollyJs = new PollyJS('Polly Test') -const server = pollyJs.getPollyServer() -const polly = pollyJs.getPollyInstance() - -server - .post('https://reqres.in/api/register') - .on('beforeResponse', (req, res) => { - req.send({ - email: 'test@example.com', - password: '123456', - }) - res.send({ - token: 'yyy', - id: 1, - }) - }) - .on('beforePersist', (req, recording) => { - const response = JSON.parse(recording.response.content.text) - response.token = 'xxx' - recording.response.content.text = response - }) - -describe('https://reqres.in/api/register', () => { - it('should filter token', async () => { - const response = await fetch('https://reqres.in/api/register', { - method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - email: 'eve.holt@reqres.in', - password: 'pistol', - }), - }).then(response => { - return response.json() - }) - - expect(response.id).toEqual(1) - expect(response.token).toEqual('yyy') - - await polly.stop() - }) -})