From d8c13fcda6330884d705029923227cde83b4d442 Mon Sep 17 00:00:00 2001 From: Jamie King Date: Tue, 12 Mar 2024 06:54:24 -0700 Subject: [PATCH] fix(requestIterator): generated IDs were not URL-safe --- lib/requestIterator.js | 5 +++-- test/requestIterator.test.js | 14 +++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/requestIterator.js b/lib/requestIterator.js index 7886e592..5c1d27e0 100644 --- a/lib/requestIterator.js +++ b/lib/requestIterator.js @@ -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') @@ -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 || {} @@ -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(/\[\]/g, hyperid())) + ? Buffer.from(data.toString().replace(/\[\]/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 diff --git a/test/requestIterator.test.js b/test/requestIterator.test.js index 1ca6a72d..07f7a0ef 100644 --- a/test/requestIterator.test.js +++ b/test/requestIterator.test.js @@ -202,7 +202,11 @@ test('request iterator should not replace any [] tags with generated IDs whe }) test('request iterator should replace all [] 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' @@ -212,15 +216,19 @@ test('request iterator should replace all [] 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('[]'), false, 'One or more [] 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('[]'), false, 'One or more [] 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) => {