Skip to content

Commit 9c24edd

Browse files
committed
refactor with recent changes
1 parent 56bc643 commit 9c24edd

File tree

10 files changed

+104
-57
lines changed

10 files changed

+104
-57
lines changed

src/targets/go/native.js

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
var util = require('util');
44

5-
module.exports = function (options) {
6-
5+
module.exports = function (source, options) {
76
// Let's Go!
87
var code = [];
98

@@ -13,81 +12,66 @@ module.exports = function (options) {
1312
printBody: true
1413
}, options);
1514

16-
// Set some shortcuts
17-
var req = {
18-
url: this.source.fullUrl,
19-
method: this.source.method,
20-
hostname: this.source.uriObj.hostname,
21-
port: this.source.uriObj.port,
22-
path: this.source.uriObj.path,
23-
headers: this.source.headersObj
24-
};
25-
26-
var bodyPresent = this.source.postData && this.source.postData.text;
27-
2815
var errorPlaceholder = opts.checkErrors ? 'err' : '_';
29-
30-
var errorCheck = function() {
16+
17+
var errorCheck = function () {
3118
if (opts.checkErrors) {
3219
code.push('\tif err != nil {');
3320
code.push('\t\tpanic(err)');
3421
code.push('\t}');
3522
}
3623
};
3724

38-
// Create boilerplate
25+
// Create boilerplate
3926
code.push('package main\n');
4027
code.push('import (');
4128
code.push('\t"fmt"');
42-
if (bodyPresent) code.push('\t"strings"');
29+
30+
if (source.postData.text) {
31+
code.push('\t"strings"');
32+
}
33+
4334
code.push('\t"net/http"');
44-
if (opts.printBody) code.push('\t"io/ioutil"');
35+
36+
if (opts.printBody) {
37+
code.push('\t"io/ioutil"');
38+
}
4539
code.push(')\n');
4640

4741
code.push('func main() {');
4842

4943
// Create client
5044
code.push('\tclient := &http.Client{}');
51-
code.push('\turl := "' + req.url + '"');
45+
code.push(util.format('\turl := "%s"', source.fullUrl));
5246

5347
// If we have body content or not create the var and reader or nil
54-
if (bodyPresent) {
55-
code.push('\tpayload := ' + JSON.stringify(this.source.postData.text));
56-
req.body = 'strings.NewReader(payload)';
48+
if (source.postData.text) {
49+
code.push('\tpayload := ' + JSON.stringify(source.postData.text));
50+
code.push(util.format('\treq, ' + errorPlaceholder + ' := http.NewRequest("%s", url, strings.NewReader(payload))', source.method));
5751
} else {
58-
req.body = 'nil';
52+
code.push(util.format('\treq, %s := http.NewRequest("%s", url, nil)', errorPlaceholder, source.method));
5953
}
6054

61-
code.push('\treq, ' + errorPlaceholder + ' := http.NewRequest("' + req.method + '", url, ' + req.body + ')');
6255
errorCheck();
6356

6457
// Add headers
65-
var headersPresent = this.source.headers && this.source.headers.length;
58+
Object.keys(source.allHeaders).map(function (key) {
59+
code.push(util.format('\treq.Header.Add("%s", "%s")', key, source.allHeaders[key]));
60+
});
6661

67-
if (headersPresent) {
68-
for (var header in this.source.headers) {
69-
var key = this.source.headers[header].name;
70-
var val = this.source.headers[header].value;
71-
code.push('\treq.Header.Add("' + key + '", "' + val + '")');
72-
}
73-
}
62+
// Make request
63+
code.push(util.format('\tres, %s := client.Do(req)', errorPlaceholder));
64+
errorCheck();
7465

75-
// Add Cookies
76-
var cookiesPresent = this.source.cookies && this.source.cookies.length;
77-
if (cookiesPresent) {
78-
var cookies = this.source.cookies.map(function (cookie) {
79-
return cookie.name + '=' + cookie.value;
80-
}).join('; ');
81-
code.push('\treq.Header.Add("Cookie", "' + cookies + '")');
66+
// Get Body
67+
if (opts.printBody) {
68+
code.push('\tdefer res.Body.Close()');
8269
}
8370

84-
// Make request
85-
code.push('\tres, ' + errorPlaceholder + ' := client.Do(req)');
86-
errorCheck();
71+
if (opts.printBody) {
72+
code.push(util.format('\tbody, %s := ioutil.ReadAll(res.Body)', errorPlaceholder));
73+
}
8774

88-
// Get Body
89-
if (opts.printBody) code.push('\tdefer res.Body.Close()');
90-
if (opts.printBody) code.push('\tbody, ' + errorPlaceholder + ' := ioutil.ReadAll(res.Body)');
9175
errorCheck();
9276

9377
// Print it
@@ -96,7 +80,7 @@ module.exports = function (options) {
9680
if (opts.printBody) {
9781
code.push('\tfmt.Println(string(body))');
9882
}
99-
83+
10084
// End main block
10185
code.push('}');
10286

test/fixtures/output/go/native/application-form-encoded.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func main() {
1212
url := "http://mockbin.com/har"
1313
payload := "foo=bar&hello=world"
1414
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
15-
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
15+
req.Header.Add("content-type", "application/x-www-form-urlencoded")
1616
res, _ := client.Do(req)
1717
defer res.Body.Close()
1818
body, _ := ioutil.ReadAll(res.Body)

test/fixtures/output/go/native/application-json.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func main() {
1212
url := "http://mockbin.com/har"
1313
payload := "{\"foo\": \"bar\"}"
1414
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
15-
req.Header.Add("Content-Type", "application/json")
15+
req.Header.Add("content-type", "application/json")
1616
res, _ := client.Do(req)
1717
defer res.Body.Close()
1818
body, _ := ioutil.ReadAll(res.Body)

test/fixtures/output/go/native/cookies.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func main() {
1010
client := &http.Client{}
1111
url := "http://mockbin.com/har"
1212
req, _ := http.NewRequest("POST", url, nil)
13-
req.Header.Add("Cookie", "foo=bar; bar=baz")
13+
req.Header.Add("cookie", "foo=bar; bar=baz")
1414
res, _ := client.Do(req)
1515
defer res.Body.Close()
1616
body, _ := ioutil.ReadAll(res.Body)

test/fixtures/output/go/native/full.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import (
99

1010
func main() {
1111
client := &http.Client{}
12-
url := "http://mockbin.com/har?baz=abc&foo=bar&foo=baz"
12+
url := "http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value"
1313
payload := "foo=bar"
1414
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
15-
req.Header.Add("Accept", "application/json")
16-
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
17-
req.Header.Add("Cookie", "foo=bar; bar=baz")
15+
req.Header.Add("cookie", "foo=bar; bar=baz")
16+
req.Header.Add("accept", "application/json")
17+
req.Header.Add("content-type", "application/x-www-form-urlencoded")
1818
res, _ := client.Do(req)
1919
defer res.Body.Close()
2020
body, _ := ioutil.ReadAll(res.Body)

test/fixtures/output/go/native/headers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ func main() {
1010
client := &http.Client{}
1111
url := "http://mockbin.com/har"
1212
req, _ := http.NewRequest("GET", url, nil)
13-
req.Header.Add("Accept", "application/json")
14-
req.Header.Add("X-Foo", "Bar")
13+
req.Header.Add("accept", "application/json")
14+
req.Header.Add("x-foo", "Bar")
1515
res, _ := client.Do(req)
1616
defer res.Body.Close()
1717
body, _ := ioutil.ReadAll(res.Body)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"net/http"
7+
"io/ioutil"
8+
)
9+
10+
func main() {
11+
client := &http.Client{}
12+
url := "http://mockbin.com/har"
13+
payload := "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001--"
14+
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
15+
req.Header.Add("content-type", "multipart/form-data; boundary=---011000010111000001101001")
16+
res, _ := client.Do(req)
17+
defer res.Body.Close()
18+
body, _ := ioutil.ReadAll(res.Body)
19+
fmt.Println(res)
20+
fmt.Println(string(body))
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"net/http"
7+
"io/ioutil"
8+
)
9+
10+
func main() {
11+
client := &http.Client{}
12+
url := "http://mockbin.com/har"
13+
payload := "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--"
14+
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
15+
req.Header.Add("content-type", "multipart/form-data; boundary=---011000010111000001101001")
16+
res, _ := client.Do(req)
17+
defer res.Body.Close()
18+
body, _ := ioutil.ReadAll(res.Body)
19+
fmt.Println(res)
20+
fmt.Println(string(body))
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"net/http"
7+
"io/ioutil"
8+
)
9+
10+
func main() {
11+
client := &http.Client{}
12+
url := "http://mockbin.com/har"
13+
payload := "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--"
14+
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
15+
req.Header.Add("content-type", "multipart/form-data; boundary=---011000010111000001101001")
16+
res, _ := client.Do(req)
17+
defer res.Body.Close()
18+
body, _ := ioutil.ReadAll(res.Body)
19+
fmt.Println(res)
20+
fmt.Println(string(body))
21+
}

test/fixtures/output/go/native/query.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
func main() {
1010
client := &http.Client{}
11-
url := "http://mockbin.com/har?key=value&baz=abc&foo=bar&foo=baz"
11+
url := "http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value"
1212
req, _ := http.NewRequest("GET", url, nil)
1313
res, _ := client.Do(req)
1414
defer res.Body.Close()

0 commit comments

Comments
 (0)