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

fix(requestIterator): generated IDs were not URL-safe #515

Merged
merged 1 commit into from
Mar 18, 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
5 changes: 3 additions & 2 deletions lib/requestIterator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const hyperid = require('hyperid')(true)
const hyperid = require('hyperid')
const inherits = require('util').inherits
const requestBuilder = require('./httpRequestBuilder')
const clone = require('lodash.clonedeep')
Expand All @@ -26,6 +26,7 @@ function RequestIterator (opts) {
return new RequestIterator(opts)
}

this.hyperid = hyperid({ urlSafe: true })
this.resetted = false
this.headers = {}
this.initialContext = opts.initialContext || {}
Expand Down Expand Up @@ -108,7 +109,7 @@ RequestIterator.prototype.rebuildRequest = function () {
data = this.requestBuilder(this.currentRequest, this.context)
if (data) {
this.currentRequest.requestBuffer = this.reqDefaults.idReplacement
? Buffer.from(data.toString().replace(/\[<id>\]/g, hyperid()))
? Buffer.from(data.toString().replace(/\[<id>\]/g, this.hyperid()))
: data
} else if (this.currentRequestIndex === 0) {
// when first request fails to build, we can not reset pipeline, or it'll never end
Expand Down
14 changes: 11 additions & 3 deletions test/requestIterator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,11 @@ test('request iterator should not replace any [<id>] tags with generated IDs whe
})

test('request iterator should replace all [<id>] tags with generated IDs when calling move with idReplacement enabled', (t) => {
t.plan(4)
t.plan(6)

function isUrlSafe (string) {
return /^[A-Za-z0-9\-_]+$/.test(string)
}

const opts = server.address()
opts.method = 'POST'
Expand All @@ -212,15 +216,19 @@ test('request iterator should replace all [<id>] tags with generated IDs when ca

const iterator = new RequestIterator(opts)
const first = iterator.currentRequest.requestBuffer.toString().trim()
const firstId = first.split('\n').pop().trim()

t.equal(first.includes('[<id>]'), false, 'One or more [<id>] tags were not replaced')
t.equal(first.slice(-1), '0', 'Generated ID should end with request number')
t.ok(firstId.endsWith('0'), 'Generated ID should end with request number')
t.ok(isUrlSafe(firstId), 'Generated ID should be URL-safe')

iterator.nextRequest()
const second = iterator.currentRequest.requestBuffer.toString().trim()
const secondId = second.split('\n').pop().trim()

t.equal(second.includes('[<id>]'), false, 'One or more [<id>] tags were not replaced')
t.equal(second.slice(-1), '1', 'Generated ID should end with a unique request number')
t.ok(secondId.endsWith('1'), 'Generated ID should end with a unique request number')
t.ok(isUrlSafe(secondId), 'Generated ID should be URL-safe')
})

test('request iterator should invoke onResponse callback when set', (t) => {
Expand Down
Loading