Skip to content

Commit 871bdce

Browse files
author
Montana Flynn
committed
Add tests for options
1 parent d1e6a1e commit 871bdce

2 files changed

Lines changed: 61 additions & 22 deletions

File tree

src/targets/go/native.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ module.exports = function (source, options) {
2727

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

30+
var indent = opts.showBoilerplate ? 1 : 0
31+
3032
var errorCheck = function () {
3133
if (opts.checkErrors) {
32-
code.push(1, 'if err != nil {')
33-
.push(2, 'panic(err)')
34-
.push(1, '}')
34+
code.push(indent, 'if err != nil {')
35+
.push(indent + 1, 'panic(err)')
36+
.push(indent, '}')
3537
}
3638
}
3739

@@ -40,20 +42,20 @@ module.exports = function (source, options) {
4042
code.push('package main')
4143
.blank()
4244
.push('import (')
43-
.push(1, '"fmt"')
45+
.push(indent, '"fmt"')
4446

4547
if (opts.timeout > 0) {
46-
code.push(1, '"time"')
48+
code.push(indent, '"time"')
4749
}
4850

4951
if (source.postData.text) {
50-
code.push(1, '"strings"')
52+
code.push(indent, '"strings"')
5153
}
5254

53-
code.push(1, '"net/http"')
55+
code.push(indent, '"net/http"')
5456

5557
if (opts.printBody) {
56-
code.push(1, '"io/ioutil"')
58+
code.push(indent, '"io/ioutil"')
5759
}
5860

5961
code.push(')')
@@ -66,25 +68,25 @@ module.exports = function (source, options) {
6668
var client
6769
if (opts.timeout > 0) {
6870
client = 'client'
69-
code.push(1, 'client := http.Client{')
70-
.push(2, 'Timeout: time.Duration(%s * time.Second),', opts.timeout)
71-
.push(1, '}')
71+
code.push(indent, 'client := http.Client{')
72+
.push(indent + 1, 'Timeout: time.Duration(%s * time.Second),', opts.timeout)
73+
.push(indent, '}')
7274
.blank()
7375
} else {
7476
client = 'http.DefaultClient'
7577
}
7678

77-
code.push(1, 'url := "%s"', source.fullUrl)
79+
code.push(indent, 'url := "%s"', source.fullUrl)
7880
.blank()
7981

8082
// If we have body content or not create the var and reader or nil
8183
if (source.postData.text) {
82-
code.push(1, 'payload := strings.NewReader(%s)', JSON.stringify(source.postData.text))
84+
code.push(indent, 'payload := strings.NewReader(%s)', JSON.stringify(source.postData.text))
8385
.blank()
84-
.push(1, 'req, %s := http.NewRequest("%s", url, payload)', errorPlaceholder, source.method)
86+
.push(indent, 'req, %s := http.NewRequest("%s", url, payload)', errorPlaceholder, source.method)
8587
.blank()
8688
} else {
87-
code.push(1, 'req, %s := http.NewRequest("%s", url, nil)', errorPlaceholder, source.method)
89+
code.push(indent, 'req, %s := http.NewRequest("%s", url, nil)', errorPlaceholder, source.method)
8890
.blank()
8991
}
9092

@@ -93,30 +95,30 @@ module.exports = function (source, options) {
9395
// Add headers
9496
if (Object.keys(source.allHeaders).length) {
9597
Object.keys(source.allHeaders).forEach(function (key) {
96-
code.push(1, 'req.Header.Add("%s", "%s")', key, source.allHeaders[key])
98+
code.push(indent, 'req.Header.Add("%s", "%s")', key, source.allHeaders[key])
9799
})
98100

99101
code.blank()
100102
}
101103

102104
// Make request
103-
code.push(1, 'res, %s := %s.Do(req)', errorPlaceholder, client)
105+
code.push(indent, 'res, %s := %s.Do(req)', errorPlaceholder, client)
104106
errorCheck()
105107

106108
// Get Body
107109
if (opts.printBody) {
108110
code.blank()
109-
.push(1, 'defer res.Body.Close()')
110-
.push(1, 'body, %s := ioutil.ReadAll(res.Body)', errorPlaceholder)
111+
.push(indent, 'defer res.Body.Close()')
112+
.push(indent, 'body, %s := ioutil.ReadAll(res.Body)', errorPlaceholder)
111113
errorCheck()
112114
}
113115

114116
// Print it
115117
code.blank()
116-
.push(1, 'fmt.Println(res)')
118+
.push(indent, 'fmt.Println(res)')
117119

118120
if (opts.printBody) {
119-
code.push(1, 'fmt.Println(string(body))')
121+
code.push(indent, 'fmt.Println(string(body))')
120122
}
121123

122124
// End main block

test/tests/go/native.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,40 @@
1+
/* global it */
2+
13
'use strict'
24

3-
module.exports = function (snippet, fixtures) {}
5+
require('should')
6+
7+
module.exports = function (HTTPSnippet, fixtures) {
8+
it('should support false boilerplate option', function () {
9+
var result = new HTTPSnippet(fixtures.requests.full).convert('go', 'native', {
10+
showBoilerplate: false
11+
})
12+
13+
result.should.be.a.String
14+
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))')
15+
})
16+
it('should support checkErrors option', function () {
17+
var result = new HTTPSnippet(fixtures.requests.full).convert('go', 'native', {
18+
checkErrors: true
19+
})
20+
21+
result.should.be.a.String
22+
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}')
23+
})
24+
it('should support printBody option', function () {
25+
var result = new HTTPSnippet(fixtures.requests.full).convert('go', 'native', {
26+
printBody: false
27+
})
28+
29+
result.should.be.a.String
30+
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}')
31+
})
32+
it('should support timeout option', function () {
33+
var result = new HTTPSnippet(fixtures.requests.full).convert('go', 'native', {
34+
timeout: 30
35+
})
36+
37+
result.should.be.a.String
38+
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}')
39+
})
40+
}

0 commit comments

Comments
 (0)