-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathh2c_test.go
65 lines (52 loc) · 1.04 KB
/
h2c_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
package h2c
import (
"crypto/tls"
"fmt"
"net"
"net/http"
"testing"
"golang.org/x/net/http2"
)
func TestHandlerH2CServeHTTP(t *testing.T) {
t.Parallel()
startServer()
client := http.Client{
Transport: &http2.Transport{
AllowHTTP: true,
DialTLS: func(netw, addr string, cfg *tls.Config) (net.Conn, error) {
return net.Dial(netw, addr)
},
},
}
resp, err := client.Get("http://127.0.0.1:8080/")
if err != nil {
t.Fatal(err)
}
if resp.ProtoMajor != 2 {
t.Errorf("Expected protocol version: %d; Got: %d", 2, resp.ProtoMajor)
}
}
func startServer() {
router := http.NewServeMux()
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello World")
})
h2cWrapper := &HandlerH2C{
Handler: router,
H2Server: &http2.Server{},
}
srv := http.Server{
Addr: ":8080",
Handler: h2cWrapper,
}
go func() {
srv.ListenAndServe()
}()
// Wait for it to start
for {
resp, _ := http.Get("http://127.0.0.1:8080/")
if resp != nil && resp.StatusCode == http.StatusOK {
break
}
}
}