Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 42 additions & 35 deletions src/targets/go/native.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,106 +19,113 @@ module.exports = function (source, options) {

// Define Options
var opts = util._extend({
showBoilerplate: true,
checkErrors: false,
printBody: true,
timeout: -1
}, options)

var errorPlaceholder = opts.checkErrors ? 'err' : '_'

var indent = opts.showBoilerplate ? 1 : 0

var errorCheck = function () {
if (opts.checkErrors) {
code.push(1, 'if err != nil {')
.push(2, 'panic(err)')
.push(1, '}')
code.push(indent, 'if err != nil {')
.push(indent + 1, 'panic(err)')
.push(indent, '}')
}
}

// Create boilerplate
code.push('package main')
if (opts.showBoilerplate) {
code.push('package main')
.blank()
.push('import (')
.push(1, '"fmt"')
.push(indent, '"fmt"')

if (opts.timeout > 0) {
code.push(1, '"time"')
}
if (opts.timeout > 0) {
code.push(indent, '"time"')
}

if (source.postData.text) {
code.push(1, '"strings"')
}
if (source.postData.text) {
code.push(indent, '"strings"')
}

code.push(1, '"net/http"')
code.push(indent, '"net/http"')

if (opts.printBody) {
code.push(1, '"io/ioutil"')
}
if (opts.printBody) {
code.push(indent, '"io/ioutil"')
}

code.push(')')
code.push(')')
.blank()
.push('func main() {')
.blank()
}

// Create client
var client
if (opts.timeout > 0) {
client = 'client'
code.push(1, 'client := http.Client{')
.push(2, 'Timeout: time.Duration(%s * time.Second),', opts.timeout)
.push(1, '}')
.blank()
code.push(indent, 'client := http.Client{')
.push(indent + 1, 'Timeout: time.Duration(%s * time.Second),', opts.timeout)
.push(indent, '}')
.blank()
} else {
client = 'http.DefaultClient'
}

code.push(1, 'url := "%s"', source.fullUrl)
.blank()
code.push(indent, 'url := "%s"', source.fullUrl)
.blank()

// If we have body content or not create the var and reader or nil
if (source.postData.text) {
code.push(1, 'payload := strings.NewReader(%s)', JSON.stringify(source.postData.text))
.blank()
.push(1, 'req, %s := http.NewRequest("%s", url, payload)', errorPlaceholder, source.method)
.blank()
code.push(indent, 'payload := strings.NewReader(%s)', JSON.stringify(source.postData.text))
.blank()
.push(indent, 'req, %s := http.NewRequest("%s", url, payload)', errorPlaceholder, source.method)
.blank()
} else {
code.push(1, 'req, %s := http.NewRequest("%s", url, nil)', errorPlaceholder, source.method)
.blank()
code.push(indent, 'req, %s := http.NewRequest("%s", url, nil)', errorPlaceholder, source.method)
.blank()
}

errorCheck()

// Add headers
if (Object.keys(source.allHeaders).length) {
Object.keys(source.allHeaders).forEach(function (key) {
code.push(1, 'req.Header.Add("%s", "%s")', key, source.allHeaders[key])
code.push(indent, 'req.Header.Add("%s", "%s")', key, source.allHeaders[key])
})

code.blank()
}

// Make request
code.push(1, 'res, %s := %s.Do(req)', errorPlaceholder, client)
code.push(indent, 'res, %s := %s.Do(req)', errorPlaceholder, client)
errorCheck()

// Get Body
if (opts.printBody) {
code.blank()
.push(1, 'defer res.Body.Close()')
.push(1, 'body, %s := ioutil.ReadAll(res.Body)', errorPlaceholder)
.push(indent, 'defer res.Body.Close()')
.push(indent, 'body, %s := ioutil.ReadAll(res.Body)', errorPlaceholder)
errorCheck()
}

// Print it
code.blank()
.push(1, 'fmt.Println(res)')
.push(indent, 'fmt.Println(res)')

if (opts.printBody) {
code.push(1, 'fmt.Println(string(body))')
code.push(indent, 'fmt.Println(string(body))')
}

// End main block
code.blank()
if (opts.showBoilerplate) {
code.blank()
.push('}')
}

return code.join()
}
Expand Down
39 changes: 38 additions & 1 deletion test/tests/go/native.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
/* global it */

