Use mocks generated by gomock for running tests

This commit is contained in:
Marius 2016-10-13 11:33:20 +02:00
parent 4d595672f6
commit 3f0faedfd8
8 changed files with 732 additions and 568 deletions

View File

@ -4,55 +4,47 @@ import (
"net/http" "net/http"
"testing" "testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
. "github.com/tus/tusd" . "github.com/tus/tusd"
) )
type concatPartialStore struct { func TestConcat(t *testing.T) {
t *assert.Assertions SubTest(t, "ExtensionDiscovery", func(t *testing.T, store *MockFullDataStore) {
zeroStore composer := NewStoreComposer()
} composer.UseCore(store)
composer.UseConcater(store)
func (s concatPartialStore) NewUpload(info FileInfo) (string, error) {
s.t.True(info.IsPartial)
s.t.False(info.IsFinal)
s.t.Nil(info.PartialUploads)
return "foo", nil
}
func (s concatPartialStore) GetInfo(id string) (FileInfo, error) {
return FileInfo{
IsPartial: true,
}, nil
}
func (s concatPartialStore) ConcatUploads(id string, uploads []string) error {
return nil
}
func TestConcatPartial(t *testing.T) {
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
MaxSize: 400, StoreComposer: composer,
BasePath: "files",
DataStore: concatPartialStore{
t: assert.New(t),
},
}) })
(&httpTest{ (&httpTest{
Name: "Successful OPTIONS request",
Method: "OPTIONS", Method: "OPTIONS",
URL: "", Code: http.StatusOK,
ResHeader: map[string]string{ ResHeader: map[string]string{
"Tus-Extension": "creation,creation-with-upload,concatenation", "Tus-Extension": "creation,creation-with-upload,concatenation",
}, },
Code: http.StatusOK,
}).Run(handler, t) }).Run(handler, t)
})
SubTest(t, "Partial", func(t *testing.T, store *MockFullDataStore) {
SubTest(t, "Create", func(t *testing.T, store *MockFullDataStore) {
store.EXPECT().NewUpload(FileInfo{
Size: 300,
IsPartial: true,
IsFinal: false,
PartialUploads: nil,
MetaData: make(map[string]string),
}).Return("foo", nil)
handler, _ := NewHandler(Config{
BasePath: "files",
DataStore: store,
})
(&httpTest{ (&httpTest{
Name: "Successful POST request",
Method: "POST", Method: "POST",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
@ -61,9 +53,19 @@ func TestConcatPartial(t *testing.T) {
}, },
Code: http.StatusCreated, Code: http.StatusCreated,
}).Run(handler, t) }).Run(handler, t)
})
SubTest(t, "Status", func(t *testing.T, store *MockFullDataStore) {
store.EXPECT().GetInfo("foo").Return(FileInfo{
IsPartial: true,
}, nil)
handler, _ := NewHandler(Config{
BasePath: "files",
DataStore: store,
})
(&httpTest{ (&httpTest{
Name: "Successful HEAD request",
Method: "HEAD", Method: "HEAD",
URL: "foo", URL: "foo",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
@ -74,66 +76,37 @@ func TestConcatPartial(t *testing.T) {
"Upload-Concat": "partial", "Upload-Concat": "partial",
}, },
}).Run(handler, t) }).Run(handler, t)
} })
})
type concatFinalStore struct { SubTest(t, "Final", func(t *testing.T, store *MockFullDataStore) {
t *assert.Assertions SubTest(t, "Create", func(t *testing.T, store *MockFullDataStore) {
zeroStore a := assert.New(t)
}
func (s concatFinalStore) NewUpload(info FileInfo) (string, error) { gomock.InOrder(
s.t.False(info.IsPartial) store.EXPECT().GetInfo("a").Return(FileInfo{
s.t.True(info.IsFinal)
s.t.Equal([]string{"a", "b"}, info.PartialUploads)
return "foo", nil
}
func (s concatFinalStore) GetInfo(id string) (FileInfo, error) {
if id == "a" || id == "b" {
return FileInfo{
IsPartial: true, IsPartial: true,
Size: 5, Size: 5,
Offset: 5, Offset: 5,
}, nil }, nil),
} store.EXPECT().GetInfo("b").Return(FileInfo{
if id == "c" {
return FileInfo{
IsPartial: true, IsPartial: true,
Size: 5, Size: 5,
Offset: 3, Offset: 5,
}, nil }, nil),
} store.EXPECT().NewUpload(FileInfo{
Size: 10,
if id == "foo" { IsPartial: false,
return FileInfo{
IsFinal: true, IsFinal: true,
PartialUploads: []string{"a", "b"}, PartialUploads: []string{"a", "b"},
Size: 10, MetaData: make(map[string]string),
Offset: 10, }).Return("foo", nil),
}, nil store.EXPECT().ConcatUploads("foo", []string{"a", "b"}).Return(nil),
} )
return FileInfo{}, ErrNotFound
}
func (s concatFinalStore) ConcatUploads(id string, uploads []string) error {
s.t.Equal("foo", id)
s.t.Equal([]string{"a", "b"}, uploads)
return nil
}
func TestConcatFinal(t *testing.T) {
a := assert.New(t)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
MaxSize: 400,
BasePath: "files", BasePath: "files",
DataStore: concatFinalStore{ DataStore: store,
t: a,
},
NotifyCompleteUploads: true, NotifyCompleteUploads: true,
}) })
@ -141,7 +114,6 @@ func TestConcatFinal(t *testing.T) {
handler.CompleteUploads = c handler.CompleteUploads = c
(&httpTest{ (&httpTest{
Name: "Successful POST request",
Method: "POST", Method: "POST",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
@ -152,14 +124,27 @@ func TestConcatFinal(t *testing.T) {
info := <-c info := <-c
a.Equal("foo", info.ID) a.Equal("foo", info.ID)
a.Equal(int64(10), info.Size) a.EqualValues(10, info.Size)
a.Equal(int64(10), info.Offset) a.EqualValues(10, info.Offset)
a.False(info.IsPartial) a.False(info.IsPartial)
a.True(info.IsFinal) a.True(info.IsFinal)
a.Equal([]string{"a", "b"}, info.PartialUploads) a.Equal([]string{"a", "b"}, info.PartialUploads)
})
SubTest(t, "Status", func(t *testing.T, store *MockFullDataStore) {
store.EXPECT().GetInfo("foo").Return(FileInfo{
IsFinal: true,
PartialUploads: []string{"a", "b"},
Size: 10,
Offset: 10,
}, nil)
handler, _ := NewHandler(Config{
BasePath: "files",
DataStore: store,
})
(&httpTest{ (&httpTest{
Name: "Successful HEAD request",
Method: "HEAD", Method: "HEAD",
URL: "foo", URL: "foo",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
@ -172,9 +157,23 @@ func TestConcatFinal(t *testing.T) {
"Upload-Offset": "10", "Upload-Offset": "10",
}, },
}).Run(handler, t) }).Run(handler, t)
})
SubTest(t, "CreateWithUnfinishedFail", func(t *testing.T, store *MockFullDataStore) {
// This upload is still unfinished (mismatching offset and size) and
// will therefore cause the POST request to fail.
store.EXPECT().GetInfo("c").Return(FileInfo{
IsPartial: true,
Size: 5,
Offset: 3,
}, nil)
handler, _ := NewHandler(Config{
BasePath: "files",
DataStore: store,
})
(&httpTest{ (&httpTest{
Name: "Concatenating non finished upload (id: c)",
Method: "POST", Method: "POST",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
@ -182,22 +181,28 @@ func TestConcatFinal(t *testing.T) {
}, },
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
}).Run(handler, t) }).Run(handler, t)
})
handler, _ = NewHandler(Config{ SubTest(t, "CreateExceedingMaxSizeFail", func(t *testing.T, store *MockFullDataStore) {
MaxSize: 9, store.EXPECT().GetInfo("huge").Return(FileInfo{
Size: 1000,
Offset: 1000,
}, nil)
handler, _ := NewHandler(Config{
MaxSize: 100,
BasePath: "files", BasePath: "files",
DataStore: concatFinalStore{ DataStore: store,
t: assert.New(t),
},
}) })
(&httpTest{ (&httpTest{
Name: "Exceeding MaxSize",
Method: "POST", Method: "POST",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
"Upload-Concat": "final; http://tus.io/files/a /files/b/", "Upload-Concat": "final; /files/huge",
}, },
Code: http.StatusRequestEntityTooLarge, Code: http.StatusRequestEntityTooLarge,
}).Run(handler, t) }).Run(handler, t)
})
})
} }

