|
| 1 | +// Copyright 2021-2025 The Connect Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +//go:build go1.25 |
| 16 | + |
| 17 | +// Copyright 2021-2025 The Connect Authors |
| 18 | +// |
| 19 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 20 | +// you may not use this file except in compliance with the License. |
| 21 | +// You may obtain a copy of the License at |
| 22 | +// |
| 23 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 24 | +// |
| 25 | +// Unless required by applicable law or agreed to in writing, software |
| 26 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 27 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 28 | +// See the License for the specific language governing permissions and |
| 29 | +// limitations under the License. |
| 30 | + |
| 31 | +package memhttptest_test |
| 32 | + |
| 33 | +import ( |
| 34 | + "bytes" |
| 35 | + "io" |
| 36 | + "net/http" |
| 37 | + "strings" |
| 38 | + "testing" |
| 39 | + "testing/synctest" |
| 40 | + |
| 41 | + "connectrpc.com/connect/internal/assert" |
| 42 | + "connectrpc.com/connect/internal/memhttp" |
| 43 | + "connectrpc.com/connect/internal/memhttp/memhttptest" |
| 44 | +) |
| 45 | + |
| 46 | +// TestMemhttpWithSynctest verifies that memhttp works correctly with synctest. |
| 47 | +func TestMemhttpWithSynctest(t *testing.T) { |
| 48 | + t.Parallel() |
| 49 | + body := "request body" |
| 50 | + |
| 51 | + handler := http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { |
| 52 | + buf := &bytes.Buffer{} |
| 53 | + _, err := io.Copy(buf, request.Body) |
| 54 | + if err != nil { |
| 55 | + t.Errorf("failed to copy body: %v", err) |
| 56 | + } |
| 57 | + if buf.String() != body { |
| 58 | + t.Errorf("got body %q, want %q", buf.String(), body) |
| 59 | + } |
| 60 | + writer.WriteHeader(http.StatusOK) |
| 61 | + }) |
| 62 | + |
| 63 | + tests := []struct { |
| 64 | + name string |
| 65 | + client func(*testing.T, *memhttp.Server) *http.Client |
| 66 | + }{ |
| 67 | + { |
| 68 | + name: "server.Client()", |
| 69 | + client: func(t *testing.T, s *memhttp.Server) *http.Client { |
| 70 | + t.Helper() |
| 71 | + return s.Client() |
| 72 | + }, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "Custom Client HTTP/1", |
| 76 | + client: func(t *testing.T, s *memhttp.Server) *http.Client { |
| 77 | + t.Helper() |
| 78 | + // HTTP/1.1's is a per-request closure, so nothing leaks outside the bubble. |
| 79 | + return &http.Client{Transport: s.TransportHTTP1()} |
| 80 | + }, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "Custom Client HTTP/2", |
| 84 | + client: func(t *testing.T, s *memhttp.Server) *http.Client { |
| 85 | + t.Helper() |
| 86 | + // HTTP/2 a goroutine running for future connections, which leaks outside the bubble. |
| 87 | + client := &http.Client{Transport: s.Transport()} |
| 88 | + // Closing idle connections here ensures synctest doesn't panic. |
| 89 | + t.Cleanup(client.CloseIdleConnections) |
| 90 | + return client |
| 91 | + }, |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + for _, test := range tests { |
| 96 | + t.Run(test.name, func(t *testing.T) { |
| 97 | + t.Parallel() |
| 98 | + synctest.Test(t, func(t *testing.T) { |
| 99 | + t.Helper() |
| 100 | + server := memhttptest.NewServer(t, handler) |
| 101 | + |
| 102 | + req, err := http.NewRequestWithContext(t.Context(), http.MethodPut, server.URL(), strings.NewReader(body)) |
| 103 | + assert.Nil(t, err) |
| 104 | + |
| 105 | + client := test.client(t, server) |
| 106 | + resp, err := client.Do(req) |
| 107 | + assert.Nil(t, err) |
| 108 | + resp.Body.Close() |
| 109 | + }) |
| 110 | + }) |
| 111 | + } |
| 112 | +} |
0 commit comments