'use strict'

module.exports = function (snippet, fixtures) {}
require('should')

module.exports = function (HTTPSnippet, fixtures) {
it('should support false boilerplate option', function () {
var result = new HTTPSnippet(fixtures.requests.full).convert('go', 'native', {
showBoilerplate: false
})

result.should.be.a.String
result.should.eql('url := \"http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value\"\n\npayload := strings.NewReader(\"foo=bar\")\n\nreq, _ := http.NewRequest(\"POST\", url, payload)\n\nreq.Header.Add(\"cookie\", \"foo=bar; bar=baz\")\nreq.Header.Add(\"accept\", \"application/json\")\nreq.Header.Add(\"content-type\", \"application/x-www-form-urlencoded\")\n\nres, _ := http.DefaultClient.Do(req)\n\ndefer res.Body.Close()\nbody, _ := ioutil.ReadAll(res.Body)\n\nfmt.Println(res)\nfmt.Println(string(body))')
})
it('should support checkErrors option', function () {
var result = new HTTPSnippet(fixtures.requests.full).convert('go', 'native', {
checkErrors: true
})

result.should.be.a.String
result.should.eql('package main\n\nimport (\n\t\"fmt\"\n\t\"strings\"\n\t\"net/http\"\n\t\"io/ioutil\"\n)\n\nfunc main() {\n\n\turl := \"http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value\"\n\n\tpayload := strings.NewReader(\"foo=bar\")\n\n\treq, err := http.NewRequest(\"POST\", url, payload)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treq.Header.Add(\"cookie\", \"foo=bar; bar=baz\")\n\treq.Header.Add(\"accept\", \"application/json\")\n\treq.Header.Add(\"content-type\", \"application/x-www-form-urlencoded\")\n\n\tres, err := http.DefaultClient.Do(req)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tdefer res.Body.Close()\n\tbody, err := ioutil.ReadAll(res.Body)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res)\n\tfmt.Println(string(body))\n\n}')
})
it('should support printBody option', function () {
var result = new HTTPSnippet(fixtures.requests.full).convert('go', 'native', {
printBody: false
})

result.should.be.a.String
result.should.eql('package main\n\nimport (\n\t\"fmt\"\n\t\"strings\"\n\t\"net/http\"\n)\n\nfunc main() {\n\n\turl := \"http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value\"\n\n\tpayload := strings.NewReader(\"foo=bar\")\n\n\treq, _ := http.NewRequest(\"POST\", url, payload)\n\n\treq.Header.Add(\"cookie\", \"foo=bar; bar=baz\")\n\treq.Header.Add(\"accept\", \"application/json\")\n\treq.Header.Add(\"content-type\", \"application/x-www-form-urlencoded\")\n\n\tres, _ := http.DefaultClient.Do(req)\n\n\tfmt.Println(res)\n\n}')
})
it('should support timeout option', function () {
var result = new HTTPSnippet(fixtures.requests.full).convert('go', 'native', {
timeout: 30
})

result.should.be.a.String
result.should.eql('package main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\t\"strings\"\n\t\"net/http\"\n\t\"io/ioutil\"\n)\n\nfunc main() {\n\n\tclient := http.Client{\n\t\tTimeout: time.Duration(30 * time.Second),\n\t}\n\n\turl := \"http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value\"\n\n\tpayload := strings.NewReader(\"foo=bar\")\n\n\treq, _ := http.NewRequest(\"POST\", url, payload)\n\n\treq.Header.Add(\"cookie\", \"foo=bar; bar=baz\")\n\treq.Header.Add(\"accept\", \"application/json\")\n\treq.Header.Add(\"content-type\", \"application/x-www-form-urlencoded\")\n\n\tres, _ := client.Do(req)\n\n\tdefer res.Body.Close()\n\tbody, _ := ioutil.ReadAll(res.Body)\n\n\tfmt.Println(res)\n\tfmt.Println(string(body))\n\n}')
})
}