addressing the review

This commit is contained in:
Chan Kang 2022-06-13 21:23:02 -04:00
parent a410921e1d
commit 1da1d61679
2 changed files with 38 additions and 2 deletions

View File

@ -190,8 +190,8 @@ func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader h
for _, proto := range d.TLSClientConfig.NextProtos { for _, proto := range d.TLSClientConfig.NextProtos {
if proto != "http/1.1" { if proto != "http/1.1" {
return nil, nil, fmt.Errorf( return nil, nil, fmt.Errorf(
`protocol %q was given but is not supported; "websocket: protocol %q was given but is not supported; "+
sharing tls.Config with net/http Transport can cause this error`, "sharing tls.Config with net/http Transport can cause this error",
proto, proto,
) )
} }

View File

@ -1098,3 +1098,39 @@ func TestNetDialConnect(t *testing.T) {
} }
} }
} }
func TestNextProtos(t *testing.T) {
ts := httptest.NewUnstartedServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}),
)
ts.EnableHTTP2 = true
ts.StartTLS()
defer ts.Close()
d := Dialer{
TLSClientConfig: ts.Client().Transport.(*http.Transport).TLSClientConfig,
}
r, err := ts.Client().Get(ts.URL)
if err != nil {
t.Fatalf("Get: %v", err)
}
r.Body.Close()
// Asserts that Dialer.TLSClientConfig.NextProtos contains "h2"
// after the Client.Get call from net/http above.
var containsHTTP2 bool = false
for _, proto := range d.TLSClientConfig.NextProtos {
if proto == "h2" {
containsHTTP2 = true
}
}
if !containsHTTP2 {
t.Fatalf("Dialer.TLSClientConfig.NextProtos does not contain \"h2\"")
}
_, _, err = d.Dial(makeWsProto(ts.URL), nil)
if err == nil {
t.Fatalf("Dial succeeded, expect fail ")
}
}