Skip to content

Commit

Permalink
Refactor remaining tests to subtest style
Browse files Browse the repository at this point in the history
  • Loading branch information
Acconut committed Oct 13, 2016
1 parent 5be9ded commit c2bb17b
Show file tree
Hide file tree
Showing 7 changed files with 347 additions and 421 deletions.
65 changes: 35 additions & 30 deletions cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,42 @@ import (
)

func TestCORS(t *testing.T) {
store := NewStoreComposer()
store.UseCore(zeroStore{})
handler, _ := NewHandler(Config{
StoreComposer: store,
SubTest(t, "Preflight", func(t *testing.T, store *MockFullDataStore) {
handler, _ := NewHandler(Config{
DataStore: store,
})

(&httpTest{
Method: "OPTIONS",
ReqHeader: map[string]string{
"Origin": "tus.io",
},
Code: http.StatusOK,
ResHeader: map[string]string{
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Upload-Length, Upload-Offset, Tus-Resumable, Upload-Metadata",
"Access-Control-Allow-Methods": "POST, GET, HEAD, PATCH, DELETE, OPTIONS",
"Access-Control-Max-Age": "86400",
"Access-Control-Allow-Origin": "tus.io",
},
}).Run(handler, t)
})

(&httpTest{
Name: "Preflight request",
Method: "OPTIONS",
ReqHeader: map[string]string{
"Origin": "tus.io",
},
Code: http.StatusOK,
ResHeader: map[string]string{
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Upload-Length, Upload-Offset, Tus-Resumable, Upload-Metadata",
"Access-Control-Allow-Methods": "POST, GET, HEAD, PATCH, DELETE, OPTIONS",
"Access-Control-Max-Age": "86400",
"Access-Control-Allow-Origin": "tus.io",
},
}).Run(handler, t)
SubTest(t, "Request", func(t *testing.T, store *MockFullDataStore) {
handler, _ := NewHandler(Config{
DataStore: store,
})

(&httpTest{
Name: "Actual request",
Method: "GET",
ReqHeader: map[string]string{
"Origin": "tus.io",
},
Code: http.StatusMethodNotAllowed,
ResHeader: map[string]string{
"Access-Control-Expose-Headers": "Upload-Offset, Location, Upload-Length, Tus-Version, Tus-Resumable, Tus-Max-Size, Tus-Extension, Upload-Metadata",
"Access-Control-Allow-Origin": "tus.io",
},
}).Run(handler, t)
(&httpTest{
Name: "Actual request",
Method: "GET",
ReqHeader: map[string]string{
"Origin": "tus.io",
},
Code: http.StatusMethodNotAllowed,
ResHeader: map[string]string{
"Access-Control-Expose-Headers": "Upload-Offset, Location, Upload-Length, Tus-Version, Tus-Resumable, Tus-Max-Size, Tus-Extension, Upload-Metadata",
"Access-Control-Allow-Origin": "tus.io",
},
}).Run(handler, t)
})
}
8 changes: 4 additions & 4 deletions get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ func (reader *closingStringReader) Close() error {
return nil
}

var reader = &closingStringReader{
Reader: strings.NewReader("hello"),
}

func TestGet(t *testing.T) {
SubTest(t, "Download", func(t *testing.T, store *MockFullDataStore) {
reader := &closingStringReader{
Reader: strings.NewReader("hello"),
}

gomock.InOrder(
store.EXPECT().GetInfo("yes").Return(FileInfo{
Offset: 5,
Expand Down
43 changes: 42 additions & 1 deletion handler_mock_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Automatically generated by MockGen. DO NOT EDIT!
// Source: handler_test.go
// Source: utils_test.go

package tusd_test

Expand Down Expand Up @@ -94,3 +94,44 @@ func (_m *MockFullDataStore) GetReader(id string) (io.Reader, error) {
func (_mr *_MockFullDataStoreRecorder) GetReader(arg0 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "GetReader", arg0)
}

// Mock of Locker interface
type MockLocker struct {
ctrl *gomock.Controller
recorder *_MockLockerRecorder
}

// Recorder for MockLocker (not exported)
type _MockLockerRecorder struct {
mock *MockLocker
}

func NewMockLocker(ctrl *gomock.Controller) *MockLocker {
mock := &MockLocker{ctrl: ctrl}
mock.recorder = &_MockLockerRecorder{mock}
return mock
}

func (_m *MockLocker) EXPECT() *_MockLockerRecorder {
return _m.recorder
}

func (_m *MockLocker) LockUpload(id string) error {
ret := _m.ctrl.Call(_m, "LockUpload", id)
ret0, _ := ret[0].(error)
return ret0
}

func (_mr *_MockLockerRecorder) LockUpload(arg0 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "LockUpload", arg0)
}

func (_m *MockLocker) UnlockUpload(id string) error {
ret := _m.ctrl.Call(_m, "UnlockUpload", id)
ret0, _ := ret[0].(error)
return ret0
}

func (_mr *_MockLockerRecorder) UnlockUpload(arg0 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "UnlockUpload", arg0)
}
45 changes: 0 additions & 45 deletions handler_test.go

This file was deleted.

55 changes: 31 additions & 24 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,38 @@ import (
)

func TestOptions(t *testing.T) {
store := NewStoreComposer()
store.UseCore(NewMockFullDataStore(nil))
handler, _ := NewHandler(Config{
StoreComposer: store,
MaxSize: 400,
SubTest(t, "Discovery", func(t *testing.T, store *MockFullDataStore) {
composer := NewStoreComposer()
composer.UseCore(store)

handler, _ := NewHandler(Config{
StoreComposer: composer,
MaxSize: 400,
})

(&httpTest{
Method: "OPTIONS",
ResHeader: map[string]string{
"Tus-Extension": "creation,creation-with-upload",
"Tus-Version": "1.0.0",
"Tus-Resumable": "1.0.0",
"Tus-Max-Size": "400",
},
Code: http.StatusOK,
}).Run(handler, t)
})

(&httpTest{
Name: "Successful request",
Method: "OPTIONS",
Code: http.StatusOK,
ResHeader: map[string]string{
"Tus-Extension": "creation,creation-with-upload",
"Tus-Version": "1.0.0",
"Tus-Resumable": "1.0.0",
"Tus-Max-Size": "400",
},
}).Run(handler, t)
SubTest(t, "InvalidVersion", func(t *testing.T, store *MockFullDataStore) {
handler, _ := NewHandler(Config{
DataStore: store,
})

(&httpTest{
Name: "Invalid or unsupported version",
Method: "POST",
ReqHeader: map[string]string{
"Tus-Resumable": "foo",
},
Code: http.StatusPreconditionFailed,
}).Run(handler, t)
(&httpTest{
Method: "POST",
ReqHeader: map[string]string{
"Tus-Resumable": "foo",
},
Code: http.StatusPreconditionFailed,
}).Run(handler, t)
})
}
Loading

0 comments on commit c2bb17b

Please sign in to comment.