-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclientconn_test.go
More file actions
63 lines (53 loc) · 1.71 KB
/
clientconn_test.go
File metadata and controls
63 lines (53 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2025 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package http_test
import (
"context"
"fmt"
"io"
"testing"
"github.com/metacubex/http"
)
func TestTransportNewClientConnRoundTrip(t *testing.T) { run(t, testTransportNewClientConnRoundTrip) }
func testTransportNewClientConnRoundTrip(t *testing.T, mode testMode) {
cst := newClientServerTest(t, mode, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, req.Host)
}), optFakeNet)
scheme := mode.Scheme() // http or https
cc, err := cst.tr.NewClientConn(context.Background(), scheme, cst.ts.Listener.Addr().String())
if err != nil {
t.Fatal(err)
}
defer cc.Close()
// Send requests for a couple different domains.
// All use the same connection.
for _, host := range []string{"example.tld", "go.dev"} {
req, _ := http.NewRequest("GET", fmt.Sprintf("%v://%v/", scheme, host), nil)
resp, err := cc.RoundTrip(req)
if err != nil {
t.Fatal(err)
}
got, _ := io.ReadAll(resp.Body)
if string(got) != host {
t.Errorf("got response body %q, want %v", got, host)
}
resp.Body.Close()
// CloseIdleConnections does not close connections created by NewClientConn.
cst.tr.CloseIdleConnections()
}
if err := cc.Err(); err != nil {
t.Errorf("before close: ClientConn.Err() = %v, want nil", err)
}
cc.Close()
if err := cc.Err(); err == nil {
t.Errorf("after close: ClientConn.Err() = nil, want error")
}
req, _ := http.NewRequest("GET", scheme+"://example.tld/", nil)
resp, err := cc.RoundTrip(req)
if err == nil {
resp.Body.Close()
t.Errorf("after close: cc.RoundTrip succeeded, want error")
}
t.Log(err)
}