-
Notifications
You must be signed in to change notification settings - Fork 403
Expand file tree
/
Copy pathskipper_h2c_test.go
More file actions
107 lines (94 loc) · 2.65 KB
/
Copy pathskipper_h2c_test.go
File metadata and controls
107 lines (94 loc) · 2.65 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package skipper
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"sync"
"syscall"
"testing"
"time"
"github.com/zalando/skipper/dataclients/routestring"
"github.com/zalando/skipper/filters/builtin"
"github.com/zalando/skipper/metrics/metricstest"
"github.com/zalando/skipper/proxy"
"github.com/zalando/skipper/routing"
)
// TestH2cEndToEnd_BackendsAndClient verifies end-to-end h2c: an h2c-capable
// backend is reached through a skipper proxy that has EnableH2cServer set.
// Both legs are checked: client→proxy (h2c) and proxy→backend (h2c).
func TestH2cEndToEnd_BackendsAndClient(t *testing.T) {
if !testListener() {
t.Skip("skipping listener test; pass -args listener to enable")
}
var mu sync.Mutex
var proto string
backend := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
proto = r.Proto
mu.Unlock()
w.WriteHeader(http.StatusOK)
}))
backend.Config.Protocols = new(http.Protocols)
backend.Config.Protocols.SetHTTP1(true)
backend.Config.Protocols.SetUnencryptedHTTP2(true)
backend.Start()
t.Cleanup(backend.Close)
dc, err := routestring.New(fmt.Sprintf(`r0: * -> "%s"`, "h2c"+backend.URL[len("http"):]))
if err != nil {
t.Fatalf("failed to create data client: %v", err)
}
MuFindAddress.Lock()
addr := FindAddress(t)
MuFindAddress.Unlock()
rt := routing.New(routing.Options{
FilterRegistry: builtin.MakeRegistry(),
DataClients: []routing.DataClient{dc},
})
defer rt.Close()
p := proxy.WithParams(proxy.Params{
Routing: rt,
Flags: proxy.Flags(proxy.OptionsNone),
Metrics: &metricstest.MockMetrics{},
})
defer p.Close() //nolint:errcheck
o := &Options{
Address: addr,
EnableH2cServer: true,
}
sigs := make(chan os.Signal, 1)
go func() {
listenAndServeQuit(p, o, sigs, nil, nil, nil) //nolint:errcheck
}()
t.Cleanup(func() { sigs <- syscall.SIGTERM })
h2cTransport := &http.Transport{}
h2cTransport.Protocols = new(http.Protocols)
h2cTransport.Protocols.SetHTTP1(true)
h2cTransport.Protocols.SetUnencryptedHTTP2(true)
h2cClient := &http.Client{
Transport: h2cTransport,
Timeout: 5 * time.Second,
}
var rsp *http.Response
var reqErr error
for range 20 {
rsp, reqErr = h2cClient.Get("http://" + addr + "/")
if reqErr == nil {
break
}
time.Sleep(15 * time.Millisecond)
}
if reqErr != nil {
t.Fatalf("h2c client request failed: %v", reqErr)
}
defer rsp.Body.Close()
if rsp.Proto != "HTTP/2.0" {
t.Errorf("expected client-to-proxy h2c proto HTTP/2.0, got %q", rsp.Proto)
}
mu.Lock()
gotProto := proto
mu.Unlock()
if gotProto != "HTTP/2.0" {
t.Errorf("expected proxy-to-backend h2c proto HTTP/2.0, got %q", gotProto)
}
}