Skip to content

Commit

Permalink
Refactor handler_test tests into subtests to reduce repetition (#3003)
Browse files Browse the repository at this point in the history
* refactor handler_test tests into subtests to reduce repetition

* provide testing.T to the handler

Co-authored-by: Chester Cheung <cheung.zhy.csu@gmail.com>
Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 3, 2023
1 parent 7d9d21b commit 537f38a
Showing 1 changed file with 67 additions and 50 deletions.
117 changes: 67 additions & 50 deletions instrumentation/net/http/otelhttp/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -26,56 +27,72 @@ import (
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

func TestResponseWriterImplementsFlusher(t *testing.T) {
h := otelhttp.NewHandler(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Implements(t, (*http.Flusher)(nil), w)
}), "test_handler",
)
func TestHandler(t *testing.T) {
testCases := []struct {
name string
handler func(*testing.T) http.Handler
requestBody io.Reader
expectedStatusCode int
}{
{
name: "implements flusher",
handler: func(t *testing.T) http.Handler {
return otelhttp.NewHandler(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Implements(t, (*http.Flusher)(nil), w)
}), "test_handler",
)
},
expectedStatusCode: 200,
},
{
name: "succeeds",
handler: func(t *testing.T) http.Handler {
return otelhttp.NewHandler(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.NotNil(t, r.Body)

r, err := http.NewRequest(http.MethodGet, "http://localhost/", nil)
require.NoError(t, err)
b, err := io.ReadAll(r.Body)
assert.NoError(t, err)
assert.Equal(t, "hello world", string(b))
}), "test_handler",
)
},
requestBody: strings.NewReader("hello world"),
expectedStatusCode: 200,
},
{
name: "succeeds with a nil body",
handler: func(t *testing.T) http.Handler {
return otelhttp.NewHandler(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Nil(t, r.Body)
}), "test_handler",
)
},
expectedStatusCode: 200,
},
{
name: "succeeds with an http.NoBody",
handler: func(t *testing.T) http.Handler {
return otelhttp.NewHandler(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.NoBody, r.Body)
}), "test_handler",
)
},
requestBody: http.NoBody,
expectedStatusCode: 200,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
r, err := http.NewRequest(http.MethodGet, "http://localhost/", tc.requestBody)
require.NoError(t, err)

h.ServeHTTP(httptest.NewRecorder(), r)
}

// This use case is important as we make sure the body isn't mutated
// when it is nil. This is a common use case for tests where the request
// is directly passed to the handler.
func TestHandlerReadingNilBodySuccess(t *testing.T) {
h := otelhttp.NewHandler(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Body != nil {
_, err := io.ReadAll(r.Body)
assert.NoError(t, err)
}
}), "test_handler",
)

r, err := http.NewRequest(http.MethodGet, "http://localhost/", nil)
require.NoError(t, err)

rr := httptest.NewRecorder()
h.ServeHTTP(rr, r)
assert.Equal(t, 200, rr.Result().StatusCode)
}

// This use case is important as we make sure the body isn't mutated
// when it is NoBody.
func TestHandlerReadingNoBodySuccess(t *testing.T) {
h := otelhttp.NewHandler(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Body != http.NoBody {
_, err := io.ReadAll(r.Body)
assert.NoError(t, err)
}
}), "test_handler",
)

r, err := http.NewRequest(http.MethodGet, "http://localhost/", http.NoBody)
require.NoError(t, err)

rr := httptest.NewRecorder()
h.ServeHTTP(rr, r)
assert.Equal(t, 200, rr.Result().StatusCode)
rr := httptest.NewRecorder()
tc.handler(t).ServeHTTP(rr, r)
assert.Equal(t, tc.expectedStatusCode, rr.Result().StatusCode)
})
}
}

0 comments on commit 537f38a

Please sign in to comment.