2019-06-11 16:23:20 +00:00
|
|
|
package handler_test
|
2015-02-09 18:37:06 +00:00
|
|
|
|
|
|
|
import (
|
2019-09-15 11:43:59 +00:00
|
|
|
"context"
|
2015-02-09 18:37:06 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"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"
|
2015-02-09 18:37:06 +00:00
|
|
|
)
|
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
func TestHead(t *testing.T) {
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "Status", 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-25 20:10:55 +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("yes").Return(lock, nil),
|
|
|
|
lock.EXPECT().Lock().Return(nil),
|
2019-09-15 11:43:59 +00:00
|
|
|
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
|
2016-10-13 16:33:33 +00:00
|
|
|
Offset: 11,
|
|
|
|
Size: 44,
|
|
|
|
MetaData: map[string]string{
|
2020-07-15 15:30:17 +00:00
|
|
|
"name": "lunrjs.png",
|
|
|
|
"empty": "",
|
2016-10-13 16:33:33 +00:00
|
|
|
},
|
|
|
|
}, 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.UseLocker(locker)
|
2015-02-09 18:37:06 +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
|
|
|
})
|
2015-02-09 18:37:06 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
res := (&httpTest{
|
|
|
|
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)
|
2015-02-09 18:37:06 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
// 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
|
2020-07-15 15:30:17 +00:00
|
|
|
if v := res.Header().Get("Upload-Metadata"); v != "name bHVucmpzLnBuZw==,empty " &&
|
|
|
|
v != "empty ,name bHVucmpzLnBuZw==" {
|
2016-10-13 09:33:20 +00:00
|
|
|
t.Errorf("Expected valid metadata (got '%s')", v)
|
|
|
|
}
|
2015-02-09 18:37:06 +00:00
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "UploadNotFoundFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2019-09-15 11:43:59 +00:00
|
|
|
store.EXPECT().GetUpload(context.Background(), "no").Return(nil, os.ErrNotExist)
|
2015-02-17 14:44:12 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2016-10-13 09:33:20 +00:00
|
|
|
})
|
2015-11-26 15:25:34 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
res := (&httpTest{
|
|
|
|
Method: "HEAD",
|
|
|
|
URL: "no",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
},
|
|
|
|
Code: http.StatusNotFound,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Content-Length": "0",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
2015-12-02 17:59:22 +00:00
|
|
|
|
2019-10-09 12:44:13 +00:00
|
|
|
if res.Body.String() != "" {
|
2016-10-13 09:33:20 +00:00
|
|
|
t.Errorf("Expected empty body for failed HEAD request")
|
|
|
|
}
|
|
|
|
})
|
2018-04-28 19:39:46 +00:00
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "DeferLengthHeader", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2019-08-25 20:10:55 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
upload := NewMockFullUpload(ctrl)
|
|
|
|
|
|
|
|
gomock.InOrder(
|
2019-09-15 11:43:59 +00:00
|
|
|
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
|
2019-08-25 20:10:55 +00:00
|
|
|
SizeIsDeferred: true,
|
|
|
|
Size: 0,
|
|
|
|
}, nil),
|
|
|
|
)
|
2018-04-28 19:39:46 +00:00
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2018-04-28 19:39:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "HEAD",
|
|
|
|
URL: "yes",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
},
|
|
|
|
Code: http.StatusOK,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Upload-Defer-Length": "1",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "NoDeferLengthHeader", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2019-08-25 20:10:55 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
upload := NewMockFullUpload(ctrl)
|
|
|
|
|
2018-04-28 19:39:46 +00:00
|
|
|
gomock.InOrder(
|
2019-09-15 11:43:59 +00:00
|
|
|
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
|
2018-04-28 19:39:46 +00:00
|
|
|
SizeIsDeferred: false,
|
2019-05-15 21:57:20 +00:00
|
|
|
Size: 10,
|
2018-04-28 19:39:46 +00:00
|
|
|
}, nil),
|
|
|
|
)
|
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2018-04-28 19:39:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "HEAD",
|
|
|
|
URL: "yes",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
},
|
|
|
|
Code: http.StatusOK,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Upload-Defer-Length": "",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
2015-02-09 18:37:06 +00:00
|
|
|
}
|