1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package request
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRespWriterWriteHeader(t *testing.T) {
rw := NewRespWriterWrapper(&httptest.ResponseRecorder{}, func(int64) {})
rw.WriteHeader(http.StatusTeapot)
assert.Equal(t, http.StatusTeapot, rw.statusCode)
assert.True(t, rw.wroteHeader)
rw.WriteHeader(http.StatusGone)
assert.Equal(t, http.StatusTeapot, rw.statusCode)
}
func TestRespWriterFlush(t *testing.T) {
rw := NewRespWriterWrapper(&httptest.ResponseRecorder{}, func(int64) {})
rw.Flush()
assert.Equal(t, http.StatusOK, rw.statusCode)
assert.True(t, rw.wroteHeader)
}
type nonFlushableResponseWriter struct{}
func (_ nonFlushableResponseWriter) Header() http.Header {
return http.Header{}
}
func (_ nonFlushableResponseWriter) Write([]byte) (int, error) {
return 0, nil
}
func (_ nonFlushableResponseWriter) WriteHeader(int) {}
func TestRespWriterFlushNoFlusher(t *testing.T) {
rw := NewRespWriterWrapper(nonFlushableResponseWriter{}, func(int64) {})
rw.Flush()
assert.Equal(t, http.StatusOK, rw.statusCode)
assert.True(t, rw.wroteHeader)
}
func TestConcurrentRespWriterWrapper(t *testing.T) {
rw := NewRespWriterWrapper(&httptest.ResponseRecorder{}, func(int64) {})
go func() {
_, _ = rw.Write([]byte("hello world"))
}()
assert.NotNil(t, rw.BytesWritten())
assert.NotNil(t, rw.StatusCode())
assert.NoError(t, rw.Error())
}
|