Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update test utils to use an interface instead of *testing.T #212

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions rest/test/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)

// A TestReporter is an interface that is used to report errors in tests.
// It can be satisfied with *testing.T.
type TestReporter interface {
Errorf(format string, args ...interface{})
}

// MakeSimpleRequest returns a http.Request. The returned request object can be
// further prepared by adding headers and query string parmaters, for instance.
func MakeSimpleRequest(method string, urlStr string, payload interface{}) *http.Request {
Expand All @@ -40,14 +45,14 @@ func MakeSimpleRequest(method string, urlStr string, payload interface{}) *http.
}

// CodeIs compares the rescorded status code
func CodeIs(t *testing.T, r *httptest.ResponseRecorder, expectedCode int) {
func CodeIs(t TestReporter, r *httptest.ResponseRecorder, expectedCode int) {
if r.Code != expectedCode {
t.Errorf("Code %d expected, got: %d", expectedCode, r.Code)
}
}

// HeaderIs tests the first value for the given headerKey
func HeaderIs(t *testing.T, r *httptest.ResponseRecorder, headerKey, expectedValue string) {
func HeaderIs(t TestReporter, r *httptest.ResponseRecorder, headerKey, expectedValue string) {
value := r.HeaderMap.Get(headerKey)
if value != expectedValue {
t.Errorf(
Expand All @@ -59,7 +64,7 @@ func HeaderIs(t *testing.T, r *httptest.ResponseRecorder, headerKey, expectedVal
}
}

func ContentTypeIsJson(t *testing.T, r *httptest.ResponseRecorder) {
func ContentTypeIsJson(t TestReporter, r *httptest.ResponseRecorder) {

mediaType, params, _ := mime.ParseMediaType(r.HeaderMap.Get("Content-Type"))
charset := params["charset"]
Expand All @@ -79,11 +84,11 @@ func ContentTypeIsJson(t *testing.T, r *httptest.ResponseRecorder) {
}
}

func ContentEncodingIsGzip(t *testing.T, r *httptest.ResponseRecorder) {
func ContentEncodingIsGzip(t TestReporter, r *httptest.ResponseRecorder) {
HeaderIs(t, r, "Content-Encoding", "gzip")
}

func BodyIs(t *testing.T, r *httptest.ResponseRecorder, expectedBody string) {
func BodyIs(t TestReporter, r *httptest.ResponseRecorder, expectedBody string) {
body, err := DecodedBody(r)
if err != nil {
t.Errorf("Body '%s' expected, got error: '%s'", expectedBody, err)
Expand Down Expand Up @@ -126,12 +131,12 @@ func DecodedBody(r *httptest.ResponseRecorder) ([]byte, error) {
}

type Recorded struct {
T *testing.T
T TestReporter
Recorder *httptest.ResponseRecorder
}

// RunRequest runs a HTTP request through the given handler
func RunRequest(t *testing.T, handler http.Handler, request *http.Request) *Recorded {
func RunRequest(t TestReporter, handler http.Handler, request *http.Request) *Recorded {
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, request)
return &Recorded{t, recorder}
Expand Down