MCPcopy
hub / github.com/cloudflare/cloudflared / TestServeHTTP

Function TestServeHTTP

connection/http2_test.go:95–165  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

93}
94
95func TestServeHTTP(t *testing.T) {
96 tests := []testRequest{
97 {
98 name: "ok",
99 endpoint: "ok",
100 expectedStatus: http.StatusOK,
101 expectedBody: []byte(http.StatusText(http.StatusOK)),
102 },
103 {
104 name: "large_file",
105 endpoint: "large_file",
106 expectedStatus: http.StatusOK,
107 expectedBody: testLargeResp,
108 },
109 {
110 name: "Bad request",
111 endpoint: "400",
112 expectedStatus: http.StatusBadRequest,
113 expectedBody: []byte(http.StatusText(http.StatusBadRequest)),
114 },
115 {
116 name: "Internal server error",
117 endpoint: "500",
118 expectedStatus: http.StatusInternalServerError,
119 expectedBody: []byte(http.StatusText(http.StatusInternalServerError)),
120 },
121 {
122 name: "Proxy error",
123 endpoint: "error",
124 expectedStatus: http.StatusBadGateway,
125 expectedBody: nil,
126 isProxyError: true,
127 },
128 }
129
130 http2Conn, edgeConn := newTestHTTP2Connection()
131
132 ctx, cancel := context.WithCancel(t.Context())
133 var wg sync.WaitGroup
134 wg.Add(1)
135 go func() {
136 defer wg.Done()
137 _ = http2Conn.Serve(ctx)
138 }()
139
140 edgeHTTP2Conn, err := testTransport.NewClientConn(edgeConn)
141 require.NoError(t, err)
142
143 for _, test := range tests {
144 endpoint := fmt.Sprintf("http://localhost:8080/%s", test.endpoint)
145 req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
146 require.NoError(t, err)
147
148 resp, err := edgeHTTP2Conn.RoundTrip(req)
149 require.NoError(t, err)
150 require.Equal(t, test.expectedStatus, resp.StatusCode)
151 if test.expectedBody != nil {
152 respBody, err := io.ReadAll(resp.Body)

Callers

nothing calls this directly

Calls 9

newTestHTTP2ConnectionFunction · 0.85
GetMethod · 0.80
WaitMethod · 0.80
ContextMethod · 0.65
AddMethod · 0.65
ServeMethod · 0.65
EqualMethod · 0.65
CloseMethod · 0.65
RoundTripMethod · 0.45

Tested by

no test coverage detected