Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

overwriteRequestQueryParams - Handle form encoded array query params #643

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions src/application/overwrites/overwriteRequestQueryParams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,4 +540,125 @@ describe('overwriteRequestQueryParams', () => {
const result = overwriteRequestQueryParams(dto)
expect(result.item.request.url.query).toMatchSnapshot()
})

it('should correctly handle multiple duplicate query params with matching overwrite values', async () => {
const overwriteValues = [
{
key: 'name',
value: 'newName',
overwrite: true
},
{
key: 'ids',
value: 'newId1',
overwrite: true
},
{
key: 'first',
value: 'newFirst',
overwrite: true
},
{
key: 'ids',
value: 'newId2',
overwrite: true
},
{
key: 'ids',
value: 'newId3',
overwrite: true
},
{
key: 'number',
value: 'newNumber1',
overwrite: true
},
{
key: 'number',
value: 'newNumber2',
overwrite: true
},
{
key: 'word',
value: 'newWord',
overwrite: true
}
]

// Set up initial query params
const pmQueryParams = [
{
key: 'name',
value: 'oldName',
disabled: false
},
{
key: 'ids',
value: 'oldId1',
disabled: false
},
{
key: 'ids',
value: 'oldId2',
disabled: false
},
{
key: 'ids',
value: 'oldId3',
disabled: false
},
{
key: 'first',
value: 'oldFirst',
disabled: false
},
{
key: 'last',
value: 'oldLast',
disabled: false
},
{
key: 'number',
value: 'oldNumber1',
disabled: false
},
{
key: 'word',
value: 'oldWord',
disabled: false
},
{
key: 'number',
value: 'oldNumber2',
disabled: false
}
]

// Add the query params to the pmOperation
// pmQueryParams.forEach(param => pmOperation.item.request.url.query.upsert(new QueryParam(param)))
pmQueryParams.forEach(param => {
pmOperation.item.request.url.query.append(new QueryParam(param))
})

const dto = {
overwriteValues,
pmOperation,
oaOperation
}

const result = overwriteRequestQueryParams(dto)

expect(result.item.request.url.query.map(qp => ({ key: qp.key, value: qp.value }))).toEqual([
{ key: 'raw', value: 'true' },
{ key: 'name', value: 'newName' },
{ key: 'ids', value: 'newId1' },
{ key: 'ids', value: 'newId2' },
{ key: 'ids', value: 'newId3' },
{ key: 'first', value: 'newFirst' },
{ key: 'last', value: 'oldLast' }, // unchanged
{ key: 'number', value: 'newNumber1' },
{ key: 'word', value: 'newWord' },
{ key: 'number', value: 'newNumber2' }
])
})
})
66 changes: 58 additions & 8 deletions src/application/overwrites/overwriteRequestQueryParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { PostmanMappedOperation } from '../../postman'
import { QueryParam } from 'postman-collection'
import { parseTpl, hasTpl, matchWildcard } from '../../utils'
import { OverwriteRequestDTO } from './applyOverwrites'
import _ from 'lodash'

/**
* Overwrite Postman request query params with values defined by the portman testsuite
Expand All @@ -13,23 +14,45 @@ export const overwriteRequestQueryParams = (dto: OverwriteRequestDTO): PostmanMa
// Early exit if overwrite values are not defined
if (!(overwriteValues instanceof Array)) return pmOperation

// Early exit if request url query is not defined
// if (!pmOperation.item?.request?.url?.query) return pmOperation

// Get all Postman query params
const queryKeys = pmOperation.item.request.url.query.map(({ key }) => key)

// Detect overwrite query params that do not exist in the Postman collection
const insertNewKeys = overwriteValues.filter(x => !queryKeys.includes(x.key))

// Extract duplicate query params
const duplicateKeys = _(queryKeys)
.countBy()
.pickBy((count: number) => count > 1)
.keys()
.value()

// Initialize counters for tracking
const queryKeyCounters = {}
const overwriteKeyCounters = {}
const duplicateKeyCounters = {}

// Util function to get the count of key
const getKeyCount = (key, counter) => counter[key] || 0

// Util function to increment the key count
const incrementKeyCount = (key, counter) => {
counter[key] = getKeyCount(key, counter) + 1
return counter[key]
}

pmOperation.item.request.url.query.each(pmQueryParam => {
// Increment counter for query param
const queryKeyIndex = incrementKeyCount(pmQueryParam.key, queryKeyCounters)

// Overwrite values for Keys
overwriteValues.forEach(overwriteItem => {
for (const overwriteItem of overwriteValues) {
// Skip keys when no overwrite is defined
if (
!(overwriteItem?.key && pmQueryParam?.key && overwriteItem.key === pmQueryParam.key) &&
!overwriteItem.key.includes('*')
) {
return
continue
}

// Check wildcard match
Expand All @@ -38,19 +61,41 @@ export const overwriteRequestQueryParams = (dto: OverwriteRequestDTO): PostmanMa
pmQueryParam.key &&
!matchWildcard(pmQueryParam.key, overwriteItem.key)
) {
return
continue
}

// Increment counter for overwrite key
const overwriteKeyIndex = incrementKeyCount(overwriteItem.key, overwriteKeyCounters)

const generatedName = parseTpl({
template: overwriteItem.value,
oaOperation: oaOperation,
options: {
casing: globals?.variableCasing
}
})
const overwriteValue =
let overwriteValue =
overwriteItem?.value && hasTpl(overwriteItem.value) ? generatedName : overwriteItem?.value

// Handle duplicated query params
let duplicateFound = false
if (
duplicateKeys.length > 0 &&
duplicateKeys.includes(pmQueryParam.key) &&
overwriteItem.key === pmQueryParam.key &&
queryKeyIndex === overwriteKeyIndex
) {
const duplicateKeyIndex = getKeyCount(overwriteItem.key, duplicateKeyCounters)
const matchingOverwriteItems = overwriteValues.filter(item => item.key === pmQueryParam.key)

const overwriteObj = matchingOverwriteItems[duplicateKeyIndex]
if (overwriteObj.value) {
overwriteValue = overwriteObj.value
incrementKeyCount(overwriteItem.key, duplicateKeyCounters)
duplicateFound = true
}
}

// Test suite - Overwrite/extend query param value
let hasValue = false
if (overwriteValue !== undefined && pmQueryParam?.value) {
Expand Down Expand Up @@ -83,7 +128,12 @@ export const overwriteRequestQueryParams = (dto: OverwriteRequestDTO): PostmanMa

// Set Postman query param
pmOperation.item.request.url.query.upsert(pmQueryParam)
})

// Break the loop once a matching overwrite is applied
if (duplicateFound) {
break
}
}
})

// Test suite - Remove query param
Expand Down
Loading