Skip to content

Commit

Permalink
fix: logic to handle different postman versions
Browse files Browse the repository at this point in the history
  • Loading branch information
kobenguyent committed Sep 9, 2024
1 parent 8c4fd49 commit 1193f48
Show file tree
Hide file tree
Showing 7 changed files with 2,600 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/ui/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1732,9 +1732,10 @@ export function covertPostmanAuthToRestfoxAuth(request: any) {

if ('auth' in request) {
if(request.auth.type === 'bearer' && request.auth.bearer) {
const token = Array.isArray(request.auth.bearer) ? request.auth.bearer.find((item: any) => item.key === 'token')?.value || '' : request.auth.bearer.token
authentication = {
type: 'bearer',
token: request.auth.bearer.token
token
}
} else if(request.auth.type === 'basic' && request.auth.basic) {
const username = request.auth.basic.find((item: any) => item.key === 'username')?.value || ''
Expand Down
46 changes: 44 additions & 2 deletions packages/ui/src/parsers/postman.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import path from 'node:path'
import { convertPostmanExportToRestfoxCollection } from './postman'
import { addSortOrderToTree, flattenTree } from '@/helpers'

test('importPostmanV2', async() => {
test('importPostmanV2.0', async() => {
const currentFolder = path.dirname(new URL(import.meta.url).pathname)

const testDataFolder = path.join(currentFolder, '..', '..', 'test-data', 'postman-import-v2')

const inputFile = await readFile(path.join(testDataFolder, 'Argos.API.postman_collection.json'), 'utf-8')
const inputFile = await readFile(path.join(testDataFolder, 'Argos.API.postman_collection.v2.0.json'), 'utf-8')
const input = JSON.parse(inputFile)
const outputFile = await readFile(path.join(testDataFolder, 'Restfox_2024-09-06.json'), 'utf-8')
const expected = JSON.parse(outputFile)
Expand Down Expand Up @@ -45,3 +45,45 @@ test('importPostmanV2', async() => {

expect(collection).toEqual(expectedCollection)
})

test('importPostmanV2.1', async() => {
const currentFolder = path.dirname(new URL(import.meta.url).pathname)

const testDataFolder = path.join(currentFolder, '..', '..', 'test-data', 'postman-import-v2')

const inputFile = await readFile(path.join(testDataFolder, 'Argos.API.postman_collection.v2.1.json'), 'utf-8')
const input = JSON.parse(inputFile)
const outputFile = await readFile(path.join(testDataFolder, 'Restfox_2024-09-06.v2.1.json'), 'utf-8')
const expected = JSON.parse(outputFile)

const converted: any = await convertPostmanExportToRestfoxCollection(input, false, expected.collection[0].workspaceId)

const collectionTree = converted.collection
addSortOrderToTree(collectionTree)
const collection: any[] = JSON.parse(JSON.stringify(flattenTree(collectionTree)))

collection.sort((a, b) => {
return a.name.localeCompare(b.name)
})

collection.forEach((item) => {
item.plugins = []
delete item._id
delete item.parentId
})

const expectedCollection: any[] = expected.collection

expectedCollection.sort((a, b) => {
return a.name.localeCompare(b.name)
})

expectedCollection.forEach((item) => {
delete item._id
delete item.parentId
})

writeFile(path.join(testDataFolder, 'test-snapshot.v2.1.json'), JSON.stringify(collection, null, 2), 'utf-8')

expect(collection).toEqual(expectedCollection)
})
Loading

0 comments on commit 1193f48

Please sign in to comment.