From e24dd94524e1a2a4a04b4f5296e102b75dac4583 Mon Sep 17 00:00:00 2001 From: dostrikov Date: Thu, 22 Aug 2024 15:02:04 +0300 Subject: [PATCH] fix(postman): introduce support for v2.0.0 auth object (#251) fixes #250 --- .../postman/src/converter/DefaultConverter.ts | 23 +++++-- .../postman/tests/DefaultConverter.spec.ts | 5 ++ .../v2.0.0-auth.postman_collection.json | 68 +++++++++++++++++++ ...v2.0.0-auth.postman_collection.result.json | 21 ++++++ 4 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 packages/postman/tests/fixtures/v2.0.0-auth.postman_collection.json create mode 100644 packages/postman/tests/fixtures/v2.0.0-auth.postman_collection.result.json diff --git a/packages/postman/src/converter/DefaultConverter.ts b/packages/postman/src/converter/DefaultConverter.ts index af4c404b..a558545e 100644 --- a/packages/postman/src/converter/DefaultConverter.ts +++ b/packages/postman/src/converter/DefaultConverter.ts @@ -121,16 +121,12 @@ export class DefaultConverter implements Converter { /* istanbul ignore next */ private authRequest(request: Request, auth: Postman.RequestAuth): void { - const params: Postman.Variable[] | undefined = auth[auth.type]; + const options = this.getAuthOptions(auth); - if (!params) { + if (!options) { return; } - const options = Object.fromEntries( - params.map((val: Postman.Variable) => [val.key, val.value].map(String)) - ); - switch (auth.type) { case 'apikey': this.apiKeyAuth(request, options); @@ -150,6 +146,21 @@ export class DefaultConverter implements Converter { } } + private getAuthOptions( + auth: Postman.RequestAuth + ): Record | undefined { + const params: Postman.Variable[] | Record | undefined = + auth[auth.type]; + + return Array.isArray(params) + ? Object.fromEntries( + params.map((val: Postman.Variable) => + [val.key, val.value].map(String) + ) + ) + : params; + } + /* istanbul ignore next */ private oauth2(request: Request, options: Record): void { if (!options.accessToken || options.tokenType === 'mac') { diff --git a/packages/postman/tests/DefaultConverter.spec.ts b/packages/postman/tests/DefaultConverter.spec.ts index 0739b0f3..f8a12159 100644 --- a/packages/postman/tests/DefaultConverter.spec.ts +++ b/packages/postman/tests/DefaultConverter.spec.ts @@ -20,6 +20,11 @@ describe('DefaultConverter', () => { }); it.each([ + { + inputPath: './fixtures/v2.0.0-auth.postman_collection.json', + expectedPath: './fixtures/v2.0.0-auth.postman_collection.result.json', + label: 'v2.0.0 auth' + }, { inputPath: './fixtures/trailing-slash.postman_collection.json', expectedPath: diff --git a/packages/postman/tests/fixtures/v2.0.0-auth.postman_collection.json b/packages/postman/tests/fixtures/v2.0.0-auth.postman_collection.json new file mode 100644 index 00000000..6b72bcf1 --- /dev/null +++ b/packages/postman/tests/fixtures/v2.0.0-auth.postman_collection.json @@ -0,0 +1,68 @@ +{ + "info": { + "name": "Auth", + "schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json" + }, + "item": [ + { + "name": "users", + "item": [ + { + "name": "Users", + "request": { + "auth": { + "type": "bearer", + "bearer": { + "token": "token-value" + } + }, + "method": "GET", + "header": [ + { + "key": "Accept", + "value": "application/json" + } + ], + "url": "{{baseUrl}}/users" + }, + "response": [ + { + "name": "Success", + "originalRequest": { + "method": "GET", + "header": [ + { + "key": "Accept", + "value": "application/json" + }, + { + "key": "Authorization", + "value": "Bearer ", + "description": "Added as a part of security scheme: bearer" + } + ], + "url": "{{baseUrl}}/user" + }, + "status": "OK", + "code": 200, + "header": [ + { + "key": "Content-Type", + "value": "application/json" + } + ], + "cookie": [], + "body": "{\n \"username\": \"\",\n \"name\": \"\" \n}" + } + ] + } + ] + } + ], + "variable": [ + { + "key": "baseUrl", + "value": "https://example.com/api/v1" + } + ] +} diff --git a/packages/postman/tests/fixtures/v2.0.0-auth.postman_collection.result.json b/packages/postman/tests/fixtures/v2.0.0-auth.postman_collection.result.json new file mode 100644 index 00000000..6f4ec645 --- /dev/null +++ b/packages/postman/tests/fixtures/v2.0.0-auth.postman_collection.result.json @@ -0,0 +1,21 @@ +[ + { + "bodySize": -1, + "cookies": [], + "headers": [ + { + "name": "Accept", + "value": "application/json" + }, + { + "name": "authorization", + "value": "Bearer token-value" + } + ], + "headersSize": -1, + "httpVersion": "HTTP/1.1", + "method": "GET", + "queryString": [], + "url": "https://example.com/api/v1/users" + } +]