From 9b4126cae617dd9cc3d87accba22a08b21eee59b Mon Sep 17 00:00:00 2001 From: Marius Date: Thu, 13 Oct 2016 12:29:13 +0200 Subject: [PATCH] Enable subtests for environments prior to Go 1.7 --- handler_test.go | 114 ------------------------------------------- subtest_go17_test.go | 22 +++++++++ subtest_test.go | 28 +++++++++++ utils_test.go | 109 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 159 insertions(+), 114 deletions(-) create mode 100644 subtest_go17_test.go create mode 100644 subtest_test.go create mode 100644 utils_test.go diff --git a/handler_test.go b/handler_test.go index 748d325..73ce184 100644 --- a/handler_test.go +++ b/handler_test.go @@ -1,128 +1,14 @@ package tusd_test import ( - "fmt" - "io" - "io/ioutil" "net/http" - "net/http/httptest" - "reflect" "strings" "testing" "github.com/golang/mock/gomock" - - "github.com/tus/tusd" . "github.com/tus/tusd" ) -type zeroStore struct{} - -func (store zeroStore) NewUpload(info FileInfo) (string, error) { - return "", nil -} -func (store zeroStore) WriteChunk(id string, offset int64, src io.Reader) (int64, error) { - return 0, nil -} - -func (store zeroStore) GetInfo(id string) (FileInfo, error) { - return FileInfo{}, nil -} - -type FullDataStore interface { - tusd.DataStore - tusd.TerminaterDataStore - tusd.ConcaterDataStore - tusd.GetReaderDataStore -} - -type httpTest struct { - Name string - - Method string - URL string - - ReqBody io.Reader - ReqHeader map[string]string - - Code int - ResBody string - ResHeader map[string]string -} - -func (test *httpTest) Run(handler http.Handler, t *testing.T) *httptest.ResponseRecorder { - req, _ := http.NewRequest(test.Method, test.URL, test.ReqBody) - - // Add headers - for key, value := range test.ReqHeader { - req.Header.Set(key, value) - } - - req.Host = "tus.io" - w := httptest.NewRecorder() - handler.ServeHTTP(w, req) - - if w.Code != test.Code { - t.Errorf("Expected %v %s as status code (got %v %s)", test.Code, http.StatusText(test.Code), w.Code, http.StatusText(w.Code)) - } - - for key, value := range test.ResHeader { - header := w.HeaderMap.Get(key) - - if value != header { - t.Errorf("Expected '%s' as '%s' (got '%s')", value, key, header) - } - } - - if test.ResBody != "" && string(w.Body.Bytes()) != test.ResBody { - t.Errorf("Expected '%s' as body (got '%s'", test.ResBody, string(w.Body.Bytes())) - } - - return w -} - -func SubTest(t *testing.T, name string, runTest func(*testing.T, *MockFullDataStore)) { - t.Run(name, func(subT *testing.T) { - //subT.Parallel() - - ctrl := gomock.NewController(subT) - defer ctrl.Finish() - - store := NewMockFullDataStore(ctrl) - - runTest(subT, store) - }) -} - -type ReaderMatcher struct { - expect string -} - -func NewReaderMatcher(expect string) gomock.Matcher { - return ReaderMatcher{ - expect: expect, - } -} - -func (m ReaderMatcher) Matches(x interface{}) bool { - input, ok := x.(io.Reader) - if !ok { - return false - } - - bytes, err := ioutil.ReadAll(input) - if err != nil { - panic(err) - } - - readStr := string(bytes) - return reflect.DeepEqual(m.expect, readStr) -} - -func (m ReaderMatcher) String() string { - return fmt.Sprintf("reads to %s", m.expect) -} - func TestMethodOverride(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() diff --git a/subtest_go17_test.go b/subtest_go17_test.go new file mode 100644 index 0000000..272e106 --- /dev/null +++ b/subtest_go17_test.go @@ -0,0 +1,22 @@ +// +build go1.7 + +package tusd_test + +import ( + "testing" + + "github.com/golang/mock/gomock" +) + +func SubTest(t *testing.T, name string, runTest func(*testing.T, *MockFullDataStore)) { + t.Run(name, func(subT *testing.T) { + //subT.Parallel() + + ctrl := gomock.NewController(subT) + defer ctrl.Finish() + + store := NewMockFullDataStore(ctrl) + + runTest(subT, store) + }) +} diff --git a/subtest_test.go b/subtest_test.go new file mode 100644 index 0000000..6fd44a6 --- /dev/null +++ b/subtest_test.go @@ -0,0 +1,28 @@ +// +build !go1.7 + +package tusd_test + +import ( + "fmt" + "testing" + + "github.com/golang/mock/gomock" +) + +func SubTest(t *testing.T, name string, runTest func(*testing.T, *MockFullDataStore)) { + fmt.Println("\t=== RUN SubTest:", name) + + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + store := NewMockFullDataStore(ctrl) + + runTest(t, store) + + if t.Failed() { + fmt.Println("\t--- FAIL SubTest:", name) + t.FailNow() + } else { + fmt.Println("\t--- PASS SubTest:", name) + } +} diff --git a/utils_test.go b/utils_test.go new file mode 100644 index 0000000..4449217 --- /dev/null +++ b/utils_test.go @@ -0,0 +1,109 @@ +package tusd_test + +import ( + "fmt" + "io" + "io/ioutil" + "net/http" + "net/http/httptest" + "reflect" + "testing" + + "github.com/golang/mock/gomock" + "github.com/tus/tusd" + . "github.com/tus/tusd" +) + +type zeroStore struct{} + +func (store zeroStore) NewUpload(info FileInfo) (string, error) { + return "", nil +} +func (store zeroStore) WriteChunk(id string, offset int64, src io.Reader) (int64, error) { + return 0, nil +} + +func (store zeroStore) GetInfo(id string) (FileInfo, error) { + return FileInfo{}, nil +} + +type FullDataStore interface { + tusd.DataStore + tusd.TerminaterDataStore + tusd.ConcaterDataStore + tusd.GetReaderDataStore +} + +type httpTest struct { + Name string + + Method string + URL string + + ReqBody io.Reader + ReqHeader map[string]string + + Code int + ResBody string + ResHeader map[string]string +} + +func (test *httpTest) Run(handler http.Handler, t *testing.T) *httptest.ResponseRecorder { + req, _ := http.NewRequest(test.Method, test.URL, test.ReqBody) + + // Add headers + for key, value := range test.ReqHeader { + req.Header.Set(key, value) + } + + req.Host = "tus.io" + w := httptest.NewRecorder() + handler.ServeHTTP(w, req) + + if w.Code != test.Code { + t.Errorf("Expected %v %s as status code (got %v %s)", test.Code, http.StatusText(test.Code), w.Code, http.StatusText(w.Code)) + } + + for key, value := range test.ResHeader { + header := w.HeaderMap.Get(key) + + if value != header { + t.Errorf("Expected '%s' as '%s' (got '%s')", value, key, header) + } + } + + if test.ResBody != "" && string(w.Body.Bytes()) != test.ResBody { + t.Errorf("Expected '%s' as body (got '%s'", test.ResBody, string(w.Body.Bytes())) + } + + return w +} + +type ReaderMatcher struct { + expect string +} + +func NewReaderMatcher(expect string) gomock.Matcher { + return ReaderMatcher{ + expect: expect, + } +} + +func (m ReaderMatcher) Matches(x interface{}) bool { + input, ok := x.(io.Reader) + if !ok { + return false + } + + bytes, err := ioutil.ReadAll(input) + if err != nil { + panic(err) + } + + readStr := string(bytes) + return reflect.DeepEqual(m.expect, readStr) +} + +func (m ReaderMatcher) String() string { + return fmt.Sprintf("reads to %s", m.expect) +}