Repository: mcollina/autocannon (js)

Issue:
`idReplacement` not unique
Expected: `[<id>]` to be replaced with a unique id
Actual: `[<id>]` is not unique across requests

I ran the idReplacement sample with this modified handler, which parses the body and prints the email.
```nodejs
function handle(req, res) {
	let data = []
	req.on('data', chunk => {
		data.push(chunk)
	})
	req.on('end', () => {
		console.log(JSON.parse(data).email);
		res.end('hello world');
	})
}
```

I expected to see unique emails, but saw this instead (with `connections: 2, amount: 10`):
```
new-5DznV1MxQ8qpeLJIzpe+8A/0000000000@user.com
new-5DznV1MxQ8qpeLJIzpe+8A/0000000001@user.com
new-5DznV1MxQ8qpeLJIzpe+8A/0000000000@user.com
new-5DznV1MxQ8qpeLJIzpe+8A/0000000001@user.com
new-5DznV1MxQ8qpeLJIzpe+8A/0000000000@user.com
new-5DznV1MxQ8qpeLJIzpe+8A/0000000001@user.com
new-5DznV1MxQ8qpeLJIzpe+8A/0000000000@user.com
new-5DznV1MxQ8qpeLJIzpe+8A/0000000001@user.com
new-5DznV1MxQ8qpeLJIzpe+8A/0000000000@user.com
new-5DznV1MxQ8qpeLJIzpe+8A/0000000001@user.com
```
From what I can tell, the `this.rebuildRequest` is only called on the first request for each connection: https://github.com/mcollina/autocannon/blob/1fea4240163048719e6423bb73214eb653e1fe19/lib/requestIterator.js#L44-L49
Oppose the on each request:
https://github.com/mcollina/autocannon/blob/1fea4240163048719e6423bb73214eb653e1fe19/lib/requestIterator.js#L28-L31

I'm not sure if this is a problem with the example or a bug with the `idReplacement` feature.

The example, as is, doesn't produce unique email addresses. I did notice that adding: `setupRequest: req => req` to the request does generate a unique email address on each request. 
```
new-x//7JAdiRZGiy4cAd5KNvg/0000000000@user.com
new-x//7JAdiRZGiy4cAd5KNvg/0000000001@user.com
new-x//7JAdiRZGiy4cAd5KNvg/0000000002@user.com
new-x//7JAdiRZGiy4cAd5KNvg/0000000003@user.com
new-x//7JAdiRZGiy4cAd5KNvg/0000000004@user.com
new-x//7JAdiRZGiy4cAd5KNvg/0000000005@user.com
new-x//7JAdiRZGiy4cAd5KNvg/0000000006@user.com
new-x//7JAdiRZGiy4cAd5KNvg/0000000007@user.com
new-x//7JAdiRZGiy4cAd5KNvg/0000000008@user.com
new-x//7JAdiRZGiy4cAd5KNvg/0000000009@user.com
```

Is `setupRequest` required to use `idReplacement`?



Pull request:
fix: unique idReplacement on every request
Fixes #266 

Patch:
diff --git a/lib/requestIterator.js b/lib/requestIterator.js
index db972af..f647d19 100644
--- a/lib/requestIterator.js
+++ b/lib/requestIterator.js
@@ -26,7 +26,7 @@ RequestIterator.prototype.nextRequest = function () {
   }
   this.currentRequest = this.requests[this.currentRequestIndex]
   // only builds if it has dynamic setup
-  if (typeof this.currentRequest.setupRequest === 'function') {
+  if (this.reqDefaults.idReplacement || typeof this.currentRequest.setupRequest === 'function') {
     this.rebuildRequest()
   }
   return this.currentRequest


Test patch:
diff --git a/test/requestIterator.test.js b/test/requestIterator.test.js
index 9614f07..e598550 100644
--- a/test/requestIterator.test.js
+++ b/test/requestIterator.test.js
@@ -173,7 +173,7 @@ 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(2)
+  t.plan(4)
 
   const opts = server.address()
   opts.method = 'POST'
@@ -182,10 +182,16 @@ test('request iterator should replace all [<id>] tags with generated IDs when ca
   opts.idReplacement = true
 
   const iterator = new RequestIterator(opts)
-  const result = iterator.currentRequest.requestBuffer.toString().trim()
+  const first = iterator.currentRequest.requestBuffer.toString().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')
+
+  iterator.nextRequest()
+  const second = iterator.currentRequest.requestBuffer.toString().trim()
 
-  t.equal(result.includes('[<id>]'), false, 'One or more [<id>] tags were not replaced')
-  t.equal(result.slice(-1), '0', 'Generated ID should end with request number')
+  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')
 })
 
 test('request iterator should invoke onResponse callback when set', (t) => {