View File

@ -1,37 +1,14 @@
package tusd_test package tusd_test
import ( import (
"io"
"net/http" "net/http"
"os"
"strings" "strings"
"testing" "testing"
"github.com/golang/mock/gomock"
. "github.com/tus/tusd" . "github.com/tus/tusd"
) )
type getStore struct {
zeroStore
}
func (s getStore) GetInfo(id string) (FileInfo, error) {
if id != "yes" {
return FileInfo{}, os.ErrNotExist
}
return FileInfo{
Offset: 5,
Size: 20,
MetaData: map[string]string{
"filename": "file.jpg\"evil",
},
}, nil
}
func (s getStore) GetReader(id string) (io.Reader, error) {
return reader, nil
}
type closingStringReader struct { type closingStringReader struct {
*strings.Reader *strings.Reader
closed bool closed bool
@ -47,12 +24,23 @@ var reader = &closingStringReader{
} }
func TestGet(t *testing.T) { func TestGet(t *testing.T) {
SubTest(t, "Successful download", func(t *testing.T, store *MockFullDataStore) {
gomock.InOrder(
store.EXPECT().GetInfo("yes").Return(FileInfo{
Offset: 5,
Size: 20,
MetaData: map[string]string{
"filename": "file.jpg\"evil",
},
}, nil),
store.EXPECT().GetReader("yes").Return(reader, nil),
)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
DataStore: getStore{}, DataStore: store,
}) })
(&httpTest{ (&httpTest{
Name: "Successful download",
Method: "GET", Method: "GET",
URL: "yes", URL: "yes",
Code: http.StatusOK, Code: http.StatusOK,
@ -66,4 +54,5 @@ func TestGet(t *testing.T) {
if !reader.closed { if !reader.closed {
t.Error("expected reader to be closed") t.Error("expected reader to be closed")
} }
})
} }

96
handler_mock_test.go Normal file
View File

@ -0,0 +1,96 @@
// Automatically generated by MockGen. DO NOT EDIT!
// Source: handler_test.go
package tusd_test
import (
io "io"
gomock "github.com/golang/mock/gomock"
tusd "github.com/tus/tusd"
)
// Mock of FullDataStore interface
type MockFullDataStore struct {
ctrl *gomock.Controller
recorder *_MockFullDataStoreRecorder
}
// Recorder for MockFullDataStore (not exported)
type _MockFullDataStoreRecorder struct {
mock *MockFullDataStore
}
func NewMockFullDataStore(ctrl *gomock.Controller) *MockFullDataStore {
mock := &MockFullDataStore{ctrl: ctrl}
mock.recorder = &_MockFullDataStoreRecorder{mock}
return mock
}
func (_m *MockFullDataStore) EXPECT() *_MockFullDataStoreRecorder {
return _m.recorder
}
func (_m *MockFullDataStore) NewUpload(info tusd.FileInfo) (string, error) {
ret := _m.ctrl.Call(_m, "NewUpload", info)
ret0, _ := ret[0].(string)
ret1, _ := ret[1].(error)
return ret0, ret1
}
func (_mr *_MockFullDataStoreRecorder) NewUpload(arg0 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "NewUpload", arg0)
}
func (_m *MockFullDataStore) WriteChunk(id string, offset int64, src io.Reader) (int64, error) {
ret := _m.ctrl.Call(_m, "WriteChunk", id, offset, src)
ret0, _ := ret[0].(int64)
ret1, _ := ret[1].(error)
return ret0, ret1
}
func (_mr *_MockFullDataStoreRecorder) WriteChunk(arg0, arg1, arg2 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteChunk", arg0, arg1, arg2)
}
func (_m *MockFullDataStore) GetInfo(id string) (tusd.FileInfo, error) {
ret := _m.ctrl.Call(_m, "GetInfo", id)
ret0, _ := ret[0].(tusd.FileInfo)
ret1, _ := ret[1].(error)
return ret0, ret1
}
func (_mr *_MockFullDataStoreRecorder) GetInfo(arg0 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "GetInfo", arg0)
}
func (_m *MockFullDataStore) Terminate(id string) error {
ret := _m.ctrl.Call(_m, "Terminate", id)
ret0, _ := ret[0].(error)
return ret0
}
func (_mr *_MockFullDataStoreRecorder) Terminate(arg0 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "Terminate", arg0)
}
func (_m *MockFullDataStore) ConcatUploads(destination string, partialUploads []string) error {
ret := _m.ctrl.Call(_m, "ConcatUploads", destination, partialUploads)
ret0, _ := ret[0].(error)
return ret0
}
func (_mr *_MockFullDataStoreRecorder) ConcatUploads(arg0, arg1 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "ConcatUploads", arg0, arg1)
}
func (_m *MockFullDataStore) GetReader(id string) (io.Reader, error) {
ret := _m.ctrl.Call(_m, "GetReader", id)
ret0, _ := ret[0].(io.Reader)
ret1, _ := ret[1].(error)
return ret0, ret1
}
func (_mr *_MockFullDataStoreRecorder) GetReader(arg0 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "GetReader", arg0)
}

