MCPcopy
hub / github.com/yusing/godoxy / TestProxyHTTPH2C

Function TestProxyHTTPH2C

agent/pkg/handler/proxy_http_test.go:22–69  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

20)
21
22func TestProxyHTTPH2C(t *testing.T) {
23 gotBackendProto := make(chan int, 1)
24 backend := httptest.NewUnstartedServer(h2c.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
25 gotBackendProto <- r.ProtoMajor
26 w.Header().Set("Content-Type", "application/grpc")
27 w.Header().Set("Trailer", "Grpc-Status")
28 w.WriteHeader(http.StatusOK)
29 _, _ = w.Write([]byte{0, 0, 0, 0, 0})
30 w.Header().Set("Grpc-Status", "0")
31 }), &http2.Server{}))
32 backend.Start()
33 t.Cleanup(backend.Close)
34
35 backendURL, err := url.Parse(backend.URL)
36 require.NoError(t, err)
37 _, err = strconv.Atoi(backendURL.Port())
38 require.NoError(t, err)
39
40 req := httptest.NewRequest(
41 http.MethodPost,
42 "http://agent.local"+agent.APIEndpointBase+agent.EndpointProxyHTTP+"/management.ManagementService/GetServerKey",
43 strings.NewReader("grpc-body"),
44 )
45 req.Header.Set("Content-Type", "application/grpc+proto")
46 (&agentproxy.Config{
47 Scheme: "h2c",
48 Host: backendURL.Host,
49 HTTPConfig: route.HTTPConfig{},
50 }).SetAgentProxyConfigHeaders(req.Header)
51
52 rec := httptest.NewRecorder()
53 handler.ProxyHTTP(rec, req)
54
55 res := rec.Result()
56 defer res.Body.Close()
57 _, _ = io.ReadAll(res.Body)
58
59 require.Equal(t, http.StatusOK, res.StatusCode)
60 require.Equal(t, "application/grpc", res.Header.Get("Content-Type"))
61 require.Equal(t, "0", res.Trailer.Get("Grpc-Status"))
62
63 select {
64 case proto := <-gotBackendProto:
65 require.Equal(t, 2, proto)
66 case <-time.After(2 * time.Second):
67 t.Fatal("timed out waiting for backend request")
68 }
69}

Callers

nothing calls this directly

Calls 10

ProxyHTTPFunction · 0.92
StartMethod · 0.65
CloseMethod · 0.65
GetMethod · 0.65
SetMethod · 0.45
HeaderMethod · 0.45
WriteHeaderMethod · 0.45
WriteMethod · 0.45
ParseMethod · 0.45

Tested by

no test coverage detected