Skip to content

Commit

Permalink
Merge pull request #100 from grafana/fix/decode-uri-idempotent
Browse files Browse the repository at this point in the history
[fix] Make normalization idempotent
  • Loading branch information
e-fisher authored Feb 23, 2022
2 parents 82f395f + a10b500 commit bd31279
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/normalize/entries/decodePostDataParams.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ function getDecodedPostDataParamEntries(entries) {

const mimeType = getContentTypeValue(entry.request.postData.mimeType)

if (mimeType !== 'application/x-www-form-urlencoded') {
if (
mimeType !== 'application/x-www-form-urlencoded' ||
entry.request.postData.decoded
) {
return entry
}

Expand All @@ -33,6 +36,7 @@ function getDecodedPostDataParamEntries(entries) {
})

entry.request.postData.params = postDataParams
entry.request.postData.decoded = true
return entry
})
}
Expand Down
34 changes: 28 additions & 6 deletions test/unit/normalize/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ class MockArchive {
}
}

test('falsy archive', (t) => {
test('falsy archive', t => {
// Unmodified archive is returned
t.is(normalize(-1), -1)
})

test('falsy archive.log', (t) => {
test('falsy archive.log', t => {
const invalid = { test: true }
// Unmodified archive is returned
t.is(normalize(invalid), invalid)
})

test('falsy archive.log.pages', (t) => {
test('falsy archive.log.pages', t => {
const archive = new MockArchive()
.addEntry({
id: 'last',
Expand All @@ -57,13 +57,13 @@ test('falsy archive.log.pages', (t) => {
t.notDeepEqual(result, archive)
})

test('falsy archive.log.entries', (t) => {
test('falsy archive.log.entries', t => {
const invalid = { log: { pages: [] } }
// Unmodified archive is returned
t.is(normalize(invalid), invalid)
})

test('entries are sorted', (t) => {
test('entries are sorted', t => {
const archive = new MockArchive()
.addEntry({
id: 'last',
Expand All @@ -83,7 +83,7 @@ test('entries are sorted', (t) => {
t.is(result.log.entries[2].id, 'last')
})

test('option.addSleep=true', (t) => {
test('option.addSleep=true', t => {
const archive = new MockArchive()
.addPage({ id: 'page_2' })
.addPage({ id: 'page_1' })
Expand Down Expand Up @@ -114,3 +114,25 @@ test('option.addSleep=true', (t) => {
t.deepEqual(result.log.entries[2].sleep, [{ [SleepPlacement.After]: 500 }]) // rounded
t.deepEqual(result.log.entries[3].sleep, undefined) // last entry has no sleep
})

test('x-www-form-urlencoded values are decoded once', t => {
const archive = new MockArchive().addEntry({
request: {
postData: {
mimeType: 'application/x-www-form-urlencoded',
params: [
{
name: 'name',
value: '80%25',
},
],
},
},
}).archive

const normalized = normalize(archive)
const normalizedTwice = normalize(normalized)

t.is(normalized.log.entries[0].request.postData.params[0].value, '80%')
t.is(normalizedTwice.log.entries[0].request.postData.params[0].value, '80%')
})
1 change: 1 addition & 0 deletions typings/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ declare module 'har-to-k6' {
export interface PostData extends Body {
params: QueryParameter[]
comment?: string
decoded?: boolean
}

export interface URLEncodedParameter {
Expand Down

0 comments on commit bd31279

Please sign in to comment.