View File

@ -1,15 +1,18 @@
package tusd_test package tusd_test
import ( import (
"fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "reflect"
"strings" "strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/golang/mock/gomock"
"github.com/tus/tusd"
. "github.com/tus/tusd" . "github.com/tus/tusd"
) )
@ -26,6 +29,13 @@ func (store zeroStore) GetInfo(id string) (FileInfo, error) {
return FileInfo{}, nil return FileInfo{}, nil
} }
type FullDataStore interface {
tusd.DataStore
tusd.TerminaterDataStore
tusd.ConcaterDataStore
tusd.GetReaderDataStore
}
type httpTest struct { type httpTest struct {
Name string Name string
@ -41,8 +51,6 @@ type httpTest struct {
} }
func (test *httpTest) Run(handler http.Handler, t *testing.T) *httptest.ResponseRecorder { func (test *httpTest) Run(handler http.Handler, t *testing.T) *httptest.ResponseRecorder {
t.Logf("'%s' in %s", test.Name, assert.CallerInfo()[1])
req, _ := http.NewRequest(test.Method, test.URL, test.ReqBody) req, _ := http.NewRequest(test.Method, test.URL, test.ReqBody)
// Add headers // Add headers
@ -73,33 +81,61 @@ func (test *httpTest) Run(handler http.Handler, t *testing.T) *httptest.Response
return w return w
} }
type methodOverrideStore struct { func SubTest(t *testing.T, name string, runTest func(*testing.T, *MockFullDataStore)) {
zeroStore t.Run(name, func(subT *testing.T) {
t *testing.T //subT.Parallel()
called bool
ctrl := gomock.NewController(subT)
defer ctrl.Finish()
store := NewMockFullDataStore(ctrl)
runTest(subT, store)
})
} }
func (s methodOverrideStore) GetInfo(id string) (FileInfo, error) { type ReaderMatcher struct {
if id != "yes" { expect string
return FileInfo{}, os.ErrNotExist
} }
return FileInfo{ func NewReaderMatcher(expect string) gomock.Matcher {
Offset: 5, return ReaderMatcher{
Size: 20, expect: expect,
}, nil }
} }
func (s *methodOverrideStore) WriteChunk(id string, offset int64, src io.Reader) (int64, error) { func (m ReaderMatcher) Matches(x interface{}) bool {
s.called = true input, ok := x.(io.Reader)
if !ok {
return false
}
return 5, nil 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) { func TestMethodOverride(t *testing.T) {
store := &methodOverrideStore{ mockCtrl := gomock.NewController(t)
t: t, defer mockCtrl.Finish()
}
store := NewMockFullDataStore(mockCtrl)
store.EXPECT().GetInfo("yes").Return(FileInfo{
Offset: 5,
Size: 20,
}, nil)
store.EXPECT().WriteChunk("yes", int64(5), NewReaderMatcher("hello")).Return(int64(5), nil)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
DataStore: store, DataStore: store,
}) })
@ -120,8 +156,4 @@ func TestMethodOverride(t *testing.T) {
"Upload-Offset": "10", "Upload-Offset": "10",
}, },
}).Run(handler, t) }).Run(handler, t)
if !store.called {
t.Fatal("WriteChunk implementation not called")
}
} }

