Skip to content

Commit 93cf5d4

Browse files
authored
Implement Unwrap() for responseWriterDelegator (#1480)
If the ResponseWriter implements any of the following methods, the ResponseController will call them as appropriate: Flush() FlushError() error // alternative Flush returning an error Hijack() (net.Conn, *bufio.ReadWriter, error) SetReadDeadline(deadline time.Time) error SetWriteDeadline(deadline time.Time) error EnableFullDuplex() error If the ResponseWriter doesn't implement the methods, the ResponseController will call Unwrap() method until it finds a ResponseWriter in the chain This commit implements Unwrap() method to simply return the wrapped ResponseWriter Signed-off-by: Igor Drozdov <[email protected]>
1 parent 50ab457 commit 93cf5d4

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

prometheus/promhttp/delegator.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ func (r *responseWriterDelegator) Write(b []byte) (int, error) {
7676
return n, err
7777
}
7878

79+
// Unwrap lets http.ResponseController get the underlying http.ResponseWriter,
80+
// by implementing the [rwUnwrapper](https://cs.opensource.google/go/go/+/refs/tags/go1.21.4:src/net/http/responsecontroller.go;l=42-44) interface.
81+
func (r *responseWriterDelegator) Unwrap() http.ResponseWriter {
82+
return r.ResponseWriter
83+
}
84+
7985
type (
8086
closeNotifierDelegator struct{ *responseWriterDelegator }
8187
flusherDelegator struct{ *responseWriterDelegator }

prometheus/promhttp/delegator_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)