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" + } +]