Skip to content

Commit

Permalink
Implement Unwrap() for responseWriterDelegator
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Igor Drozdov committed Mar 27, 2024
1 parent 50ab457 commit 550ce82
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions prometheus/promhttp/delegator.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func (r *responseWriterDelegator) Write(b []byte) (int, error) {
return n, err
}

func (r *responseWriterDelegator) Unwrap() http.ResponseWriter {
return r.ResponseWriter
}

type (
closeNotifierDelegator struct{ *responseWriterDelegator }
flusherDelegator struct{ *responseWriterDelegator }
Expand Down
28 changes: 28 additions & 0 deletions prometheus/promhttp/delegator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2024 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package promhttp

import (
"net/http/httptest"
"testing"
)

func TestResponseWriterDelegatorUnwrap(t *testing.T) {
w := httptest.NewRecorder()
rwd := &responseWriterDelegator{ResponseWriter: w}

if rwd.Unwrap() != w {
t.Error("unwrapped responsewriter must equal to the original responsewriter")
}
}

0 comments on commit 550ce82

Please sign in to comment.