2015-12-25 21:33:27 +00:00
|
|
|
package tusd_test
|
2015-02-17 13:19:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2016-10-13 16:42:40 +00:00
|
|
|
"strings"
|
2015-02-17 13:19:56 +00:00
|
|
|
"testing"
|
2015-12-25 21:33:27 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
2016-01-20 15:40:13 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2015-12-25 21:33:27 +00:00
|
|
|
. "github.com/tus/tusd"
|
2015-02-17 13:19:56 +00:00
|
|
|
)
|
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
func TestConcat(t *testing.T) {
|
|
|
|
SubTest(t, "ExtensionDiscovery", func(t *testing.T, store *MockFullDataStore) {
|
|
|
|
composer := NewStoreComposer()
|
|
|
|
composer.UseCore(store)
|
|
|
|
composer.UseConcater(store)
|
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
|
|
|
StoreComposer: composer,
|
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "OPTIONS",
|
|
|
|
Code: http.StatusOK,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Tus-Extension": "creation,creation-with-upload,concatenation",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
2015-02-17 13:19:56 +00:00
|
|
|
})
|
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
SubTest(t, "Partial", func(t *testing.T, store *MockFullDataStore) {
|
|
|
|
SubTest(t, "Create", func(t *testing.T, store *MockFullDataStore) {
|
|
|
|
store.EXPECT().NewUpload(FileInfo{
|
|
|
|
Size: 300,
|
|
|
|
IsPartial: true,
|
|
|
|
IsFinal: false,
|
|
|
|
PartialUploads: nil,
|
|
|
|
MetaData: make(map[string]string),
|
|
|
|
}).Return("foo", nil)
|
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
|
|
|
BasePath: "files",
|
|
|
|
DataStore: store,
|
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "300",
|
|
|
|
"Upload-Concat": "partial",
|
|
|
|
},
|
|
|
|
Code: http.StatusCreated,
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
|
|
|
|
|
|
|
SubTest(t, "Status", func(t *testing.T, store *MockFullDataStore) {
|
|
|
|
store.EXPECT().GetInfo("foo").Return(FileInfo{
|
|
|
|
IsPartial: true,
|
|
|
|
}, 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": "partial",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
2015-02-17 13:19:56 +00:00
|
|
|
})
|
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
SubTest(t, "Final", func(t *testing.T, store *MockFullDataStore) {
|
|
|
|
SubTest(t, "Create", func(t *testing.T, store *MockFullDataStore) {
|
|
|
|
a := assert.New(t)
|
|
|
|
|
|
|
|
gomock.InOrder(
|
|
|
|
store.EXPECT().GetInfo("a").Return(FileInfo{
|
|
|
|
IsPartial: true,
|
|
|
|
Size: 5,
|
|
|
|
Offset: 5,
|
|
|
|
}, nil),
|
|
|
|
store.EXPECT().GetInfo("b").Return(FileInfo{
|
|
|
|
IsPartial: true,
|
|
|
|
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),
|
|
|
|
)
|
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
|
|
|
BasePath: "files",
|
|
|
|
DataStore: store,
|
|
|
|
NotifyCompleteUploads: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
c := make(chan FileInfo, 1)
|
|
|
|
handler.CompleteUploads = c
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Concat": "final; http://tus.io/files/a /files/b/",
|
|
|
|
},
|
|
|
|
Code: http.StatusCreated,
|
|
|
|
}).Run(handler, t)
|
|
|
|
|
|
|
|
info := <-c
|
|
|
|
a.Equal("foo", info.ID)
|
|
|
|
a.EqualValues(10, info.Size)
|
|
|
|
a.EqualValues(10, info.Offset)
|
|
|
|
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)
|
|
|
|
})
|
2016-10-13 16:42:40 +00:00
|
|
|
|
|
|
|
SubTest(t, "UploadToFinalFail", func(t *testing.T, store *MockFullDataStore) {
|
|
|
|
store.EXPECT().GetInfo("foo").Return(FileInfo{
|
|
|
|
Size: 10,
|
|
|
|
Offset: 0,
|
|
|
|
IsFinal: true,
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
|
|
|
DataStore: store,
|
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "PATCH",
|
|
|
|
URL: "foo",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Content-Type": "application/offset+octet-stream",
|
|
|
|
"Upload-Offset": "5",
|
|
|
|
},
|
|
|
|
ReqBody: strings.NewReader("hello"),
|
|
|
|
Code: http.StatusForbidden,
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
2016-10-13 17:03:18 +00:00
|
|
|
|
|
|
|
SubTest(t, "InvalidConcatHeaderFail", func(t *testing.T, store *MockFullDataStore) {
|
|
|
|
handler, _ := NewHandler(Config{
|
|
|
|
DataStore: store,
|
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Method: "POST",
|
|
|
|
URL: "",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Concat": "final; ",
|
|
|
|
},
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
}).Run(handler, t)
|
|
|
|
})
|
2015-02-17 13:19:56 +00:00
|
|
|
})
|
|
|
|
}
|