Skip to content

Instantly share code, notes, and snippets.

@GlenTiki
Created July 1, 2016 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GlenTiki/50be0e30c735b1740d06246268768a8e to your computer and use it in GitHub Desktop.
Save GlenTiki/50be0e30c735b1740d06246268768a8e to your computer and use it in GitHub Desktop.
const inherits = require('util').inherits
const xtend = require('xtend')
const requestBuilder = require('./httpRequestBuilder')
function RequestIterator (requests, defaults) {
this.requests = requests || [{}]
this.requests.forEach((request) => {
request = xtend(requestDefaults, request)
})
this.currentRequestIndex = 0
this.currentRequest = this.requests[0]
this.requestBuilder = requestBuilder(defaults)
}
inherits(RequestIterator, {})
RequestIterator.prototype.nextRequest = function () {
++this.currentRequestIndex
this.currentRequestIndex = this.currentRequestIndex < this.requests.length ? this.currentRequestIndex : 0
this.currentRequest = this.requests[this.currentRequestIndex]
}
RequestIterator.prototype.nextRequestBuffer = function () {
// get next request
this.nextRequest()
return this.currentRequest.requestBuffer
}
RequestIterator.prototype.setRequests = function (newRequests) {
this.currentRequestIndex = 0
this.requests = newRequests || [{}]
this.rebuildRequests()
}
RequestIterator.prototype.rebuildRequests = function () {
this.requests.forEach((request) => {
request.requestBuffer = this.requestBuilder(request)
})
}
RequestIterator.prototype.setHeaders = function (newHeaders) {
this.currentRequest.headers = newHeaders || {}
this.rebuildRequest()
}
RequestIterator.prototype.setBody = function (newBody) {
this.currentRequest.body = newBody || new Buffer(0)
this.rebuildRequest()
}
RequestIterator.prototype.setHeadersAndBody = function (newHeaders, newBody) {
this.currentRequest.headers = newHeaders || {}
this.currentRequest.body = newBody || new Buffer(0)
this.rebuildRequest()
}
RequestIterator.prototype.setRequest = function (newRequest) {
this.currentRequest = xtend(newRequest, requestDefaults)
this.rebuildRequest()
}
RequestIterator.prototype.rebuildRequest = function () {
this.currentRequest.requestBuffer = this.requestBuilder(this.currentRequest)
this.requests[this.currentRequestIndex] = this.currentRequest
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment