Skip to content

Commit

Permalink
fix(requestIterator): generated IDs were not URL-safe (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
10xLaCroixDrinker authored Mar 18, 2024
1 parent f1af1c3 commit 23a843d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
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

0 comments on commit 23a843d

Please sign in to comment.