Skip to content

Commit 9fddd2c

Browse files
committed
dial: Redirect wss/ws correctly by modifying the http client
Closes #333 Needs a test.
1 parent a94999f commit 9fddd2c

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

Diff for: dial.go

+15
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ func (opts *DialOptions) cloneWithDefaults(ctx context.Context) (context.Context
7070
if o.HTTPHeader == nil {
7171
o.HTTPHeader = http.Header{}
7272
}
73+
newClient := *o.HTTPClient
74+
oldCheckRedirect := o.HTTPClient.CheckRedirect
75+
newClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
76+
switch req.URL.Scheme {
77+
case "ws":
78+
req.URL.Scheme = "http"
79+
case "wss":
80+
req.URL.Scheme = "https"
81+
}
82+
if oldCheckRedirect != nil {
83+
return oldCheckRedirect(req, via)
84+
}
85+
return nil
86+
}
87+
o.HTTPClient = &newClient
7388

7489
return ctx, cancel, &o
7590
}

Diff for: dial_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,9 @@ type roundTripperFunc func(*http.Request) (*http.Response, error)
304304
func (f roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
305305
return f(r)
306306
}
307+
308+
func TestDialRedirect(t *testing.T) {
309+
t.Parallel()
310+
311+
// TODO:
312+
}

0 commit comments

Comments
 (0)