Skip to content

Commit 448d03e

Browse files
committed
Implement Unwrap() for responseWriterDelegator
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 448d03e

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

prometheus/promhttp/delegator.go

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ func (r *responseWriterDelegator) Write(b []byte) (int, error) {
7676
return n, err
7777
}
7878

79+
func (r *responseWriterDelegator) Unwrap() http.ResponseWriter {
80+
return r.ResponseWriter
81+
}
82+
7983
type (
8084
closeNotifierDelegator struct{ *responseWriterDelegator }
8185
flusherDelegator struct{ *responseWriterDelegator }

prometheus/promhttp/delegator_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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/httptest"
18+
"testing"
19+
)
20+
21+
func TestResponseWriterDelegatorUnwrap(t *testing.T) {
22+
w := httptest.NewRecorder()
23+
rwd := &responseWriterDelegator{ResponseWriter: w}
24+
25+
if rwd.Unwrap() != w {
26+
t.Error("unwrapped responsewriter must equal to the original responsewriter")
27+
}
28+
}

0 commit comments

Comments
 (0)