2019-06-11 16:23:20 +00:00
|
|
|
package handler_test
|
2015-02-09 18:37:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2015-12-25 21:33:27 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
2023-03-08 12:03:18 +00:00
|
|
|
. "github.com/tus/tusd/v2/pkg/handler"
|
2015-02-09 18:37:06 +00:00
|
|
|
)
|
|
|
|
|
2015-07-28 12:58:52 +00:00
|
|
|
type closingStringReader struct {
|
|
|
|
*strings.Reader
|
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (reader *closingStringReader) Close() error {
|
|
|
|
reader.closed = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-12 15:05:54 +00:00
|
|
|
func TestGet(t *testing.T) {
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "Download", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2016-10-13 16:08:34 +00:00
|
|
|
reader := &closingStringReader{
|
|
|
|
Reader: strings.NewReader("hello"),
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
gomock.InOrder(
|
2019-09-12 10:37:43 +00:00
|
|
|
locker.EXPECT().NewLock("yes").Return(lock, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
lock.EXPECT().Lock(gomock.Any(), gomock.Any()).Return(nil),
|
|
|
|
store.EXPECT().GetUpload(gomock.Any(), "yes").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2016-10-13 09:33:20 +00:00
|
|
|
Offset: 5,
|
|
|
|
Size: 20,
|
|
|
|
MetaData: map[string]string{
|
|
|
|
"filename": "file.jpg\"evil",
|
2018-02-28 21:55:14 +00:00
|
|
|
"filetype": "image/jpeg",
|
2016-10-13 09:33:20 +00:00
|
|
|
},
|
|
|
|
}, nil),
|
2022-03-19 22:21:17 +00:00
|
|
|
upload.EXPECT().GetReader(gomock.Any()).Return(reader, nil),
|
2019-09-12 10:37:43 +00:00
|
|
|
lock.EXPECT().Unlock().Return(nil),
|
2016-10-13 09:33:20 +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)
|
|
|
|
|
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
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
2016-10-13 16:33:33 +00:00
|
|
|
Method: "GET",
|
|
|
|
URL: "yes",
|
2016-10-13 09:33:20 +00:00
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Content-Length": "5",
|
2018-02-28 21:55:14 +00:00
|
|
|
"Content-Type": "image/jpeg",
|
|
|
|
"Content-Disposition": `inline;filename="file.jpg\"evil"`,
|
2016-10-13 09:33:20 +00:00
|
|
|
},
|
2016-10-13 16:33:33 +00:00
|
|
|
Code: http.StatusOK,
|
|
|
|
ResBody: "hello",
|
2016-10-13 09:33:20 +00:00
|
|
|
}).Run(handler, t)
|
|
|
|
|
|
|
|
if !reader.closed {
|
|
|
|
t.Error("expected reader to be closed")
|
|
|
|
}
|
2015-02-09 18:37:06 +00:00
|
|
|
})
|
2016-10-13 16:38:43 +00:00
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "EmptyDownload", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2019-08-24 13:14:51 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
upload := NewMockFullUpload(ctrl)
|
|
|
|
|
|
|
|
gomock.InOrder(
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().GetUpload(gomock.Any(), "yes").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2019-08-24 13:14:51 +00:00
|
|
|
Offset: 0,
|
|
|
|
}, nil),
|
|
|
|
)
|
2016-10-13 16:38:43 +00:00
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2016-10-13 16:38:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "GET",
|
|
|
|
URL: "yes",
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Content-Length": "0",
|
2018-02-19 17:28:42 +00:00
|
|
|
"Content-Disposition": `attachment`,
|
2016-10-13 16:38:43 +00:00
|
|
|
},
|
|
|
|
Code: http.StatusNoContent,
|
|
|
|
ResBody: "",
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
2016-10-13 16:48:46 +00:00
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "InvalidFileType", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2019-08-24 13:14:51 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
upload := NewMockFullUpload(ctrl)
|
|
|
|
|
|
|
|
gomock.InOrder(
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().GetUpload(gomock.Any(), "yes").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2019-08-24 13:14:51 +00:00
|
|
|
Offset: 0,
|
|
|
|
MetaData: map[string]string{
|
|
|
|
"filetype": "non-a-valid-mime-type",
|
|
|
|
},
|
|
|
|
}, nil),
|
|
|
|
)
|
2018-02-28 21:55:14 +00:00
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2018-02-28 21:55:14 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "GET",
|
|
|
|
URL: "yes",
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Content-Length": "0",
|
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
"Content-Disposition": `attachment`,
|
|
|
|
},
|
|
|
|
Code: http.StatusNoContent,
|
|
|
|
ResBody: "",
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
|
|
|
|
2019-08-20 14:16:05 +00:00
|
|
|
SubTest(t, "NotWhitelistedFileType", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
|
2019-08-24 13:14:51 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
upload := NewMockFullUpload(ctrl)
|
|
|
|
|
|
|
|
gomock.InOrder(
|
2022-03-19 22:21:17 +00:00
|
|
|
store.EXPECT().GetUpload(gomock.Any(), "yes").Return(upload, nil),
|
|
|
|
upload.EXPECT().GetInfo(gomock.Any()).Return(FileInfo{
|
2019-08-24 13:14:51 +00:00
|
|
|
Offset: 0,
|
|
|
|
MetaData: map[string]string{
|
2019-10-07 09:28:13 +00:00
|
|
|
"filetype": "application/vnd.openxmlformats-officedocument.wordprocessingml.document.v1",
|
2019-09-09 11:37:53 +00:00
|
|
|
"filename": "invoice.docx",
|
2019-08-24 13:14:51 +00:00
|
|
|
},
|
|
|
|
}, nil),
|
|
|
|
)
|
2018-02-28 21:55:14 +00:00
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
2019-08-20 14:16:05 +00:00
|
|
|
StoreComposer: composer,
|
2018-02-28 21:55:14 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "GET",
|
|
|
|
URL: "yes",
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Content-Length": "0",
|
2019-10-07 09:28:13 +00:00
|
|
|
"Content-Type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document.v1",
|
2019-09-05 17:45:43 +00:00
|
|
|
"Content-Disposition": `attachment;filename="invoice.docx"`,
|
2018-02-28 21:55:14 +00:00
|
|
|
},
|
|
|
|
Code: http.StatusNoContent,
|
|
|
|
ResBody: "",
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
2015-02-09 18:37:06 +00:00
|
|
|
}
|