Skip to content

Commit ed6a9fc

Browse files
committed
Add support for insecureSkipVerify with Go
1 parent f744394 commit ed6a9fc

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

src/targets/go/native.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ module.exports = function (source, options) {
4747
code.push(indent, '"time"')
4848
}
4949

50+
if (opts.insecureSkipVerify) {
51+
code.push(indent, '"crypto/tls"')
52+
}
53+
5054
if (source.postData.text) {
5155
code.push(indent, '"strings"')
5256
}
@@ -63,14 +67,28 @@ module.exports = function (source, options) {
6367
.blank()
6468
}
6569

70+
// Create an insecure transport for the client
71+
if (opts.insecureSkipVerify) {
72+
code.push(indent, 'insecureTransport := http.DefaultTransport.(*http.Transport).Clone()')
73+
code.push(indent, 'insecureTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}')
74+
}
75+
6676
// Create client
6777
let client
68-
if (opts.timeout > 0) {
78+
if (opts.timeout > 0 || opts.insecureSkipVerify) {
6979
client = 'client'
7080
code.push(indent, 'client := http.Client{')
71-
.push(indent + 1, 'Timeout: time.Duration(%s * time.Second),', opts.timeout)
72-
.push(indent, '}')
73-
.blank()
81+
82+
if (opts.timeout > 0) {
83+
code.push(indent + 1, 'Timeout: time.Duration(%s * time.Second),', opts.timeout)
84+
}
85+
86+
if (opts.insecureSkipVerify) {
87+
code.push(indent + 1, 'Transport: insecureTransport,')
88+
}
89+
90+
code.push(indent, '}')
91+
code.blank()
7492
} else {
7593
client = 'http.DefaultClient'
7694
}

test/targets/go/native.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,51 @@ func main() {
132132
\tfmt.Println(res)
133133
\tfmt.Println(string(body))
134134
135+
}`)
136+
})
137+
138+
it('should support insecureSkipVerify option', function () {
139+
const result = new HTTPSnippet(fixtures.requests.full).convert('go', 'native', {
140+
insecureSkipVerify: true
141+
})
142+
143+
result.should.be.a.String()
144+
result.should.eql(`package main
145+
146+
import (
147+
\t"fmt"
148+
\t"crypto/tls"
149+
\t"strings"
150+
\t"net/http"
151+
\t"io/ioutil"
152+
)
153+
154+
func main() {
155+
156+
\tinsecureTransport := http.DefaultTransport.(*http.Transport).Clone()
157+
\tinsecureTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
158+
\tclient := http.Client{
159+
\t\tTransport: insecureTransport,
160+
\t}
161+
162+
\turl := "http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value"
163+
164+
\tpayload := strings.NewReader("foo=bar")
165+
166+
\treq, _ := http.NewRequest("POST", url, payload)
167+
168+
\treq.Header.Add("cookie", "foo=bar; bar=baz")
169+
\treq.Header.Add("accept", "application/json")
170+
\treq.Header.Add("content-type", "application/x-www-form-urlencoded")
171+
172+
\tres, _ := client.Do(req)
173+
174+
\tdefer res.Body.Close()
175+
\tbody, _ := ioutil.ReadAll(res.Body)
176+
177+
\tfmt.Println(res)
178+
\tfmt.Println(string(body))
179+
135180
}`)
136181
})
137182
}

0 commit comments

Comments
 (0)