Skip to content

Instantly share code, notes, and snippets.

@GlenTiki
Last active June 30, 2016 15: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/554576994501d5b041007c3667f06aac to your computer and use it in GitHub Desktop.
Save GlenTiki/554576994501d5b041007c3667f06aac to your computer and use it in GitHub Desktop.
// version 1
instance1 = autocannon({
url: 'localhost:3000',
headers: {auth: 'token'},
body: 'some body',
method: 'POST',
customiseRequest: function (client) {
client.setHeaders(...)
client.setBody(...)
client.setHeadersAndBody(...)
}
})
// version 2
instance2 = autocannon({
url: 'localhost:3000',
headers: {auth: 'token'},
requests: [{
body: 'login data or w/e',
headers: {},
method: 'POST',
path: '/login'
}, {
path: '/users/me/settings'
}, {
body: 'updated settings'
path: '/users/me/settings'
method: 'POST',
customiseRequest: function (client) {
client.setHeaders(...)
client.setBody(...)
client.setHeadersAndBody(...)
}
}]
})
// version 3
instance3 = autocannon({
url: 'localhost:3000',
headers: {auth: 'token'},
customiseRequest: function (client) {
client.setHeaders(...)
client.setBody(...)
client.setHeadersAndBody(...)
},
requests: [{
body: 'login data or w/e',
headers: {},
method: 'POST',
path: '/login'
}, {
path: '/users/me/settings'
}, {
body: 'updated settings'
path: '/users/me/settings'
method: 'POST',
customiseRequest: function (client) {
client.setHeaders(...)
client.setBody(...)
client.setHeadersAndBody(...)
}
}]
})
@GlenTiki
Copy link
Author

GlenTiki commented Jun 30, 2016

const methods = [
  'GET',
  'DELETE',
  'POST',
  'PUT'
]

function buildRequest (reqData, defaults) {
  const method = reqData.method || defaults.method || 'GET'
  const path = reqData.path || defaults.path || '/'
  const headers = reqData.headers || defaults.headers || {}
  const body = reqData.body || defaults.body

  let host = reqData.host
  if (!host) {
    const hostname = reqData.hostname || defaults.hostname || 'localhost'
    const port = reqData.port || 80
    host = hostname + ':' + port
  }

  if (methods.indexOf(method) < 0) {
    throw new Error(`${method} HTTP method is not supported`)
  }

  const baseReq = `${method} ${path} HTTP/1.1\r\nHost: ${host}\r\nConnection: keep-alive\r\n`

  let bodyBuf

  if (typeof body === 'string') {
    bodyBuf = new Buffer(body)
  } else if (Buffer.isBuffer(body)) {
    bodyBuf = body
  } else if (body) {
    throw new Error('body must be either a string or a buffer')
  }

  if (bodyBuf) {
    headers['Content-Length'] = '' + bodyBuf.length
  }

  let req = Object.keys(headers)
    .map((key) => `${key}: ${headers[key]}\r\n`)
    .reduce((acc, str) => acc + str, baseReq)

  req = new Buffer(req + '\r\n', 'utf8')

  if (bodyBuf) {
    req = Buffer.concat([req, bodyBuf, new Buffer('\r\n')])
  }

  return req
}

function _noop () {}

module.exports = buildRequest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment