From f753e0b669ff1d608468098e38c762b44f844080 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Wed, 26 Oct 2016 11:50:37 +0100 Subject: [PATCH] Move test util functions to dedicated file to clean up test files Also use verbose mode on tests so you can see which test is running when things print to stdout. --- hooks/pre-commit | 2 +- .../matrix-org/go-neb/goneb_services_test.go | 57 ++--------------- .../matrix-org/go-neb/testutil_test.go | 63 +++++++++++++++++++ 3 files changed, 69 insertions(+), 53 deletions(-) create mode 100644 src/github.com/matrix-org/go-neb/testutil_test.go diff --git a/hooks/pre-commit b/hooks/pre-commit index 09ff264..cc0e2f0 100755 --- a/hooks/pre-commit +++ b/hooks/pre-commit @@ -6,4 +6,4 @@ golint src/... go fmt ./src/... go tool vet --shadow ./src gocyclo -over 12 src/ -gb test -timeout 5s +gb test -timeout 5s -test.v diff --git a/src/github.com/matrix-org/go-neb/goneb_services_test.go b/src/github.com/matrix-org/go-neb/goneb_services_test.go index baa925a..3dcf5d9 100644 --- a/src/github.com/matrix-org/go-neb/goneb_services_test.go +++ b/src/github.com/matrix-org/go-neb/goneb_services_test.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "fmt" "net/http" "net/http/httptest" "os" @@ -10,45 +9,7 @@ import ( ) var mux = http.NewServeMux() - -type MockTripper struct { - handlers map[string]func(req *http.Request) (*http.Response, error) -} - -func (rt MockTripper) RoundTrip(req *http.Request) (*http.Response, error) { - key := req.Method + " " + req.URL.Path - h := rt.handlers[key] - if h == nil { - panic( - fmt.Sprintf("Test RoundTrip: Unhandled request: %s\nHandlers: %d", - key, len(rt.handlers)), - ) - } - return h(req) -} - -func (rt MockTripper) Handle(method, path string, handler func(req *http.Request) (*http.Response, error)) { - key := method + " " + path - if _, exists := rt.handlers[key]; exists { - panic("Test handler with key " + key + " already exists") - } - rt.handlers[key] = handler -} - -var tripper = MockTripper{make(map[string]func(req *http.Request) (*http.Response, error))} - -type nopCloser struct { - *bytes.Buffer -} - -func (nopCloser) Close() error { return nil } - -func newResponse(statusCode int, body string) *http.Response { - return &http.Response{ - StatusCode: statusCode, - Body: nopCloser{bytes.NewBufferString(body)}, - } -} +var mxTripper = newMatrixTripper() func TestMain(m *testing.M) { setup(envVars{ @@ -56,26 +17,18 @@ func TestMain(m *testing.M) { DatabaseType: "sqlite3", DatabaseURL: ":memory:", }, mux, &http.Client{ - Transport: tripper, + Transport: mxTripper, }) exitCode := m.Run() os.Exit(exitCode) } func TestConfigureClient(t *testing.T) { - for k := range tripper.handlers { - delete(tripper.handlers, k) - } + mxTripper.ClearHandlers() mockWriter := httptest.NewRecorder() - tripper.Handle("POST", "/_matrix/client/r0/user/@link:hyrule/filter", - func(req *http.Request) (*http.Response, error) { - return newResponse(200, `{ - "filter_id":"abcdef" - }`), nil - }, - ) syncChan := make(chan string) - tripper.Handle("GET", "/_matrix/client/r0/sync", + mxTripper.HandlePOSTFilter("@link:hyrule") + mxTripper.Handle("GET", "/_matrix/client/r0/sync", func(req *http.Request) (*http.Response, error) { syncChan <- "sync" return newResponse(200, `{ diff --git a/src/github.com/matrix-org/go-neb/testutil_test.go b/src/github.com/matrix-org/go-neb/testutil_test.go new file mode 100644 index 0000000..c3d63aa --- /dev/null +++ b/src/github.com/matrix-org/go-neb/testutil_test.go @@ -0,0 +1,63 @@ +package main + +import ( + "bytes" + "fmt" + "io/ioutil" + "net/http" +) + +// newResponse creates a new HTTP response with the given data. +func newResponse(statusCode int, body string) *http.Response { + return &http.Response{ + StatusCode: statusCode, + Body: ioutil.NopCloser(bytes.NewBufferString(body)), + } +} + +// matrixTripper mocks out RoundTrip and calls a registered handler instead. +type matrixTripper struct { + handlers map[string]func(req *http.Request) (*http.Response, error) +} + +func newMatrixTripper() *matrixTripper { + return &matrixTripper{ + handlers: make(map[string]func(req *http.Request) (*http.Response, error)), + } +} + +func (rt *matrixTripper) RoundTrip(req *http.Request) (*http.Response, error) { + key := req.Method + " " + req.URL.Path + h := rt.handlers[key] + if h == nil { + panic(fmt.Sprintf( + "RoundTrip: Unhandled request: %s\nHandlers: %d", + key, len(rt.handlers), + )) + } + return h(req) +} + +func (rt *matrixTripper) Handle(method, path string, handler func(req *http.Request) (*http.Response, error)) { + key := method + " " + path + if _, exists := rt.handlers[key]; exists { + panic(fmt.Sprintf("Test handler with key %s already exists", key)) + } + rt.handlers[key] = handler +} + +func (rt *matrixTripper) HandlePOSTFilter(userID string) { + rt.Handle("POST", "/_matrix/client/r0/user/"+userID+"/filter", + func(req *http.Request) (*http.Response, error) { + return newResponse(200, `{ + "filter_id":"abcdef" + }`), nil + }, + ) +} + +func (rt *matrixTripper) ClearHandlers() { + for k := range rt.handlers { + delete(rt.handlers, k) + } +}