grafana/pkg/web/response_writer_test.go
Serge Zaitsev f5802878f1
Chore: Move remaining web framework code to pkg/web, remove macaron binding module (#43018)
* remove macaron binding dependency

* completely purge macaron binding

* move everything to pkg/web

* remove non-go files from pkg/web

* clean up leftovers of macaron imports

* make linter happy
2021-12-13 15:56:14 +01:00

43 lines
935 B
Go

package web
import (
"net/http"
"testing"
"github.com/stretchr/testify/require"
)
func Test_responseWriter_WriteHeader(t *testing.T) {
t.Run("it should set status code as expected", func(t *testing.T) {
f := fakeResponseWriter{}
rw := NewResponseWriter("GET", &f)
rw.WriteHeader(200)
require.Equal(t, 200, rw.Status())
require.Equal(t, 200, f.Status)
})
t.Run("it should set status code to 500 if WriteHeader is called with invalid HTTP status", func(t *testing.T) {
f := fakeResponseWriter{}
rw := NewResponseWriter("GET", &f)
rw.WriteHeader(0)
require.Equal(t, 500, rw.Status())
require.Equal(t, 500, f.Status)
})
}
type fakeResponseWriter struct {
Status int
}
func (f *fakeResponseWriter) Header() http.Header {
return http.Header{}
}
func (f *fakeResponseWriter) Write([]byte) (int, error) {
return 0, nil
}
func (f *fakeResponseWriter) WriteHeader(statusCode int) {
f.Status = statusCode
}