2015-12-25 21:33:27 +00:00
|
|
|
package tusd_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"
|
2015-12-25 21:33:27 +00:00
|
|
|
. "github.com/tus/tusd"
|
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) {
|
2016-10-13 14:45:40 +00:00
|
|
|
SubTest(t, "Download", func(t *testing.T, store *MockFullDataStore) {
|
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()
|
|
|
|
locker := NewMockLocker(ctrl)
|
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
gomock.InOrder(
|
2016-10-13 16:33:33 +00:00
|
|
|
locker.EXPECT().LockUpload("yes"),
|
2016-10-13 09:33:20 +00:00
|
|
|
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),
|
2016-10-13 16:33:33 +00:00
|
|
|
locker.EXPECT().UnlockUpload("yes"),
|
2016-10-13 09:33:20 +00:00
|
|
|
)
|
|
|
|
|
2016-10-13 16:33:33 +00:00
|
|
|
composer := NewStoreComposer()
|
|
|
|
composer.UseCore(store)
|
|
|
|
composer.UseGetReader(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-19 17:28:42 +00:00
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
"Content-Disposition": `attachment;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
|
|
|
|
|
|
|
SubTest(t, "EmptyDownload", func(t *testing.T, store *MockFullDataStore) {
|
|
|
|
store.EXPECT().GetInfo("yes").Return(FileInfo{
|
|
|
|
Offset: 0,
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
|
|
|
DataStore: store,
|
|
|
|
})
|
|
|
|
|
|
|
|
(&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
|
|
|
|
|
|
|
SubTest(t, "NotProvided", func(t *testing.T, store *MockFullDataStore) {
|
|
|
|
composer := NewStoreComposer()
|
|
|
|
composer.UseCore(store)
|
|
|
|
|
|
|
|
handler, _ := NewUnroutedHandler(Config{
|
|
|
|
StoreComposer: composer,
|
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "GET",
|
|
|
|
URL: "foo",
|
|
|
|
Code: http.StatusNotImplemented,
|
|
|
|
}).Run(http.HandlerFunc(handler.GetFile), t)
|
|
|
|
})
|
2015-02-09 18:37:06 +00:00
|
|
|
}
|