Debian Codegraph @

caddytest/integration/h2listener_test.go

raw symbols SCIP
1 package integration
2
3 import (
4 "fmt"
5 "net/http"
6 "slices"
7 "strings"
8 "testing"
9
10 "github.com/caddyserver/caddy/v2/caddytest"
11 )
12
13 func newH2ListenerWithVersionsWithTLSTester(t *testing.T, serverVersions []string, clientVersions []string) *caddytest.Tester {
14 const baseConfig = `
15 {
16 skip_install_trust
17 admin localhost:2999
18 http_port 9080
19 https_port 9443
20 servers :9443 {
21 protocols %s
22 }
23 }
24 localhost {
25 respond "{http.request.tls.proto} {http.request.proto}"
26 }
27 `
28 tester := caddytest.NewTester(t)
29 tester.InitServer(fmt.Sprintf(baseConfig, strings.Join(serverVersions, " ")), "caddyfile")
30
31 tr := tester.Client.Transport.(*http.Transport)
32 tr.TLSClientConfig.NextProtos = clientVersions
33 tr.Protocols = new(http.Protocols)
34 if slices.Contains(clientVersions, "h2") {
35 tr.ForceAttemptHTTP2 = true
36 tr.Protocols.SetHTTP2(true)
37 }
38 if !slices.Contains(clientVersions, "http/1.1") {
39 tr.Protocols.SetHTTP1(false)
40 }
41
42 return tester
43 }
44
45 func TestH2ListenerWithTLS(t *testing.T) {
46 tests := []struct {
47 serverVersions []string
48 clientVersions []string
49 expectedBody string
50 failed bool
51 }{
52 {[]string{"h2"}, []string{"h2"}, "h2 HTTP/2.0", false},
53 {[]string{"h2"}, []string{"http/1.1"}, "", true},
54 {[]string{"h1"}, []string{"http/1.1"}, "http/1.1 HTTP/1.1", false},
55 {[]string{"h1"}, []string{"h2"}, "", true},
56 {[]string{"h2", "h1"}, []string{"h2"}, "h2 HTTP/2.0", false},
57 {[]string{"h2", "h1"}, []string{"http/1.1"}, "http/1.1 HTTP/1.1", false},
58 }
59 for _, tc := range tests {
60 tester := newH2ListenerWithVersionsWithTLSTester(t, tc.serverVersions, tc.clientVersions)
61 t.Logf("running with server versions %v and client versions %v:", tc.serverVersions, tc.clientVersions)
62 if tc.failed {
63 resp, err := tester.Client.Get("https://localhost:9443")
64 if err == nil {
65 t.Errorf("unexpected response: %d", resp.StatusCode)
66 }
67 } else {
68 tester.AssertGetResponse("https://localhost:9443", 200, tc.expectedBody)
69 }
70 }
71 }
72
73 func newH2ListenerWithVersionsWithoutTLSTester(t *testing.T, serverVersions []string, clientVersions []string) *caddytest.Tester {
74 const baseConfig = `
75 {
76 skip_install_trust
77 admin localhost:2999
78 http_port 9080
79 servers :9080 {
80 protocols %s
81 }
82 }
83 http://localhost {
84 respond "{http.request.proto}"
85 }
86 `
87 tester := caddytest.NewTester(t)
88 tester.InitServer(fmt.Sprintf(baseConfig, strings.Join(serverVersions, " ")), "caddyfile")
89
90 tr := tester.Client.Transport.(*http.Transport)
91 tr.Protocols = new(http.Protocols)
92 if slices.Contains(clientVersions, "h2c") {
93 tr.Protocols.SetHTTP1(false)
94 tr.Protocols.SetUnencryptedHTTP2(true)
95 } else if slices.Contains(clientVersions, "http/1.1") {
96 tr.Protocols.SetHTTP1(true)
97 tr.Protocols.SetUnencryptedHTTP2(false)
98 }
99
100 return tester
101 }
102
103 func TestH2ListenerWithoutTLS(t *testing.T) {
104 tests := []struct {
105 serverVersions []string
106 clientVersions []string
107 expectedBody string
108 failed bool
109 }{
110 {[]string{"h2c"}, []string{"h2c"}, "HTTP/2.0", false},
111 {[]string{"h2c"}, []string{"http/1.1"}, "", true},
112 {[]string{"h1"}, []string{"http/1.1"}, "HTTP/1.1", false},
113 {[]string{"h1"}, []string{"h2c"}, "", true},
114 {[]string{"h2c", "h1"}, []string{"h2c"}, "HTTP/2.0", false},
115 {[]string{"h2c", "h1"}, []string{"http/1.1"}, "HTTP/1.1", false},
116 }
117 for _, tc := range tests {
118 tester := newH2ListenerWithVersionsWithoutTLSTester(t, tc.serverVersions, tc.clientVersions)
119 t.Logf("running with server versions %v and client versions %v:", tc.serverVersions, tc.clientVersions)
120 if tc.failed {
121 resp, err := tester.Client.Get("http://localhost:9080")
122 if err == nil {
123 t.Errorf("unexpected response: %d", resp.StatusCode)
124 }
125 } else {
126 tester.AssertGetResponse("http://localhost:9080", 200, tc.expectedBody)
127 }
128 }
129 }