Use mocks generated by gomock for running tests
This commit is contained in:
parent
4d595672f6
commit
3f0faedfd8
353
concat_test.go
353
concat_test.go
|
@ -4,200 +4,205 @@ 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) {
|
handler, _ := NewHandler(Config{
|
||||||
s.t.True(info.IsPartial)
|
StoreComposer: composer,
|
||||||
s.t.False(info.IsFinal)
|
})
|
||||||
s.t.Nil(info.PartialUploads)
|
|
||||||
|
|
||||||
return "foo", nil
|
(&httpTest{
|
||||||
}
|
Method: "OPTIONS",
|
||||||
|
Code: http.StatusOK,
|
||||||
func (s concatPartialStore) GetInfo(id string) (FileInfo, error) {
|
ResHeader: map[string]string{
|
||||||
return FileInfo{
|
"Tus-Extension": "creation,creation-with-upload,concatenation",
|
||||||
IsPartial: true,
|
},
|
||||||
}, nil
|
}).Run(handler, t)
|
||||||
}
|
|
||||||
|
|
||||||
func (s concatPartialStore) ConcatUploads(id string, uploads []string) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestConcatPartial(t *testing.T) {
|
|
||||||
handler, _ := NewHandler(Config{
|
|
||||||
MaxSize: 400,
|
|
||||||
BasePath: "files",
|
|
||||||
DataStore: concatPartialStore{
|
|
||||||
t: assert.New(t),
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
(&httpTest{
|
SubTest(t, "Partial", func(t *testing.T, store *MockFullDataStore) {
|
||||||
Name: "Successful OPTIONS request",
|
SubTest(t, "Create", func(t *testing.T, store *MockFullDataStore) {
|
||||||
Method: "OPTIONS",
|
store.EXPECT().NewUpload(FileInfo{
|
||||||
URL: "",
|
Size: 300,
|
||||||
ResHeader: map[string]string{
|
IsPartial: true,
|
||||||
"Tus-Extension": "creation,creation-with-upload,concatenation",
|
IsFinal: false,
|
||||||
},
|
PartialUploads: nil,
|
||||||
Code: http.StatusOK,
|
MetaData: make(map[string]string),
|
||||||
}).Run(handler, t)
|
}).Return("foo", nil)
|
||||||
|
|
||||||
(&httpTest{
|
handler, _ := NewHandler(Config{
|
||||||
Name: "Successful POST request",
|
BasePath: "files",
|
||||||
Method: "POST",
|
DataStore: store,
|
||||||
ReqHeader: map[string]string{
|
})
|
||||||
"Tus-Resumable": "1.0.0",
|
|
||||||
"Upload-Length": "300",
|
|
||||||
"Upload-Concat": "partial",
|
|
||||||
},
|
|
||||||
Code: http.StatusCreated,
|
|
||||||
}).Run(handler, t)
|
|
||||||
|
|
||||||
(&httpTest{
|
(&httpTest{
|
||||||
Name: "Successful HEAD request",
|
Method: "POST",
|
||||||
Method: "HEAD",
|
ReqHeader: map[string]string{
|
||||||
URL: "foo",
|
"Tus-Resumable": "1.0.0",
|
||||||
ReqHeader: map[string]string{
|
"Upload-Length": "300",
|
||||||
"Tus-Resumable": "1.0.0",
|
"Upload-Concat": "partial",
|
||||||
},
|
},
|
||||||
Code: http.StatusOK,
|
Code: http.StatusCreated,
|
||||||
ResHeader: map[string]string{
|
}).Run(handler, t)
|
||||||
"Upload-Concat": "partial",
|
})
|
||||||
},
|
|
||||||
}).Run(handler, t)
|
|
||||||
}
|
|
||||||
|
|
||||||
type concatFinalStore struct {
|
SubTest(t, "Status", func(t *testing.T, store *MockFullDataStore) {
|
||||||
t *assert.Assertions
|
store.EXPECT().GetInfo("foo").Return(FileInfo{
|
||||||
zeroStore
|
IsPartial: true,
|
||||||
}
|
}, nil)
|
||||||
|
|
||||||
func (s concatFinalStore) NewUpload(info FileInfo) (string, error) {
|
handler, _ := NewHandler(Config{
|
||||||
s.t.False(info.IsPartial)
|
BasePath: "files",
|
||||||
s.t.True(info.IsFinal)
|
DataStore: store,
|
||||||
s.t.Equal([]string{"a", "b"}, info.PartialUploads)
|
})
|
||||||
|
|
||||||
return "foo", nil
|
(&httpTest{
|
||||||
}
|
Method: "HEAD",
|
||||||
|
URL: "foo",
|
||||||
func (s concatFinalStore) GetInfo(id string) (FileInfo, error) {
|
ReqHeader: map[string]string{
|
||||||
if id == "a" || id == "b" {
|
"Tus-Resumable": "1.0.0",
|
||||||
return FileInfo{
|
},
|
||||||
IsPartial: true,
|
Code: http.StatusOK,
|
||||||
Size: 5,
|
ResHeader: map[string]string{
|
||||||
Offset: 5,
|
"Upload-Concat": "partial",
|
||||||
}, nil
|
},
|
||||||
}
|
}).Run(handler, t)
|
||||||
|
})
|
||||||
if id == "c" {
|
|
||||||
return FileInfo{
|
|
||||||
IsPartial: true,
|
|
||||||
Size: 5,
|
|
||||||
Offset: 3,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if id == "foo" {
|
|
||||||
return FileInfo{
|
|
||||||
IsFinal: true,
|
|
||||||
PartialUploads: []string{"a", "b"},
|
|
||||||
Size: 10,
|
|
||||||
Offset: 10,
|
|
||||||
}, 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{
|
|
||||||
MaxSize: 400,
|
|
||||||
BasePath: "files",
|
|
||||||
DataStore: concatFinalStore{
|
|
||||||
t: a,
|
|
||||||
},
|
|
||||||
NotifyCompleteUploads: true,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
c := make(chan FileInfo, 1)
|
SubTest(t, "Final", func(t *testing.T, store *MockFullDataStore) {
|
||||||
handler.CompleteUploads = c
|
SubTest(t, "Create", func(t *testing.T, store *MockFullDataStore) {
|
||||||
|
a := assert.New(t)
|
||||||
|
|
||||||
(&httpTest{
|
gomock.InOrder(
|
||||||
Name: "Successful POST request",
|
store.EXPECT().GetInfo("a").Return(FileInfo{
|
||||||
Method: "POST",
|
IsPartial: true,
|
||||||
ReqHeader: map[string]string{
|
Size: 5,
|
||||||
"Tus-Resumable": "1.0.0",
|
Offset: 5,
|
||||||
"Upload-Concat": "final; http://tus.io/files/a /files/b/",
|
}, nil),
|
||||||
},
|
store.EXPECT().GetInfo("b").Return(FileInfo{
|
||||||
Code: http.StatusCreated,
|
IsPartial: true,
|
||||||
}).Run(handler, t)
|
Size: 5,
|
||||||
|
Offset: 5,
|
||||||
|
}, nil),
|
||||||
|
store.EXPECT().NewUpload(FileInfo{
|
||||||
|
Size: 10,
|
||||||
|
IsPartial: false,
|
||||||
|
IsFinal: true,
|
||||||
|
PartialUploads: []string{"a", "b"},
|
||||||
|
MetaData: make(map[string]string),
|
||||||
|
}).Return("foo", nil),
|
||||||
|
store.EXPECT().ConcatUploads("foo", []string{"a", "b"}).Return(nil),
|
||||||
|
)
|
||||||
|
|
||||||
info := <-c
|
handler, _ := NewHandler(Config{
|
||||||
a.Equal("foo", info.ID)
|
BasePath: "files",
|
||||||
a.Equal(int64(10), info.Size)
|
DataStore: store,
|
||||||
a.Equal(int64(10), info.Offset)
|
NotifyCompleteUploads: true,
|
||||||
a.False(info.IsPartial)
|
})
|
||||||
a.True(info.IsFinal)
|
|
||||||
a.Equal([]string{"a", "b"}, info.PartialUploads)
|
|
||||||
|
|
||||||
(&httpTest{
|
c := make(chan FileInfo, 1)
|
||||||
Name: "Successful HEAD request",
|
handler.CompleteUploads = c
|
||||||
Method: "HEAD",
|
|
||||||
URL: "foo",
|
|
||||||
ReqHeader: map[string]string{
|
|
||||||
"Tus-Resumable": "1.0.0",
|
|
||||||
},
|
|
||||||
Code: http.StatusOK,
|
|
||||||
ResHeader: map[string]string{
|
|
||||||
"Upload-Concat": "final; http://tus.io/files/a http://tus.io/files/b",
|
|
||||||
"Upload-Length": "10",
|
|
||||||
"Upload-Offset": "10",
|
|
||||||
},
|
|
||||||
}).Run(handler, t)
|
|
||||||
|
|
||||||
(&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",
|
"Upload-Concat": "final; http://tus.io/files/a /files/b/",
|
||||||
"Upload-Concat": "final; http://tus.io/files/c",
|
},
|
||||||
},
|
Code: http.StatusCreated,
|
||||||
Code: http.StatusBadRequest,
|
}).Run(handler, t)
|
||||||
}).Run(handler, t)
|
|
||||||
|
|
||||||
handler, _ = NewHandler(Config{
|
info := <-c
|
||||||
MaxSize: 9,
|
a.Equal("foo", info.ID)
|
||||||
BasePath: "files",
|
a.EqualValues(10, info.Size)
|
||||||
DataStore: concatFinalStore{
|
a.EqualValues(10, info.Offset)
|
||||||
t: assert.New(t),
|
a.False(info.IsPartial)
|
||||||
},
|
a.True(info.IsFinal)
|
||||||
|
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{
|
||||||
|
Method: "HEAD",
|
||||||
|
URL: "foo",
|
||||||
|
ReqHeader: map[string]string{
|
||||||
|
"Tus-Resumable": "1.0.0",
|
||||||
|
},
|
||||||
|
Code: http.StatusOK,
|
||||||
|
ResHeader: map[string]string{
|
||||||
|
"Upload-Concat": "final; http://tus.io/files/a http://tus.io/files/b",
|
||||||
|
"Upload-Length": "10",
|
||||||
|
"Upload-Offset": "10",
|
||||||
|
},
|
||||||
|
}).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{
|
||||||
|
Method: "POST",
|
||||||
|
ReqHeader: map[string]string{
|
||||||
|
"Tus-Resumable": "1.0.0",
|
||||||
|
"Upload-Concat": "final; http://tus.io/files/c",
|
||||||
|
},
|
||||||
|
Code: http.StatusBadRequest,
|
||||||
|
}).Run(handler, t)
|
||||||
|
})
|
||||||
|
|
||||||
|
SubTest(t, "CreateExceedingMaxSizeFail", func(t *testing.T, store *MockFullDataStore) {
|
||||||
|
store.EXPECT().GetInfo("huge").Return(FileInfo{
|
||||||
|
Size: 1000,
|
||||||
|
Offset: 1000,
|
||||||
|
}, nil)
|
||||||
|
|
||||||
|
handler, _ := NewHandler(Config{
|
||||||
|
MaxSize: 100,
|
||||||
|
BasePath: "files",
|
||||||
|
DataStore: store,
|
||||||
|
})
|
||||||
|
|
||||||
|
(&httpTest{
|
||||||
|
Method: "POST",
|
||||||
|
ReqHeader: map[string]string{
|
||||||
|
"Tus-Resumable": "1.0.0",
|
||||||
|
"Upload-Concat": "final; /files/huge",
|
||||||
|
},
|
||||||
|
Code: http.StatusRequestEntityTooLarge,
|
||||||
|
}).Run(handler, t)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
(&httpTest{
|
|
||||||
Name: "Exceeding MaxSize",
|
|
||||||
Method: "POST",
|
|
||||||
ReqHeader: map[string]string{
|
|
||||||
"Tus-Resumable": "1.0.0",
|
|
||||||
"Upload-Concat": "final; http://tus.io/files/a /files/b/",
|
|
||||||
},
|
|
||||||
Code: http.StatusRequestEntityTooLarge,
|
|
||||||
}).Run(handler, t)
|
|
||||||
}
|
}
|
||||||
|
|
73
get_test.go
73
get_test.go
|
@ -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,23 +24,35 @@ var reader = &closingStringReader{
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGet(t *testing.T) {
|
func TestGet(t *testing.T) {
|
||||||
handler, _ := NewHandler(Config{
|
SubTest(t, "Successful download", func(t *testing.T, store *MockFullDataStore) {
|
||||||
DataStore: getStore{},
|
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{
|
||||||
|
DataStore: store,
|
||||||
|
})
|
||||||
|
|
||||||
|
(&httpTest{
|
||||||
|
Method: "GET",
|
||||||
|
URL: "yes",
|
||||||
|
Code: http.StatusOK,
|
||||||
|
ResBody: "hello",
|
||||||
|
ResHeader: map[string]string{
|
||||||
|
"Content-Length": "5",
|
||||||
|
"Content-Disposition": `inline;filename="file.jpg\"evil"`,
|
||||||
|
},
|
||||||
|
}).Run(handler, t)
|
||||||
|
|
||||||
|
if !reader.closed {
|
||||||
|
t.Error("expected reader to be closed")
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
(&httpTest{
|
|
||||||
Name: "Successful download",
|
|
||||||
Method: "GET",
|
|
||||||
URL: "yes",
|
|
||||||
Code: http.StatusOK,
|
|
||||||
ResBody: "hello",
|
|
||||||
ResHeader: map[string]string{
|
|
||||||
"Content-Length": "5",
|
|
||||||
"Content-Disposition": `inline;filename="file.jpg\"evil"`,
|
|
||||||
},
|
|
||||||
}).Run(handler, t)
|
|
||||||
|
|
||||||
if !reader.closed {
|
|
||||||
t.Error("expected reader to be closed")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
|
@ -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
|
}
|
||||||
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
return FileInfo{
|
bytes, err := ioutil.ReadAll(input)
|
||||||
Offset: 5,
|
if err != nil {
|
||||||
Size: 20,
|
panic(err)
|
||||||
}, nil
|
}
|
||||||
|
|
||||||
|
readStr := string(bytes)
|
||||||
|
return reflect.DeepEqual(m.expect, readStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *methodOverrideStore) WriteChunk(id string, offset int64, src io.Reader) (int64, error) {
|
func (m ReaderMatcher) String() string {
|
||||||
s.called = true
|
return fmt.Sprintf("reads to %s", m.expect)
|
||||||
|
|
||||||
return 5, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
112
head_test.go
112
head_test.go
|
@ -8,67 +8,65 @@ import (
|
||||||
. "github.com/tus/tusd"
|
. "github.com/tus/tusd"
|
||||||
)
|
)
|
||||||
|
|
||||||
type headStore struct {
|
|
||||||
zeroStore
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s headStore) GetInfo(id string) (FileInfo, error) {
|
|
||||||
if id != "yes" {
|
|
||||||
return FileInfo{}, os.ErrNotExist
|
|
||||||
}
|
|
||||||
|
|
||||||
return FileInfo{
|
|
||||||
Offset: 11,
|
|
||||||
Size: 44,
|
|
||||||
MetaData: map[string]string{
|
|
||||||
"name": "lunrjs.png",
|
|
||||||
"type": "image/png",
|
|
||||||
},
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHead(t *testing.T) {
|
func TestHead(t *testing.T) {
|
||||||
handler, _ := NewHandler(Config{
|
SubTest(t, "Successful HEAD request", func(t *testing.T, store *MockFullDataStore) {
|
||||||
BasePath: "https://buy.art/",
|
store.EXPECT().GetInfo("yes").Return(FileInfo{
|
||||||
DataStore: headStore{},
|
Offset: 11,
|
||||||
|
Size: 44,
|
||||||
|
MetaData: map[string]string{
|
||||||
|
"name": "lunrjs.png",
|
||||||
|
"type": "image/png",
|
||||||
|
},
|
||||||
|
}, nil)
|
||||||
|
|
||||||
|
handler, _ := NewHandler(Config{
|
||||||
|
DataStore: store,
|
||||||
|
})
|
||||||
|
|
||||||
|
res := (&httpTest{
|
||||||
|
Name: "Successful request",
|
||||||
|
Method: "HEAD",
|
||||||
|
URL: "yes",
|
||||||
|
ReqHeader: map[string]string{
|
||||||
|
"Tus-Resumable": "1.0.0",
|
||||||
|
},
|
||||||
|
Code: http.StatusOK,
|
||||||
|
ResHeader: map[string]string{
|
||||||
|
"Upload-Offset": "11",
|
||||||
|
"Upload-Length": "44",
|
||||||
|
"Cache-Control": "no-store",
|
||||||
|
},
|
||||||
|
}).Run(handler, t)
|
||||||
|
|
||||||
|
// Since the order of a map is not guaranteed in Go, we need to be prepared
|
||||||
|
// for the case, that the order of the metadata may have been changed
|
||||||
|
if v := res.Header().Get("Upload-Metadata"); v != "name bHVucmpzLnBuZw==,type aW1hZ2UvcG5n" &&
|
||||||
|
v != "type aW1hZ2UvcG5n,name bHVucmpzLnBuZw==" {
|
||||||
|
t.Errorf("Expected valid metadata (got '%s')", v)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
res := (&httpTest{
|
SubTest(t, "Non-existing file", func(t *testing.T, store *MockFullDataStore) {
|
||||||
Name: "Successful request",
|
store.EXPECT().GetInfo("no").Return(FileInfo{}, os.ErrNotExist)
|
||||||
Method: "HEAD",
|
|
||||||
URL: "yes",
|
|
||||||
ReqHeader: map[string]string{
|
|
||||||
"Tus-Resumable": "1.0.0",
|
|
||||||
},
|
|
||||||
Code: http.StatusOK,
|
|
||||||
ResHeader: map[string]string{
|
|
||||||
"Upload-Offset": "11",
|
|
||||||
"Upload-Length": "44",
|
|
||||||
"Cache-Control": "no-store",
|
|
||||||
},
|
|
||||||
}).Run(handler, t)
|
|
||||||
|
|
||||||
// Since the order of a map is not guaranteed in Go, we need to be prepared
|
handler, _ := NewHandler(Config{
|
||||||
// for the case, that the order of the metadata may have been changed
|
DataStore: store,
|
||||||
if v := res.Header().Get("Upload-Metadata"); v != "name bHVucmpzLnBuZw==,type aW1hZ2UvcG5n" &&
|
})
|
||||||
v != "type aW1hZ2UvcG5n,name bHVucmpzLnBuZw==" {
|
|
||||||
t.Errorf("Expected valid metadata (got '%s')", v)
|
|
||||||
}
|
|
||||||
|
|
||||||
res = (&httpTest{
|
res := (&httpTest{
|
||||||
Name: "Non-existing file",
|
Method: "HEAD",
|
||||||
Method: "HEAD",
|
URL: "no",
|
||||||
URL: "no",
|
ReqHeader: map[string]string{
|
||||||
ReqHeader: map[string]string{
|
"Tus-Resumable": "1.0.0",
|
||||||
"Tus-Resumable": "1.0.0",
|
},
|
||||||
},
|
Code: http.StatusNotFound,
|
||||||
Code: http.StatusNotFound,
|
ResHeader: map[string]string{
|
||||||
ResHeader: map[string]string{
|
"Content-Length": "0",
|
||||||
"Content-Length": "0",
|
},
|
||||||
},
|
}).Run(handler, t)
|
||||||
}).Run(handler, 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")
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,
|
||||||
|
|
462
post_test.go
462
post_test.go
|
@ -2,228 +2,276 @@ 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{
|
||||||
|
Method: "POST",
|
||||||
|
ReqHeader: map[string]string{
|
||||||
|
"Tus-Resumable": "1.0.0",
|
||||||
|
"Upload-Length": "300",
|
||||||
|
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
||||||
|
},
|
||||||
|
Code: http.StatusCreated,
|
||||||
|
ResHeader: map[string]string{
|
||||||
|
"Location": "http://tus.io/files/foo",
|
||||||
|
},
|
||||||
|
}).Run(handler, t)
|
||||||
})
|
})
|
||||||
|
|
||||||
(&httpTest{
|
SubTest(t, "CreateExceedingMaxSizeFail", func(t *testing.T, store *MockFullDataStore) {
|
||||||
Name: "Successful request",
|
handler, _ := NewHandler(Config{
|
||||||
Method: "POST",
|
MaxSize: 400,
|
||||||
ReqHeader: map[string]string{
|
DataStore: store,
|
||||||
"Tus-Resumable": "1.0.0",
|
BasePath: "/files/",
|
||||||
"Upload-Length": "300",
|
})
|
||||||
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
|
||||||
},
|
|
||||||
Code: http.StatusCreated,
|
|
||||||
ResHeader: map[string]string{
|
|
||||||
"Location": "http://tus.io/files/foo",
|
|
||||||
},
|
|
||||||
}).Run(handler, t)
|
|
||||||
|
|
||||||
(&httpTest{
|
(&httpTest{
|
||||||
Name: "Exceeding MaxSize",
|
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-Length": "500",
|
"Upload-Length": "500",
|
||||||
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
||||||
},
|
},
|
||||||
Code: http.StatusRequestEntityTooLarge,
|
Code: http.StatusRequestEntityTooLarge,
|
||||||
}).Run(handler, t)
|
}).Run(handler, t)
|
||||||
|
|
||||||
(&httpTest{
|
|
||||||
Name: "Ignore Forwarded headers",
|
|
||||||
Method: "POST",
|
|
||||||
ReqHeader: map[string]string{
|
|
||||||
"Tus-Resumable": "1.0.0",
|
|
||||||
"Upload-Length": "300",
|
|
||||||
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
|
||||||
"X-Forwarded-Host": "foo.com",
|
|
||||||
"X-Forwarded-Proto": "https",
|
|
||||||
},
|
|
||||||
Code: http.StatusCreated,
|
|
||||||
ResHeader: map[string]string{
|
|
||||||
"Location": "http://tus.io/files/foo",
|
|
||||||
},
|
|
||||||
}).Run(handler, t)
|
|
||||||
|
|
||||||
handler, _ = NewHandler(Config{
|
|
||||||
MaxSize: 400,
|
|
||||||
BasePath: "files",
|
|
||||||
DataStore: postStore{
|
|
||||||
t: a,
|
|
||||||
},
|
|
||||||
RespectForwardedHeaders: true,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
(&httpTest{
|
SubTest(t, "ForwardHeaders", func(t *testing.T, store *MockFullDataStore) {
|
||||||
Name: "Respect X-Forwarded-* headers",
|
SubTest(t, "IgnoreXForwarded", func(t *testing.T, store *MockFullDataStore) {
|
||||||
Method: "POST",
|
store.EXPECT().NewUpload(FileInfo{
|
||||||
ReqHeader: map[string]string{
|
Size: 300,
|
||||||
"Tus-Resumable": "1.0.0",
|
MetaData: map[string]string{},
|
||||||
"Upload-Length": "300",
|
}).Return("foo", nil)
|
||||||
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
|
||||||
"X-Forwarded-Host": "foo.com",
|
|
||||||
"X-Forwarded-Proto": "https",
|
|
||||||
},
|
|
||||||
Code: http.StatusCreated,
|
|
||||||
ResHeader: map[string]string{
|
|
||||||
"Location": "https://foo.com/files/foo",
|
|
||||||
},
|
|
||||||
}).Run(handler, t)
|
|
||||||
|
|
||||||
(&httpTest{
|
handler, _ := NewHandler(Config{
|
||||||
Name: "Respect Forwarded headers",
|
DataStore: store,
|
||||||
Method: "POST",
|
BasePath: "/files/",
|
||||||
ReqHeader: map[string]string{
|
})
|
||||||
"Tus-Resumable": "1.0.0",
|
|
||||||
"Upload-Length": "300",
|
|
||||||
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
|
||||||
"X-Forwarded-Host": "bar.com",
|
|
||||||
"X-Forwarded-Proto": "http",
|
|
||||||
"Forwarded": "proto=https,host=foo.com",
|
|
||||||
},
|
|
||||||
Code: http.StatusCreated,
|
|
||||||
ResHeader: map[string]string{
|
|
||||||
"Location": "https://foo.com/files/foo",
|
|
||||||
},
|
|
||||||
}).Run(handler, t)
|
|
||||||
|
|
||||||
(&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",
|
"X-Forwarded-Host": "foo.com",
|
||||||
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
"X-Forwarded-Proto": "https",
|
||||||
"X-Forwarded-Proto": "aaa",
|
},
|
||||||
"Forwarded": "proto=bbb",
|
Code: http.StatusCreated,
|
||||||
},
|
ResHeader: map[string]string{
|
||||||
Code: http.StatusCreated,
|
"Location": "http://tus.io/files/foo",
|
||||||
ResHeader: map[string]string{
|
},
|
||||||
"Location": "http://tus.io/files/foo",
|
}).Run(handler, t)
|
||||||
},
|
})
|
||||||
}).Run(handler, t)
|
|
||||||
}
|
SubTest(t, "RespectXForwarded", func(t *testing.T, store *MockFullDataStore) {
|
||||||
|
store.EXPECT().NewUpload(FileInfo{
|
||||||
func TestPostWithUpload(t *testing.T) {
|
Size: 300,
|
||||||
a := assert.New(t)
|
MetaData: map[string]string{},
|
||||||
|
}).Return("foo", nil)
|
||||||
handler, _ := NewHandler(Config{
|
|
||||||
MaxSize: 400,
|
handler, _ := NewHandler(Config{
|
||||||
BasePath: "files",
|
DataStore: store,
|
||||||
DataStore: postStore{
|
BasePath: "/files/",
|
||||||
t: a,
|
RespectForwardedHeaders: true,
|
||||||
},
|
})
|
||||||
})
|
|
||||||
|
(&httpTest{
|
||||||
(&httpTest{
|
Method: "POST",
|
||||||
Name: "Successful request",
|
ReqHeader: map[string]string{
|
||||||
Method: "POST",
|
"Tus-Resumable": "1.0.0",
|
||||||
ReqHeader: map[string]string{
|
"Upload-Length": "300",
|
||||||
"Tus-Resumable": "1.0.0",
|
"X-Forwarded-Host": "foo.com",
|
||||||
"Upload-Length": "300",
|
"X-Forwarded-Proto": "https",
|
||||||
"Content-Type": "application/offset+octet-stream",
|
},
|
||||||
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
Code: http.StatusCreated,
|
||||||
},
|
ResHeader: map[string]string{
|
||||||
ReqBody: strings.NewReader("hello"),
|
"Location": "https://foo.com/files/foo",
|
||||||
Code: http.StatusCreated,
|
},
|
||||||
ResHeader: map[string]string{
|
}).Run(handler, t)
|
||||||
"Location": "http://tus.io/files/foo",
|
})
|
||||||
"Upload-Offset": "5",
|
|
||||||
},
|
SubTest(t, "RespectForwarded", func(t *testing.T, store *MockFullDataStore) {
|
||||||
}).Run(handler, t)
|
store.EXPECT().NewUpload(FileInfo{
|
||||||
|
Size: 300,
|
||||||
(&httpTest{
|
MetaData: map[string]string{},
|
||||||
Name: "Exceeding upload size",
|
}).Return("foo", nil)
|
||||||
Method: "POST",
|
|
||||||
ReqHeader: map[string]string{
|
handler, _ := NewHandler(Config{
|
||||||
"Tus-Resumable": "1.0.0",
|
DataStore: store,
|
||||||
"Upload-Length": "300",
|
BasePath: "/files/",
|
||||||
"Content-Type": "application/offset+octet-stream",
|
RespectForwardedHeaders: true,
|
||||||
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
})
|
||||||
},
|
|
||||||
ReqBody: bytes.NewReader(make([]byte, 400)),
|
(&httpTest{
|
||||||
Code: http.StatusRequestEntityTooLarge,
|
Method: "POST",
|
||||||
}).Run(handler, t)
|
ReqHeader: map[string]string{
|
||||||
|
"Tus-Resumable": "1.0.0",
|
||||||
(&httpTest{
|
"Upload-Length": "300",
|
||||||
Name: "Incorrect content type",
|
"X-Forwarded-Host": "bar.com",
|
||||||
Method: "POST",
|
"X-Forwarded-Proto": "http",
|
||||||
ReqHeader: map[string]string{
|
"Forwarded": "proto=https,host=foo.com",
|
||||||
"Tus-Resumable": "1.0.0",
|
},
|
||||||
"Upload-Length": "300",
|
Code: http.StatusCreated,
|
||||||
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
ResHeader: map[string]string{
|
||||||
"Content-Type": "application/false",
|
"Location": "https://foo.com/files/foo",
|
||||||
},
|
},
|
||||||
ReqBody: strings.NewReader("hello"),
|
}).Run(handler, t)
|
||||||
Code: http.StatusCreated,
|
})
|
||||||
ResHeader: map[string]string{
|
|
||||||
"Location": "http://tus.io/files/foo",
|
SubTest(t, "FilterForwardedProtocol", func(t *testing.T, store *MockFullDataStore) {
|
||||||
"Upload-Offset": "",
|
store.EXPECT().NewUpload(FileInfo{
|
||||||
},
|
Size: 300,
|
||||||
}).Run(handler, t)
|
MetaData: map[string]string{},
|
||||||
|
}).Return("foo", nil)
|
||||||
(&httpTest{
|
|
||||||
Name: "Upload and final concatenation",
|
handler, _ := NewHandler(Config{
|
||||||
Method: "POST",
|
DataStore: store,
|
||||||
ReqHeader: map[string]string{
|
BasePath: "/files/",
|
||||||
"Tus-Resumable": "1.0.0",
|
RespectForwardedHeaders: true,
|
||||||
"Upload-Length": "300",
|
})
|
||||||
"Content-Type": "application/offset+octet-stream",
|
|
||||||
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
(&httpTest{
|
||||||
"Upload-Concat": "final; http://tus.io/files/a http://tus.io/files/b",
|
Method: "POST",
|
||||||
},
|
ReqHeader: map[string]string{
|
||||||
ReqBody: strings.NewReader("hello"),
|
"Tus-Resumable": "1.0.0",
|
||||||
Code: http.StatusForbidden,
|
"Upload-Length": "300",
|
||||||
}).Run(handler, t)
|
"X-Forwarded-Proto": "aaa",
|
||||||
|
"Forwarded": "proto=bbb",
|
||||||
|
},
|
||||||
|
Code: http.StatusCreated,
|
||||||
|
ResHeader: map[string]string{
|
||||||
|
"Location": "http://tus.io/files/foo",
|
||||||
|
},
|
||||||
|
}).Run(handler, t)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
SubTest(t, "WithUpload", func(t *testing.T, store *MockFullDataStore) {
|
||||||
|
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{
|
||||||
|
DataStore: store,
|
||||||
|
BasePath: "/files/",
|
||||||
|
})
|
||||||
|
|
||||||
|
(&httpTest{
|
||||||
|
Method: "POST",
|
||||||
|
ReqHeader: map[string]string{
|
||||||
|
"Tus-Resumable": "1.0.0",
|
||||||
|
"Upload-Length": "300",
|
||||||
|
"Content-Type": "application/offset+octet-stream",
|
||||||
|
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
||||||
|
},
|
||||||
|
ReqBody: strings.NewReader("hello"),
|
||||||
|
Code: http.StatusCreated,
|
||||||
|
ResHeader: map[string]string{
|
||||||
|
"Location": "http://tus.io/files/foo",
|
||||||
|
"Upload-Offset": "5",
|
||||||
|
},
|
||||||
|
}).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{
|
||||||
|
Method: "POST",
|
||||||
|
ReqHeader: map[string]string{
|
||||||
|
"Tus-Resumable": "1.0.0",
|
||||||
|
"Upload-Length": "300",
|
||||||
|
"Content-Type": "application/offset+octet-stream",
|
||||||
|
},
|
||||||
|
ReqBody: bytes.NewReader(make([]byte, 400)),
|
||||||
|
Code: http.StatusRequestEntityTooLarge,
|
||||||
|
}).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{
|
||||||
|
Name: "Incorrect content type",
|
||||||
|
Method: "POST",
|
||||||
|
ReqHeader: map[string]string{
|
||||||
|
"Tus-Resumable": "1.0.0",
|
||||||
|
"Upload-Length": "300",
|
||||||
|
"Content-Type": "application/false",
|
||||||
|
},
|
||||||
|
ReqBody: strings.NewReader("hello"),
|
||||||
|
Code: http.StatusCreated,
|
||||||
|
ResHeader: map[string]string{
|
||||||
|
"Location": "http://tus.io/files/foo",
|
||||||
|
"Upload-Offset": "",
|
||||||
|
},
|
||||||
|
}).Run(handler, t)
|
||||||
|
})
|
||||||
|
|
||||||
|
SubTest(t, "UploadToFinalUpload", func(t *testing.T, store *MockFullDataStore) {
|
||||||
|
handler, _ := NewHandler(Config{
|
||||||
|
DataStore: store,
|
||||||
|
BasePath: "/files/",
|
||||||
|
})
|
||||||
|
|
||||||
|
(&httpTest{
|
||||||
|
Method: "POST",
|
||||||
|
ReqHeader: map[string]string{
|
||||||
|
"Tus-Resumable": "1.0.0",
|
||||||
|
"Upload-Length": "300",
|
||||||
|
"Content-Type": "application/offset+octet-stream",
|
||||||
|
"Upload-Concat": "final; http://tus.io/files/a http://tus.io/files/b",
|
||||||
|
},
|
||||||
|
ReqBody: strings.NewReader("hello"),
|
||||||
|
Code: http.StatusForbidden,
|
||||||
|
}).Run(handler, t)
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,75 +9,71 @@ 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) {
|
||||||
handler, _ := NewHandler(Config{
|
SubTest(t, "ExtensionDiscovery", func(t *testing.T, store *MockFullDataStore) {
|
||||||
DataStore: terminateStore{
|
composer := NewStoreComposer()
|
||||||
t: t,
|
composer.UseCore(store)
|
||||||
},
|
composer.UseTerminater(store)
|
||||||
NotifyTerminatedUploads: true,
|
|
||||||
|
handler, _ := NewHandler(Config{
|
||||||
|
StoreComposer: composer,
|
||||||
|
})
|
||||||
|
|
||||||
|
(&httpTest{
|
||||||
|
Method: "OPTIONS",
|
||||||
|
Code: http.StatusOK,
|
||||||
|
ResHeader: map[string]string{
|
||||||
|
"Tus-Extension": "creation,creation-with-upload,termination",
|
||||||
|
},
|
||||||
|
}).Run(handler, t)
|
||||||
})
|
})
|
||||||
|
|
||||||
c := make(chan FileInfo, 1)
|
SubTest(t, "Termination", func(t *testing.T, store *MockFullDataStore) {
|
||||||
handler.TerminatedUploads = c
|
store.EXPECT().GetInfo("foo").Return(FileInfo{
|
||||||
|
ID: "foo",
|
||||||
|
Size: 10,
|
||||||
|
}, nil)
|
||||||
|
store.EXPECT().Terminate("foo").Return(nil)
|
||||||
|
|
||||||
(&httpTest{
|
handler, _ := NewHandler(Config{
|
||||||
Name: "Successful OPTIONS request",
|
DataStore: store,
|
||||||
Method: "OPTIONS",
|
NotifyTerminatedUploads: true,
|
||||||
URL: "",
|
})
|
||||||
ResHeader: map[string]string{
|
|
||||||
"Tus-Extension": "creation,creation-with-upload,termination",
|
|
||||||
},
|
|
||||||
Code: http.StatusOK,
|
|
||||||
}).Run(handler, t)
|
|
||||||
|
|
||||||
(&httpTest{
|
c := make(chan FileInfo, 1)
|
||||||
Name: "Successful request",
|
handler.TerminatedUploads = c
|
||||||
Method: "DELETE",
|
|
||||||
URL: "foo",
|
|
||||||
ReqHeader: map[string]string{
|
|
||||||
"Tus-Resumable": "1.0.0",
|
|
||||||
},
|
|
||||||
Code: http.StatusNoContent,
|
|
||||||
}).Run(handler, t)
|
|
||||||
|
|
||||||
info := <-c
|
(&httpTest{
|
||||||
|
Method: "DELETE",
|
||||||
|
URL: "foo",
|
||||||
|
ReqHeader: map[string]string{
|
||||||
|
"Tus-Resumable": "1.0.0",
|
||||||
|
},
|
||||||
|
Code: http.StatusNoContent,
|
||||||
|
}).Run(handler, t)
|
||||||
|
|
||||||
a := assert.New(t)
|
info := <-c
|
||||||
a.Equal("foo", info.ID)
|
|
||||||
a.Equal(int64(10), info.Size)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTerminateNotImplemented(t *testing.T) {
|
a := assert.New(t)
|
||||||
handler, _ := NewHandler(Config{
|
a.Equal("foo", info.ID)
|
||||||
DataStore: zeroStore{},
|
a.Equal(int64(10), info.Size)
|
||||||
})
|
})
|
||||||
|
|
||||||
(&httpTest{
|
SubTest(t, "NotProvided", func(t *testing.T, store *MockFullDataStore) {
|
||||||
Name: "TerminaterDataStore not implemented",
|
composer := NewStoreComposer()
|
||||||
Method: "DELETE",
|
composer.UseCore(store)
|
||||||
URL: "foo",
|
|
||||||
ReqHeader: map[string]string{
|
handler, _ := NewHandler(Config{
|
||||||
"Tus-Resumable": "1.0.0",
|
StoreComposer: composer,
|
||||||
},
|
})
|
||||||
Code: http.StatusMethodNotAllowed,
|
|
||||||
}).Run(handler, t)
|
(&httpTest{
|
||||||
|
Method: "DELETE",
|
||||||
|
URL: "foo",
|
||||||
|
ReqHeader: map[string]string{
|
||||||
|
"Tus-Resumable": "1.0.0",
|
||||||
|
},
|
||||||
|
Code: http.StatusMethodNotAllowed,
|
||||||
|
}).Run(handler, t)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue