Code coverage report for src/targets/node/native.js

Statements: 96.55% (28 / 29)      Branches: 83.33% (5 / 6)      Functions: 100% (1 / 1)      Lines: 96.55% (28 / 29)      Ignored: none     

All files » src/targets/node/ » native.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67    1   1 10       10   10               10   10       10   10   10   10   10   10   10 10 10   10   10 10 10 10 10   10   10 6     10   10     1            
'use strict'
 
var util = require('util')
 
module.exports = function (source, options) {
  var opts = util._extend({
    indent: '  '
  }, options)
 
  var code = []
 
  var reqOpts = {
    method: source.method,
    hostname: source.uriObj.hostname,
    port: source.uriObj.port,
    path: source.uriObj.path,
    headers: source.allHeaders
  }
 
  code.push('var http = require("http");')
 
  Iif (!source.postData.text && source.postData.params) {
    code.push('var querystring = require("querystring");')
  }
 
  code.push(null)
 
  code.push(util.format('var options = %s;', JSON.stringify(reqOpts, null, opts.indent)))
 
  code.push(null)
 
  code.push('var req = http.request(options, function (res) {')
 
  code.push(opts.indent + 'var chunks = [];')
 
  code.push(null)
 
  code.push(opts.indent + 'res.on("data", function (chunk) {')
  code.push(opts.indent + opts.indent + 'chunks.push(chunk);')
  code.push(opts.indent + '});')
 
  code.push(null)
 
  code.push(opts.indent + 'res.on("end", function () {')
  code.push(opts.indent + opts.indent + 'var body = Buffer.concat(chunks);')
  code.push(opts.indent + opts.indent + 'console.log(body);')
  code.push(opts.indent + '});')
  code.push('});')
 
  code.push(null)
 
  if (source.postData.text) {
    code.push(util.format('req.write(%s);', JSON.stringify(source.postData.text)))
  }
 
  code.push('req.end();')
 
  return code.join('\n')
}
 
module.exports.info = {
  key: 'native',
  title: 'HTTP',
  link: 'http://nodejs.org/api/http.html#http_http_request_options_callback',
  description: 'Node.js native HTTP interface'
}