|
| 1 | +// Copyright 2024 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package promhttp |
| 15 | + |
| 16 | +import ( |
| 17 | + "net/http" |
| 18 | + "testing" |
| 19 | + "time" |
| 20 | +) |
| 21 | + |
| 22 | +type responseWriter struct { |
| 23 | + flushErrorCalled bool |
| 24 | + setWriteDeadlineCalled time.Time |
| 25 | + setReadDeadlineCalled time.Time |
| 26 | +} |
| 27 | + |
| 28 | +func (rw *responseWriter) Header() http.Header { |
| 29 | + return nil |
| 30 | +} |
| 31 | + |
| 32 | +func (rw *responseWriter) Write(p []byte) (int, error) { |
| 33 | + return 0, nil |
| 34 | +} |
| 35 | + |
| 36 | +func (rw *responseWriter) WriteHeader(statusCode int) { |
| 37 | +} |
| 38 | + |
| 39 | +func (rw *responseWriter) FlushError() error { |
| 40 | + rw.flushErrorCalled = true |
| 41 | + |
| 42 | + return nil |
| 43 | +} |
| 44 | + |
| 45 | +func (rw *responseWriter) SetWriteDeadline(deadline time.Time) error { |
| 46 | + rw.setWriteDeadlineCalled = deadline |
| 47 | + |
| 48 | + return nil |
| 49 | +} |
| 50 | + |
| 51 | +func (rw *responseWriter) SetReadDeadline(deadline time.Time) error { |
| 52 | + rw.setReadDeadlineCalled = deadline |
| 53 | + |
| 54 | + return nil |
| 55 | +} |
| 56 | + |
| 57 | +func TestResponseWriterDelegatorUnwrap(t *testing.T) { |
| 58 | + w := &responseWriter{} |
| 59 | + rwd := &responseWriterDelegator{ResponseWriter: w} |
| 60 | + |
| 61 | + if rwd.Unwrap() != w { |
| 62 | + t.Error("unwrapped responsewriter must equal to the original responsewriter") |
| 63 | + } |
| 64 | + |
| 65 | + controller := http.NewResponseController(rwd) |
| 66 | + if err := controller.Flush(); err != nil || !w.flushErrorCalled { |
| 67 | + t.Error("FlushError must be propagated to the original responsewriter") |
| 68 | + } |
| 69 | + |
| 70 | + timeNow := time.Now() |
| 71 | + if err := controller.SetWriteDeadline(timeNow); err != nil || w.setWriteDeadlineCalled != timeNow { |
| 72 | + t.Error("SetWriteDeadline must be propagated to the original responsewriter") |
| 73 | + } |
| 74 | + |
| 75 | + if err := controller.SetReadDeadline(timeNow); err != nil || w.setReadDeadlineCalled != timeNow { |
| 76 | + t.Error("SetReadDeadline must be propagated to the original responsewriter") |
| 77 | + } |
| 78 | +} |
0 commit comments