| 93 | } |
| 94 | |
| 95 | func 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) |