Skip to content

Commit 45a0e36

Browse files
author
Montana Flynn
committed
Use http.DefaultClient if no timeout option is specified
1 parent 12b6a8a commit 45a0e36

11 files changed

Lines changed: 14 additions & 32 deletions

File tree

‎src/targets/go/native.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ module.exports = function (source, options) {
5757
code.push('func main() {\n')
5858

5959
// Create client
60+
var client
6061
if (opts.timeout > 0) {
62+
client = 'client'
6163
code.push('\tclient := http.Client{')
6264
code.push(util.format('\t\tTimeout: time.Duration(%s * time.Second),', opts.timeout))
6365
code.push('\t}\n')
6466
} else {
65-
code.push('\tclient := &http.Client{}\n')
67+
client = 'http.DefaultClient'
6668
}
6769

6870
code.push(util.format('\turl := "%s"\n', source.fullUrl))
@@ -86,7 +88,7 @@ module.exports = function (source, options) {
8688
}
8789

8890
// Make request
89-
code.push(util.format('\tres, %s := client.Do(req)', errorPlaceholder))
91+
code.push(util.format('\tres, %s := %s.Do(req)', errorPlaceholder, client))
9092
errorCheck()
9193

9294
// Get Body

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99

1010
func main() {
1111

12-
client := &http.Client{}
13-
1412
url := "http://mockbin.com/har"
1513

1614
payload := strings.NewReader("foo=bar&hello=world")
@@ -19,7 +17,7 @@ func main() {
1917

2018
req.Header.Add("content-type", "application/x-www-form-urlencoded")
2119

22-
res, _ := client.Do(req)
20+
res, _ := http.DefaultClient.Do(req)
2321

2422
defer res.Body.Close()
2523
body, _ := ioutil.ReadAll(res.Body)

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99

1010
func main() {
1111

12-
client := &http.Client{}
13-
1412
url := "http://mockbin.com/har"
1513

1614
payload := strings.NewReader("{\"number\": 1, \"string\": \"f\\\"oo\", \"arr\": [1, 2, 3], \"nested\": {\"a\": \"b\"}, \"arr_mix\": [1, \"a\", {\"arr_mix_nested\": {}}] }")
@@ -19,7 +17,7 @@ func main() {
1917

2018
req.Header.Add("content-type", "application/json")
2119

22-
res, _ := client.Do(req)
20+
res, _ := http.DefaultClient.Do(req)
2321

2422
defer res.Body.Close()
2523
body, _ := ioutil.ReadAll(res.Body)

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ import (
88

99
func main() {
1010

11-
client := &http.Client{}
12-
1311
url := "http://mockbin.com/har"
1412

1513
req, _ := http.NewRequest("POST", url, nil)
1614

1715
req.Header.Add("cookie", "foo=bar; bar=baz")
1816

19-
res, _ := client.Do(req)
17+
res, _ := http.DefaultClient.Do(req)
2018

2119
defer res.Body.Close()
2220
body, _ := ioutil.ReadAll(res.Body)

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99

1010
func main() {
1111

12-
client := &http.Client{}
13-
1412
url := "http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value"
1513

1614
payload := strings.NewReader("foo=bar")
@@ -21,7 +19,7 @@ func main() {
2119
req.Header.Add("accept", "application/json")
2220
req.Header.Add("content-type", "application/x-www-form-urlencoded")
2321

24-
res, _ := client.Do(req)
22+
res, _ := http.DefaultClient.Do(req)
2523

2624
defer res.Body.Close()
2725
body, _ := ioutil.ReadAll(res.Body)

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@ import (
88

99
func main() {
1010

11-
client := &http.Client{}
12-
1311
url := "http://mockbin.com/har"
1412

1513
req, _ := http.NewRequest("GET", url, nil)
1614

1715
req.Header.Add("accept", "application/json")
1816
req.Header.Add("x-foo", "Bar")
1917

20-
res, _ := client.Do(req)
18+
res, _ := http.DefaultClient.Do(req)
2119

2220
defer res.Body.Close()
2321
body, _ := ioutil.ReadAll(res.Body)

‎test/fixtures/output/go/native/multipart-data.go‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99

1010
func main() {
1111

12-
client := &http.Client{}
13-
1412
url := "http://mockbin.com/har"
1513

1614
payload := strings.NewReader("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001--")
@@ -19,7 +17,7 @@ func main() {
1917

2018
req.Header.Add("content-type", "multipart/form-data; boundary=---011000010111000001101001")
2119

22-
res, _ := client.Do(req)
20+
res, _ := http.DefaultClient.Do(req)
2321

2422
defer res.Body.Close()
2523
body, _ := ioutil.ReadAll(res.Body)

‎test/fixtures/output/go/native/multipart-file.go‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99

1010
func main() {
1111

12-
client := &http.Client{}
13-
1412
url := "http://mockbin.com/har"
1513

1614
payload := strings.NewReader("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n-----011000010111000001101001--")
@@ -19,7 +17,7 @@ func main() {
1917

2018
req.Header.Add("content-type", "multipart/form-data; boundary=---011000010111000001101001")
2119

22-
res, _ := client.Do(req)
20+
res, _ := http.DefaultClient.Do(req)
2321

2422
defer res.Body.Close()
2523
body, _ := ioutil.ReadAll(res.Body)

‎test/fixtures/output/go/native/multipart-form-data.go‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99

1010
func main() {
1111

12-
client := &http.Client{}
13-
1412
url := "http://mockbin.com/har"
1513

1614
payload := strings.NewReader("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nbar\r\n-----011000010111000001101001--")
@@ -19,7 +17,7 @@ func main() {
1917

2018
req.Header.Add("content-type", "multipart/form-data; boundary=---011000010111000001101001")
2119

22-
res, _ := client.Do(req)
20+
res, _ := http.DefaultClient.Do(req)
2321

2422
defer res.Body.Close()
2523
body, _ := ioutil.ReadAll(res.Body)

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ import (
88

99
func main() {
1010

11-
client := &http.Client{}
12-
1311
url := "http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value"
1412

1513
req, _ := http.NewRequest("GET", url, nil)
1614

17-
res, _ := client.Do(req)
15+
res, _ := http.DefaultClient.Do(req)
1816

1917
defer res.Body.Close()
2018
body, _ := ioutil.ReadAll(res.Body)

0 commit comments

Comments
 (0)