2019-06-11 16:23:20 +00:00
|
|
|
package handler_test
|
2015-02-28 13:47:39 +00:00
|
|
|
|
|
|
|
import (
|
2019-09-15 11:43:59 +00:00
|
|
|
"context"
|
2015-02-28 13:47:39 +00:00
|
|
|
"net/http"
|
|
|
|
"testing"
|
2015-12-25 21:33:27 +00:00
|
|
|
|
2016-10-13 16:33:33 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
2019-06-11 16:23:20 +00:00
|
|
|
. "github.com/tus/tusd/pkg/handler"
|
2016-03-12 21:24:57 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2015-02-28 13:47:39 +00:00
|
|
|
)
|
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
func TestTerminate(t *testing.T) {
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "ExtensionDiscovery", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
|
|
|
composer = NewStoreComposer()
|
2016-10-13 09:33:20 +00:00
|
|
|
composer.UseCore(store)
|
|
|
|
composer.UseTerminater(store)
|
2016-03-12 21:24:57 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
handler, _ := NewHandler(Config{
|
|
|
|
StoreComposer: composer,
|
|
|
|
})
|
2015-02-28 13:47:39 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
(&httpTest{
|
|
|
|
Method: "OPTIONS",
|
|
|
|
Code: http.StatusOK,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Tus-Extension": "creation,creation-with-upload,termination",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
2015-02-28 13:47:39 +00:00
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "Termination", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2016-10-13 16:33:33 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
2019-09-12 10:37:43 +00:00
|
|
|
locker := NewMockFullLocker(ctrl)
|
|
|
|
lock := NewMockFullLock(ctrl)
|
2019-08-24 13:14:51 +00:00
|
|
|
upload := NewMockFullUpload(ctrl)
|
2016-10-13 16:33:33 +00:00
|
|
|
|
|
|
|
gomock.InOrder(
|
2019-09-12 10:37:43 +00:00
|
|
|
locker.EXPECT().NewLock("foo").Return(lock, nil),
|
|
|
|
lock.EXPECT().Lock().Return(nil),
|
2019-09-15 11:43:59 +00:00
|
|
|
store.EXPECT().GetUpload(context.Background(), "foo").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
|
2016-10-13 16:33:33 +00:00
|
|
|
ID: "foo",
|
|
|
|
Size: 10,
|
|
|
|
}, nil),
|
2019-08-24 13:14:51 +00:00
|
|
|
store.EXPECT().AsTerminatableUpload(upload).Return(upload),
|
2019-09-15 11:43:59 +00:00
|
|
|
upload.EXPECT().Terminate(context.Background()).Return(nil),
|
2019-09-12 10:37:43 +00:00
|
|
|
lock.EXPECT().Unlock().Return(nil),
|
2016-10-13 16:33:33 +00:00
|
|
|
)
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
composer = NewStoreComposer()
|
2016-10-13 16:33:33 +00:00
|
|
|
composer.UseCore(store)
|
|
|
|
composer.UseTerminater(store)
|
|
|
|
composer.UseLocker(locker)
|
2016-03-12 21:24:57 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
handler, _ := NewHandler(Config{
|
2016-10-13 16:33:33 +00:00
|
|
|
StoreComposer: composer,
|
2016-10-13 09:33:20 +00:00
|
|
|
NotifyTerminatedUploads: true,
|
|
|
|
})
|
2015-12-26 23:44:02 +00:00
|
|
|
|
2019-09-19 09:15:48 +00:00
|
|
|
c := make(chan HookEvent, 1)
|
2016-10-13 09:33:20 +00:00
|
|
|
handler.TerminatedUploads = c
|
2016-03-12 21:24:57 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
(&httpTest{
|
|
|
|
Method: "DELETE",
|
|
|
|
URL: "foo",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
},
|
|
|
|
Code: http.StatusNoContent,
|
|
|
|
}).Run(handler, t)
|
2016-03-12 21:24:57 +00:00
|
|
|
|
2019-09-19 09:15:48 +00:00
|
|
|
event := <-c
|
|
|
|
info := event.Upload
|
2015-12-26 23:44:02 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
a := assert.New(t)
|
|
|
|
a.Equal("foo", info.ID)
|
|
|
|
a.Equal(int64(10), info.Size)
|
2019-09-19 09:15:48 +00:00
|
|
|
|
|
|
|
req := event.HTTPRequest
|
|
|
|
a.Equal("DELETE", req.Method)
|
|
|
|
a.Equal("foo", req.URI)
|
2015-12-26 23:44:02 +00:00
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "NotProvided", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
|
|
|
composer = NewStoreComposer()
|
2016-10-13 09:33:20 +00:00
|
|
|
composer.UseCore(store)
|
|
|
|
|
2016-10-13 16:48:46 +00:00
|
|
|
handler, _ := NewUnroutedHandler(Config{
|
2016-10-13 09:33:20 +00:00
|
|
|
StoreComposer: composer,
|
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "DELETE",
|
|
|
|
URL: "foo",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
},
|
2016-10-13 16:48:46 +00:00
|
|
|
Code: http.StatusNotImplemented,
|
|
|
|
}).Run(http.HandlerFunc(handler.DelFile), t)
|
2016-10-13 09:33:20 +00:00
|
|
|
})
|
2015-12-26 23:44:02 +00:00
|
|
|
}
|