Skip to content

Commit 693fac9

Browse files
authored
Merge pull request #365 from guseggert/master
Fix dial panic when ctx is nil
2 parents 7ce08e9 + 54809d6 commit 693fac9

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

dial.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ func handshakeRequest(ctx context.Context, urls string, opts *DialOptions, copts
164164
return nil, fmt.Errorf("unexpected url scheme: %q", u.Scheme)
165165
}
166166

167-
req, _ := http.NewRequestWithContext(ctx, "GET", u.String(), nil)
167+
req, err := http.NewRequestWithContext(ctx, "GET", u.String(), nil)
168+
if err != nil {
169+
return nil, fmt.Errorf("http.NewRequestWithContext failed: %w", err)
170+
}
168171
req.Header = opts.HTTPHeader.Clone()
169172
req.Header.Set("Connection", "Upgrade")
170173
req.Header.Set("Upgrade", "websocket")

dial_test.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ func TestBadDials(t *testing.T) {
2323
t.Parallel()
2424

2525
testCases := []struct {
26-
name string
27-
url string
28-
opts *DialOptions
29-
rand readerFunc
26+
name string
27+
url string
28+
opts *DialOptions
29+
rand readerFunc
30+
nilCtx bool
3031
}{
3132
{
3233
name: "badURL",
@@ -46,15 +47,24 @@ func TestBadDials(t *testing.T) {
4647
return 0, io.EOF
4748
},
4849
},
50+
{
51+
name: "nilContext",
52+
url: "http://localhost",
53+
nilCtx: true,
54+
},
4955
}
5056

5157
for _, tc := range testCases {
5258
tc := tc
5359
t.Run(tc.name, func(t *testing.T) {
5460
t.Parallel()
5561

56-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
57-
defer cancel()
62+
var ctx context.Context
63+
var cancel func()
64+
if !tc.nilCtx {
65+
ctx, cancel = context.WithTimeout(context.Background(), time.Second*5)
66+
defer cancel()
67+
}
5868

5969
if tc.rand == nil {
6070
tc.rand = rand.Reader.Read

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy