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

Statements: 85.11% (40 / 47)      Branches: 61.11% (11 / 18)      Functions: 100% (3 / 3)      Lines: 85.11% (40 / 47)      Ignored: none     

All files » src/targets/go/ » 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105    1   1   10     10           10   10 30               10 10 10   10       10 6     10   10 10     10   10     10         10     10     10 6 6   4     10     10 11       10 10     10 10 10 10       10   10 10       10   10     1            
'use strict'
 
var util = require('util')
 
module.exports = function (source, options) {
  // Let's Go!
  var code = []
 
  // Define Options
  var opts = util._extend({
    checkErrors: false,
    printBody: true,
    timeout: -1
  }, options)
 
  var errorPlaceholder = opts.checkErrors ? 'err' : '_'
 
  var errorCheck = function () {
    Iif (opts.checkErrors) {
      code.push('\tif err != nil {')
      code.push('\t\tpanic(err)')
      code.push('\t}')
    }
  }
 
  // Create boilerplate
  code.push('package main\n')
  code.push('import (')
  code.push('\t"fmt"')
 
  Iif (opts.timeout > 0) {
    code.push('\t"time"')
  }
 
  if (source.postData.text) {
    code.push('\t"strings"')
  }
 
  code.push('\t"net/http"')
 
  Eif (opts.printBody) {
    code.push('\t"io/ioutil"')
  }
 
  code.push(')\n')
 
  code.push('func main() {')
 
  // Create client
  Iif (opts.timeout > 0) {
    code.push('\tclient := http.Client{')
    code.push(util.format('\t\tTimeout: time.Duration(%s * time.Second),', opts.timeout))
    code.push('\t}')
  } else {
    code.push('\tclient := &http.Client{}')
  }
 
  code.push(util.format('\turl := "%s"', source.fullUrl))
 
  // If we have body content or not create the var and reader or nil
  if (source.postData.text) {
    code.push(util.format('\tpayload := strings.NewReader(%s)', JSON.stringify(source.postData.text)))
    code.push(util.format('\treq, %s := http.NewRequest("%s", url, payload)', errorPlaceholder, source.method))
  } else {
    code.push(util.format('\treq, %s := http.NewRequest("%s", url, nil)', errorPlaceholder, source.method))
  }
 
  errorCheck()
 
  // Add headers
  Object.keys(source.allHeaders).map(function (key) {
    code.push(util.format('\treq.Header.Add("%s", "%s")', key, source.allHeaders[key]))
  })
 
  // Make request
  code.push(util.format('\tres, %s := client.Do(req)', errorPlaceholder))
  errorCheck()
 
  // Get Body
  Eif (opts.printBody) {
    code.push('\tdefer res.Body.Close()')
    code.push(util.format('\tbody, %s := ioutil.ReadAll(res.Body)', errorPlaceholder))
    errorCheck()
  }
 
  // Print it
  code.push('\tfmt.Println(res)')
 
  Eif (opts.printBody) {
    code.push('\tfmt.Println(string(body))')
  }
 
  // End main block
  code.push('}')
 
  return code.join('\n')
}
 
module.exports.info = {
  key: 'native',
  title: 'NewRequest',
  link: 'http://golang.org/pkg/net/http/#NewRequest',
  description: 'Golang HTTP client request'
}