View File

@ -8,29 +8,19 @@ import (
. "github.com/tus/tusd" . "github.com/tus/tusd"
) )
type headStore struct { func TestHead(t *testing.T) {
zeroStore SubTest(t, "Successful HEAD request", func(t *testing.T, store *MockFullDataStore) {
} store.EXPECT().GetInfo("yes").Return(FileInfo{
func (s headStore) GetInfo(id string) (FileInfo, error) {
if id != "yes" {
return FileInfo{}, os.ErrNotExist
}
return FileInfo{
Offset: 11, Offset: 11,
Size: 44, Size: 44,
MetaData: map[string]string{ MetaData: map[string]string{
"name": "lunrjs.png", "name": "lunrjs.png",
"type": "image/png", "type": "image/png",
}, },
}, nil }, nil)
}
func TestHead(t *testing.T) {
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
BasePath: "https://buy.art/", DataStore: store,
DataStore: headStore{},
}) })
res := (&httpTest{ res := (&httpTest{
@ -54,9 +44,16 @@ func TestHead(t *testing.T) {
v != "type aW1hZ2UvcG5n,name bHVucmpzLnBuZw==" { v != "type aW1hZ2UvcG5n,name bHVucmpzLnBuZw==" {
t.Errorf("Expected valid metadata (got '%s')", v) t.Errorf("Expected valid metadata (got '%s')", v)
} }
})
res = (&httpTest{ SubTest(t, "Non-existing file", func(t *testing.T, store *MockFullDataStore) {
Name: "Non-existing file", store.EXPECT().GetInfo("no").Return(FileInfo{}, os.ErrNotExist)
handler, _ := NewHandler(Config{
DataStore: store,
})
res := (&httpTest{
Method: "HEAD", Method: "HEAD",
URL: "no", URL: "no",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
@ -71,4 +68,5 @@ func TestHead(t *testing.T) {
if string(res.Body.Bytes()) != "" { if string(res.Body.Bytes()) != "" {
t.Errorf("Expected empty body for failed HEAD request") t.Errorf("Expected empty body for failed HEAD request")
} }
})
} }

View File

@ -9,7 +9,7 @@ import (
func TestOptions(t *testing.T) { func TestOptions(t *testing.T) {
store := NewStoreComposer() store := NewStoreComposer()
store.UseCore(zeroStore{}) store.UseCore(NewMockFullDataStore(nil))
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
StoreComposer: store, StoreComposer: store,
MaxSize: 400, MaxSize: 400,

View File

@ -2,61 +2,31 @@ package tusd_test
import ( import (
"bytes" "bytes"
"io"
"io/ioutil"
"net/http" "net/http"
"strings" "strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/golang/mock/gomock"
. "github.com/tus/tusd" . "github.com/tus/tusd"
) )
type postStore struct {
t *assert.Assertions
zeroStore
}
func (s postStore) NewUpload(info FileInfo) (string, error) {
s.t.Equal(int64(300), info.Size)
metaData := info.MetaData
s.t.Equal(2, len(metaData))
s.t.Equal("hello", metaData["foo"])
s.t.Equal("world", metaData["bar"])
return "foo", nil
}
func (s postStore) WriteChunk(id string, offset int64, src io.Reader) (int64, error) {
s.t.Equal(int64(0), offset)
data, err := ioutil.ReadAll(src)
s.t.Nil(err)
s.t.Equal("hello", string(data))
return 5, nil
}
func (s postStore) ConcatUploads(id string, uploads []string) error {
s.t.True(false, "concatenation should not be attempted")
return nil
}
func TestPost(t *testing.T) { func TestPost(t *testing.T) {
a := assert.New(t) SubTest(t, "Create", func(t *testing.T, store *MockFullDataStore) {
store.EXPECT().NewUpload(FileInfo{
Size: 300,
MetaData: map[string]string{
"foo": "hello",
"bar": "world",
},
}).Return("foo", nil)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
MaxSize: 400, DataStore: store,
BasePath: "files", BasePath: "/files/",
DataStore: postStore{
t: a,
},
}) })
(&httpTest{ (&httpTest{
Name: "Successful request",
Method: "POST", Method: "POST",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
@ -68,6 +38,14 @@ func TestPost(t *testing.T) {
"Location": "http://tus.io/files/foo", "Location": "http://tus.io/files/foo",
}, },
}).Run(handler, t) }).Run(handler, t)
})
SubTest(t, "CreateExceedingMaxSizeFail", func(t *testing.T, store *MockFullDataStore) {
handler, _ := NewHandler(Config{
MaxSize: 400,
DataStore: store,
BasePath: "/files/",
})
(&httpTest{ (&httpTest{
Name: "Exceeding MaxSize", Name: "Exceeding MaxSize",
@ -79,14 +57,25 @@ func TestPost(t *testing.T) {
}, },
Code: http.StatusRequestEntityTooLarge, Code: http.StatusRequestEntityTooLarge,
}).Run(handler, t) }).Run(handler, t)
})
SubTest(t, "ForwardHeaders", func(t *testing.T, store *MockFullDataStore) {
SubTest(t, "IgnoreXForwarded", func(t *testing.T, store *MockFullDataStore) {
store.EXPECT().NewUpload(FileInfo{
Size: 300,
MetaData: map[string]string{},
}).Return("foo", nil)
handler, _ := NewHandler(Config{
DataStore: store,
BasePath: "/files/",
})
(&httpTest{ (&httpTest{
Name: "Ignore Forwarded headers",
Method: "POST", Method: "POST",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
"Upload-Length": "300", "Upload-Length": "300",
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
"X-Forwarded-Host": "foo.com", "X-Forwarded-Host": "foo.com",
"X-Forwarded-Proto": "https", "X-Forwarded-Proto": "https",
}, },
@ -95,23 +84,25 @@ func TestPost(t *testing.T) {
"Location": "http://tus.io/files/foo", "Location": "http://tus.io/files/foo",
}, },
}).Run(handler, t) }).Run(handler, t)
})
handler, _ = NewHandler(Config{ SubTest(t, "RespectXForwarded", func(t *testing.T, store *MockFullDataStore) {
MaxSize: 400, store.EXPECT().NewUpload(FileInfo{
BasePath: "files", Size: 300,
DataStore: postStore{ MetaData: map[string]string{},
t: a, }).Return("foo", nil)
},
handler, _ := NewHandler(Config{
DataStore: store,
BasePath: "/files/",
RespectForwardedHeaders: true, RespectForwardedHeaders: true,
}) })
(&httpTest{ (&httpTest{
Name: "Respect X-Forwarded-* headers",
Method: "POST", Method: "POST",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
"Upload-Length": "300", "Upload-Length": "300",
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
"X-Forwarded-Host": "foo.com", "X-Forwarded-Host": "foo.com",
"X-Forwarded-Proto": "https", "X-Forwarded-Proto": "https",
}, },
@ -120,14 +111,25 @@ func TestPost(t *testing.T) {
"Location": "https://foo.com/files/foo", "Location": "https://foo.com/files/foo",
}, },
}).Run(handler, t) }).Run(handler, t)
})
SubTest(t, "RespectForwarded", func(t *testing.T, store *MockFullDataStore) {
store.EXPECT().NewUpload(FileInfo{
Size: 300,
MetaData: map[string]string{},
}).Return("foo", nil)
handler, _ := NewHandler(Config{
DataStore: store,
BasePath: "/files/",
RespectForwardedHeaders: true,
})
(&httpTest{ (&httpTest{
Name: "Respect Forwarded headers",
Method: "POST", Method: "POST",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
"Upload-Length": "300", "Upload-Length": "300",
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
"X-Forwarded-Host": "bar.com", "X-Forwarded-Host": "bar.com",
"X-Forwarded-Proto": "http", "X-Forwarded-Proto": "http",
"Forwarded": "proto=https,host=foo.com", "Forwarded": "proto=https,host=foo.com",
@ -137,14 +139,25 @@ func TestPost(t *testing.T) {
"Location": "https://foo.com/files/foo", "Location": "https://foo.com/files/foo",
}, },
}).Run(handler, t) }).Run(handler, t)
})
SubTest(t, "FilterForwardedProtocol", func(t *testing.T, store *MockFullDataStore) {
store.EXPECT().NewUpload(FileInfo{
Size: 300,
MetaData: map[string]string{},
}).Return("foo", nil)
handler, _ := NewHandler(Config{
DataStore: store,
BasePath: "/files/",
RespectForwardedHeaders: true,
})
(&httpTest{ (&httpTest{
Name: "Filter forwarded protocol",
Method: "POST", Method: "POST",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
"Upload-Length": "300", "Upload-Length": "300",
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
"X-Forwarded-Proto": "aaa", "X-Forwarded-Proto": "aaa",
"Forwarded": "proto=bbb", "Forwarded": "proto=bbb",
}, },
@ -153,21 +166,28 @@ func TestPost(t *testing.T) {
"Location": "http://tus.io/files/foo", "Location": "http://tus.io/files/foo",
}, },
}).Run(handler, t) }).Run(handler, t)
} })
})
func TestPostWithUpload(t *testing.T) { SubTest(t, "WithUpload", func(t *testing.T, store *MockFullDataStore) {
a := assert.New(t) SubTest(t, "Create", func(t *testing.T, store *MockFullDataStore) {
gomock.InOrder(
store.EXPECT().NewUpload(FileInfo{
Size: 300,
MetaData: map[string]string{
"foo": "hello",
"bar": "world",
},
}).Return("foo", nil),
store.EXPECT().WriteChunk("foo", int64(0), NewReaderMatcher("hello")).Return(int64(5), nil),
)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
MaxSize: 400, DataStore: store,
BasePath: "files", BasePath: "/files/",
DataStore: postStore{
t: a,
},
}) })
(&httpTest{ (&httpTest{
Name: "Successful request",
Method: "POST", Method: "POST",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
@ -182,19 +202,41 @@ func TestPostWithUpload(t *testing.T) {
"Upload-Offset": "5", "Upload-Offset": "5",
}, },
}).Run(handler, t) }).Run(handler, t)
})
SubTest(t, "CreateExceedingUploadSize", func(t *testing.T, store *MockFullDataStore) {
store.EXPECT().NewUpload(FileInfo{
Size: 300,
MetaData: map[string]string{},
}).Return("foo", nil)
handler, _ := NewHandler(Config{
DataStore: store,
BasePath: "/files/",
})
(&httpTest{ (&httpTest{
Name: "Exceeding upload size",
Method: "POST", Method: "POST",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
"Upload-Length": "300", "Upload-Length": "300",
"Content-Type": "application/offset+octet-stream", "Content-Type": "application/offset+octet-stream",
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
}, },
ReqBody: bytes.NewReader(make([]byte, 400)), ReqBody: bytes.NewReader(make([]byte, 400)),
Code: http.StatusRequestEntityTooLarge, Code: http.StatusRequestEntityTooLarge,
}).Run(handler, t) }).Run(handler, t)
})
SubTest(t, "IncorrectContentType", func(t *testing.T, store *MockFullDataStore) {
store.EXPECT().NewUpload(FileInfo{
Size: 300,
MetaData: map[string]string{},
}).Return("foo", nil)
handler, _ := NewHandler(Config{
DataStore: store,
BasePath: "/files/",
})
(&httpTest{ (&httpTest{
Name: "Incorrect content type", Name: "Incorrect content type",
@ -202,7 +244,6 @@ func TestPostWithUpload(t *testing.T) {
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
"Upload-Length": "300", "Upload-Length": "300",
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
"Content-Type": "application/false", "Content-Type": "application/false",
}, },
ReqBody: strings.NewReader("hello"), ReqBody: strings.NewReader("hello"),
@ -212,18 +253,25 @@ func TestPostWithUpload(t *testing.T) {
"Upload-Offset": "", "Upload-Offset": "",
}, },
}).Run(handler, t) }).Run(handler, t)
})
SubTest(t, "UploadToFinalUpload", func(t *testing.T, store *MockFullDataStore) {
handler, _ := NewHandler(Config{
DataStore: store,
BasePath: "/files/",
})
(&httpTest{ (&httpTest{
Name: "Upload and final concatenation",
Method: "POST", Method: "POST",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0", "Tus-Resumable": "1.0.0",
"Upload-Length": "300", "Upload-Length": "300",
"Content-Type": "application/offset+octet-stream", "Content-Type": "application/offset+octet-stream",
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
"Upload-Concat": "final; http://tus.io/files/a http://tus.io/files/b", "Upload-Concat": "final; http://tus.io/files/a http://tus.io/files/b",
}, },
ReqBody: strings.NewReader("hello"), ReqBody: strings.NewReader("hello"),
Code: http.StatusForbidden, Code: http.StatusForbidden,
}).Run(handler, t) }).Run(handler, t)
})
})
} }

