Skip to content

Commit

Permalink
Allow for ID replacement in the URL (#521)
Browse files Browse the repository at this point in the history
* Allow for ID replacement in the URL

* Add tests for [<id>] in path
  • Loading branch information
MattIPv4 committed Apr 17, 2024
1 parent 41e3a18 commit bee21f8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
6 changes: 5 additions & 1 deletion lib/requestIterator.js
Expand Up @@ -108,8 +108,12 @@ RequestIterator.prototype.rebuildRequest = function () {
this.currentRequest.headers = this.currentRequest.headers || this.headers
data = this.requestBuilder(this.currentRequest, this.context)
if (data) {
const hyperid = this.hyperid()
this.currentRequest.requestBuffer = this.reqDefaults.idReplacement
? Buffer.from(data.toString().replace(/\[<id>\]/g, this.hyperid()))
? Buffer.from(data.toString()
.replace(/\[<id>\]/g, hyperid)
// in the first line only (the url), replace encoded id placeholders
.replace(/^.+/, m => m.replace(/\[%3Cid%3E]/g, 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
25 changes: 18 additions & 7 deletions test/requestIterator.test.js
Expand Up @@ -184,16 +184,20 @@ test('request iterator should allow for rebuilding the current request', (t) =>
})

test('request iterator should not replace any [<id>] tags with generated IDs when calling move with idReplacement disabled', (t) => {
t.plan(2)
t.plan(3)

const opts = server.address()
opts.path = '/[%3Cid%3E]'
opts.method = 'POST'
opts.body = '[<id>]'
opts.requests = [{}]

const iterator = new RequestIterator(opts)
const result = iterator.currentRequest.requestBuffer.toString().trim()

const path = result.split(' ')[1]
t.equal(path, '/[%3Cid%3E]', '[<id>] should be present in path')

const contentLength = result.split('Content-Length: ')[1].slice(0, 1)
t.equal(contentLength, '6', 'Content-Length was incorrect')

Expand All @@ -202,31 +206,38 @@ 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(6)
t.plan(10)

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

const opts = server.address()
opts.method = 'POST'
opts.body = '[<id>]'
opts.path = '/[%3Cid%3E]'
opts.body = '[<id>] [%3Cid%3E]'
opts.requests = [{}]
opts.idReplacement = true

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

t.equal(first.includes('[<id>]'), false, 'One or more [<id>] tags were not replaced')
t.equal(first.includes('[<id>]'), false, 'One or more [<id>] tags were not replaced in body')
t.ok(first.includes('[%3Cid%3E]'), 'Encoded [<id>] tags should not be replaced in body')
t.equal(firstPath.includes('[%3Cid%3E]'), false, 'One or more [<id>] tags were not replaced in path')
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()
const secondPath = second.split(' ')[1]
const secondId = secondPath.slice(1)

t.equal(second.includes('[<id>]'), false, 'One or more [<id>] tags were not replaced')
t.equal(second.includes('[<id>]'), false, 'One or more [<id>] tags were not replaced in body')
t.ok(second.includes('[%3Cid%3E]'), 'Encoded [<id>] tags should not be replaced in body')
t.equal(secondPath.includes('[%3Cid%3E]'), false, 'One or more [<id>] tags were not replaced in path')
t.ok(secondId.endsWith('1'), 'Generated ID should end with a unique request number')
t.ok(isUrlSafe(secondId), 'Generated ID should be URL-safe')
})
Expand Down

0 comments on commit bee21f8

Please sign in to comment.