Skip to content

Commit de749e3

Browse files
feat(client): add an interface to set insecure mode on alt transport (scaleway#2659)
Signed-off-by: Olivier Cano <[email protected]>
1 parent b7da019 commit de749e3

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

scw/client.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,15 @@ func setInsecureMode(c httpClient) {
556556
logger.Warningf("client: cannot use insecure mode with HTTP client of type %T", c)
557557
return
558558
}
559+
560+
altTransport, ok := standardHTTPClient.Transport.(interface {
561+
SetInsecureTransport()
562+
})
563+
if ok {
564+
altTransport.SetInsecureTransport()
565+
return
566+
}
567+
559568
transportClient, ok := standardHTTPClient.Transport.(*http.Transport)
560569
if !ok {
561570
logger.Warningf("client: cannot use insecure mode with Transport client of type %T", standardHTTPClient.Transport)

scw/client_test.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,19 +200,39 @@ func (fakeHTTPClient) Do(*http.Request) (*http.Response, error) {
200200
return nil, nil
201201
}
202202

203-
func (fakeHTTPClient) RoundTrip(*http.Request) (*http.Response, error) {
203+
type fakeTransport struct{}
204+
205+
func (fakeTransport) RoundTrip(*http.Request) (*http.Response, error) {
206+
return nil, nil
207+
}
208+
209+
type fakeAltTransport struct {
210+
insecure bool
211+
}
212+
213+
func (f *fakeAltTransport) RoundTrip(*http.Request) (*http.Response, error) {
204214
return nil, nil
205215
}
206216

217+
func (f *fakeAltTransport) SetInsecureTransport() {
218+
f.insecure = true
219+
}
220+
207221
// TestSetInsecureMode test if setInsecureMode panic when given custom HTTP client
208222
func TestSetInsecureMode(t *testing.T) {
209223
var buf bytes.Buffer
210224
logger.DefaultLogger.Init(&buf, logger.LogLevelWarning)
211225

212226
// custom Transport client
213227
clientWithFakeTransport := newHTTPClient()
214-
clientWithFakeTransport.Transport = fakeHTTPClient{}
228+
clientWithFakeTransport.Transport = fakeTransport{}
229+
setInsecureMode(clientWithFakeTransport)
230+
231+
// custom Alt transport client
232+
customTransport := &fakeAltTransport{}
233+
clientWithFakeTransport.Transport = customTransport
215234
setInsecureMode(clientWithFakeTransport)
235+
testhelpers.Equals(t, true, customTransport.insecure)
216236

217237
// custom HTTP client
218238
setInsecureMode(fakeHTTPClient{})
@@ -222,7 +242,7 @@ func TestSetInsecureMode(t *testing.T) {
222242
getLogMessage := func(s string) string {
223243
return strings.Join(strings.Split(s, " ")[3:], " ")
224244
}
225-
testhelpers.Equals(t, "client: cannot use insecure mode with Transport client of type scw.fakeHTTPClient", getLogMessage(lines[0]))
245+
testhelpers.Equals(t, "client: cannot use insecure mode with Transport client of type scw.fakeTransport", getLogMessage(lines[0]))
226246
testhelpers.Equals(t, "client: cannot use insecure mode with HTTP client of type scw.fakeHTTPClient", getLogMessage(lines[1]))
227247

228248
logger.DefaultLogger.Init(os.Stderr, logger.LogLevelWarning)

0 commit comments

Comments
 (0)