-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpClient_test.go
89 lines (76 loc) · 1.81 KB
/
HttpClient_test.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package httpclient
import (
"golang.org/x/net/http2"
"io"
"net"
"net/http"
"strings"
"testing"
"time"
)
func TestHttp(tt *testing.T) {
c := GetClient(time.Second)
r := c.Get("http://61.135.169.121")
if r.Error != nil || strings.Index(r.String(), "baidu.com") == -1 {
tt.Error("baidu error ", r.Error)
}
}
func TestStream(tt *testing.T) {
c := GetClient(time.Second)
r := c.ManualDo("GET", "http://61.135.169.121", nil)
if r.Error != nil || strings.Index(r.String(), "baidu.com") != -1 {
tt.Error("stream get error ", r.Error)
}
defer r.Response.Body.Close()
buf, err := io.ReadAll(r.Response.Body)
if err != nil || !strings.Contains(string(buf), "baidu.com") {
tt.Error("stream read error ", r.Error)
}
}
func TestH2C(tt *testing.T) {
listener, _ := net.Listen("tcp", ":20080")
startChan := make(chan bool, 1)
go start(listener, startChan)
<-startChan
//time.Sleep(500*time.Millisecond)
c := GetClientH2C(time.Second)
r := c.Get("http://localhost:20080")
if r.Error != nil {
tt.Error("h2c error ", r.Error)
}
if r.String() != "Hello" {
tt.Error("h2c result err ", r.String())
}
_ = listener.Close()
}
func start(l net.Listener, startChan chan bool) {
server := http.Server{}
http2.VerboseLogs = true
server.Addr = ":20080"
s2 := &http2.Server{
IdleTimeout: 1 * time.Minute,
}
_ = http2.ConfigureServer(&server, s2)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("Hello"))
})
startChan <- true
for {
rwc, err := l.Accept()
if err != nil {
continue
}
go s2.ServeConn(rwc, &http2.ServeConnOpts{BaseConfig: &server})
}
}
func getHeaders(headers http.Header) map[string]string {
out := map[string]string{}
for k, v := range headers {
if len(v) > 1 {
out[k] = strings.Join(v, "; ")
} else {
out[k] = v[0]
}
}
return out
}