View File

@ -9,30 +9,34 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
type terminateStore struct {
t *testing.T
zeroStore
}
func (s terminateStore) GetInfo(id string) (FileInfo, error) {
return FileInfo{
ID: id,
Size: 10,
}, nil
}
func (s terminateStore) Terminate(id string) error {
if id != "foo" {
s.t.Fatal("unexpected id")
}
return nil
}
func TestTerminate(t *testing.T) { func TestTerminate(t *testing.T) {
SubTest(t, "ExtensionDiscovery", func(t *testing.T, store *MockFullDataStore) {
composer := NewStoreComposer()
composer.UseCore(store)
composer.UseTerminater(store)
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
DataStore: terminateStore{ StoreComposer: composer,
t: t, })
(&httpTest{
Method: "OPTIONS",
Code: http.StatusOK,
ResHeader: map[string]string{
"Tus-Extension": "creation,creation-with-upload,termination",
}, },
}).Run(handler, t)
})
SubTest(t, "Termination", func(t *testing.T, store *MockFullDataStore) {
store.EXPECT().GetInfo("foo").Return(FileInfo{
ID: "foo",
Size: 10,
}, nil)
store.EXPECT().Terminate("foo").Return(nil)
handler, _ := NewHandler(Config{
DataStore: store,
NotifyTerminatedUploads: true, NotifyTerminatedUploads: true,
}) })
@ -40,17 +44,6 @@ func TestTerminate(t *testing.T) {
handler.TerminatedUploads = c handler.TerminatedUploads = c
(&httpTest{ (&httpTest{
Name: "Successful OPTIONS request",
Method: "OPTIONS",
URL: "",
ResHeader: map[string]string{
"Tus-Extension": "creation,creation-with-upload,termination",
},
Code: http.StatusOK,
}).Run(handler, t)
(&httpTest{
Name: "Successful request",
Method: "DELETE", Method: "DELETE",
URL: "foo", URL: "foo",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
@ -64,15 +57,17 @@ func TestTerminate(t *testing.T) {
a := assert.New(t) a := assert.New(t)
a.Equal("foo", info.ID) a.Equal("foo", info.ID)
a.Equal(int64(10), info.Size) a.Equal(int64(10), info.Size)
} })
SubTest(t, "NotProvided", func(t *testing.T, store *MockFullDataStore) {
composer := NewStoreComposer()
composer.UseCore(store)
func TestTerminateNotImplemented(t *testing.T) {
handler, _ := NewHandler(Config{ handler, _ := NewHandler(Config{
DataStore: zeroStore{}, StoreComposer: composer,
}) })
(&httpTest{ (&httpTest{
Name: "TerminaterDataStore not implemented",
Method: "DELETE", Method: "DELETE",
URL: "foo", URL: "foo",
ReqHeader: map[string]string{ ReqHeader: map[string]string{
@ -80,4 +75,5 @@ func TestTerminateNotImplemented(t *testing.T) {
}, },
Code: http.StatusMethodNotAllowed, Code: http.StatusMethodNotAllowed,
}).Run(handler, t) }).Run(handler, t)
})
} }