Skip to content

Commit

Permalink
add a test to make sure the recordingResponseWriter preserves interfa…
Browse files Browse the repository at this point in the history
…ces implemented by the wrapped writer
  • Loading branch information
Chris Toshok committed Oct 8, 2020
1 parent 082e67f commit 30ab5aa
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.14

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/kr/pretty v0.1.0 // indirect
go.opentelemetry.io/otel v0.12.0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down
65 changes: 65 additions & 0 deletions instrumentation/github.com/gorilla/mux/otelmux/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
package otelmux

import (
"bufio"
"context"
"io"
"net"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -183,3 +186,65 @@ func TestPropagationWithCustomPropagators(t *testing.T) {

router.ServeHTTP(w, r)
}

type testResponseWriter struct {
writer http.ResponseWriter
}

func (rw *testResponseWriter) Header() http.Header {
return rw.writer.Header()
}
func (rw *testResponseWriter) Write(b []byte) (int, error) {
return rw.writer.Write(b)
}
func (rw *testResponseWriter) WriteHeader(statusCode int) {
rw.writer.WriteHeader(statusCode)
}

// implement Hijacker
func (rw *testResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return nil, nil, nil
}

// implement Pusher
func (rw *testResponseWriter) Push(target string, opts *http.PushOptions) error {
return nil
}

// implement CloseNotifier
func (rw *testResponseWriter) CloseNotify() <-chan bool {
return nil
}

// implement Flusher
func (rw *testResponseWriter) Flush() {
}

// implement io.ReaderFrom
func (rw *testResponseWriter) ReadFrom(r io.Reader) (n int64, err error) {
return 0, nil
}


func TestResponseWriterInterfaces(t *testing.T) {
// make sure the recordingResponseWriter preserves interfaces implemented by the wrapped writer
provider, _ := mocktrace.NewTracerProviderAndTracer(tracerName)

router := mux.NewRouter()
router.Use(Middleware("foobar", WithTracerProvider(provider)))
router.HandleFunc("/user/{id}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Implements(t, (*http.Hijacker)(nil), w)
assert.Implements(t, (*http.Pusher)(nil), w)
assert.Implements(t, (*http.CloseNotifier)(nil), w)
assert.Implements(t, (*http.Flusher)(nil), w)
assert.Implements(t, (*io.ReaderFrom)(nil), w)
w.WriteHeader(http.StatusOK)
}))

r := httptest.NewRequest("GET", "/user/123", nil)
w := &testResponseWriter{
writer: httptest.NewRecorder(),
}

router.ServeHTTP(w, r)
}

0 comments on commit 30ab5aa

Please sign in to comment.