2015-12-25 21:33:27 +00:00
|
|
|
package tusd_test
|
2015-02-17 13:19:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
2015-12-25 21:33:27 +00:00
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
type concatPartialStore struct {
|
2016-01-20 15:40:13 +00:00
|
|
|
t *assert.Assertions
|
2015-02-17 13:19:56 +00:00
|
|
|
zeroStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s concatPartialStore) NewUpload(info FileInfo) (string, error) {
|
2016-01-20 15:40:13 +00:00
|
|
|
s.t.True(info.IsPartial)
|
|
|
|
s.t.False(info.IsFinal)
|
|
|
|
s.t.Nil(info.PartialUploads)
|
2015-02-17 13:19:56 +00:00
|
|
|
|
|
|
|
return "foo", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s concatPartialStore) GetInfo(id string) (FileInfo, error) {
|
|
|
|
return FileInfo{
|
|
|
|
IsPartial: true,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 14:33:17 +00:00
|
|
|
func (s concatPartialStore) ConcatUploads(id string, uploads []string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-17 13:19:56 +00:00
|
|
|
func TestConcatPartial(t *testing.T) {
|
|
|
|
handler, _ := NewHandler(Config{
|
|
|
|
MaxSize: 400,
|
|
|
|
BasePath: "files",
|
|
|
|
DataStore: concatPartialStore{
|
2016-01-20 15:40:13 +00:00
|
|
|
t: assert.New(t),
|
2015-02-17 13:19:56 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2016-01-20 14:33:17 +00:00
|
|
|
(&httpTest{
|
|
|
|
Name: "Successful OPTIONS request",
|
|
|
|
Method: "OPTIONS",
|
|
|
|
URL: "",
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Tus-Extension": "creation,concatenation",
|
|
|
|
},
|
|
|
|
Code: http.StatusNoContent,
|
|
|
|
}).Run(handler, t)
|
|
|
|
|
2015-02-17 14:44:12 +00:00
|
|
|
(&httpTest{
|
|
|
|
Name: "Successful POST request",
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
2015-03-23 17:15:05 +00:00
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "300",
|
|
|
|
"Upload-Concat": "partial",
|
2015-02-17 14:44:12 +00:00
|
|
|
},
|
|
|
|
Code: http.StatusCreated,
|
|
|
|
}).Run(handler, t)
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Name: "Successful HEAD request",
|
|
|
|
Method: "HEAD",
|
|
|
|
URL: "foo",
|
|
|
|
ReqHeader: map[string]string{
|
2015-03-23 17:15:05 +00:00
|
|
|
"Tus-Resumable": "1.0.0",
|
2015-02-17 14:44:12 +00:00
|
|
|
},
|
|
|
|
Code: http.StatusNoContent,
|
|
|
|
ResHeader: map[string]string{
|
2015-03-23 17:15:05 +00:00
|
|
|
"Upload-Concat": "partial",
|
2015-02-17 14:44:12 +00:00
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
2015-02-17 13:19:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type concatFinalStore struct {
|
2016-01-20 15:40:13 +00:00
|
|
|
t *assert.Assertions
|
2015-02-17 13:19:56 +00:00
|
|
|
zeroStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s concatFinalStore) NewUpload(info FileInfo) (string, error) {
|
2016-01-20 15:40:13 +00:00
|
|
|
s.t.False(info.IsPartial)
|
|
|
|
s.t.True(info.IsFinal)
|
|
|
|
s.t.Equal([]string{"a", "b"}, info.PartialUploads)
|
2015-02-17 13:19:56 +00:00
|
|
|
|
|
|
|
return "foo", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s concatFinalStore) GetInfo(id string) (FileInfo, error) {
|
|
|
|
if id == "a" || id == "b" {
|
|
|
|
return FileInfo{
|
|
|
|
IsPartial: true,
|
|
|
|
Size: 5,
|
|
|
|
Offset: 5,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if id == "c" {
|
|
|
|
return FileInfo{
|
|
|
|
IsPartial: true,
|
|
|
|
Size: 5,
|
|
|
|
Offset: 3,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if id == "foo" {
|
|
|
|
return FileInfo{
|
|
|
|
IsFinal: true,
|
|
|
|
PartialUploads: []string{"a", "b"},
|
|
|
|
Size: 10,
|
|
|
|
Offset: 10,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return FileInfo{}, ErrNotFound
|
|
|
|
}
|
|
|
|
|
2016-01-20 14:33:17 +00:00
|
|
|
func (s concatFinalStore) ConcatUploads(id string, uploads []string) error {
|
2016-01-20 15:40:13 +00:00
|
|
|
s.t.Equal("foo", id)
|
|
|
|
s.t.Equal([]string{"a", "b"}, uploads)
|
2015-02-17 13:19:56 +00:00
|
|
|
|
2016-01-20 14:33:17 +00:00
|
|
|
return nil
|
2015-02-17 13:19:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConcatFinal(t *testing.T) {
|
2016-03-12 21:52:31 +00:00
|
|
|
a := assert.New(t)
|
|
|
|
|
2015-02-17 13:19:56 +00:00
|
|
|
handler, _ := NewHandler(Config{
|
|
|
|
MaxSize: 400,
|
|
|
|
BasePath: "files",
|
|
|
|
DataStore: concatFinalStore{
|
2016-03-12 21:52:31 +00:00
|
|
|
t: a,
|
2015-02-17 13:19:56 +00:00
|
|
|
},
|
2016-03-12 21:52:31 +00:00
|
|
|
NotifyCompleteUploads: true,
|
2015-02-17 13:19:56 +00:00
|
|
|
})
|
|
|
|
|
2016-03-12 21:52:31 +00:00
|
|
|
c := make(chan FileInfo, 1)
|
|
|
|
handler.CompleteUploads = c
|
|
|
|
|
2015-02-17 14:44:12 +00:00
|
|
|
(&httpTest{
|
|
|
|
Name: "Successful POST request",
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
2015-03-23 17:15:05 +00:00
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Concat": "final; http://tus.io/files/a /files/b/",
|
2015-02-17 14:44:12 +00:00
|
|
|
},
|
|
|
|
Code: http.StatusCreated,
|
|
|
|
}).Run(handler, t)
|
|
|
|
|
2016-03-12 21:52:31 +00:00
|
|
|
info := <-c
|
|
|
|
a.Equal("foo", info.ID)
|
|
|
|
a.Equal(int64(10), info.Size)
|
|
|
|
a.Equal(int64(10), info.Offset)
|
|
|
|
a.False(info.IsPartial)
|
|
|
|
a.True(info.IsFinal)
|
|
|
|
a.Equal([]string{"a", "b"}, info.PartialUploads)
|
|
|
|
|
2015-02-17 14:44:12 +00:00
|
|
|
(&httpTest{
|
|
|
|
Name: "Successful HEAD request",
|
|
|
|
Method: "HEAD",
|
|
|
|
URL: "foo",
|
|
|
|
ReqHeader: map[string]string{
|
2015-03-23 17:15:05 +00:00
|
|
|
"Tus-Resumable": "1.0.0",
|
2015-02-17 14:44:12 +00:00
|
|
|
},
|
|
|
|
Code: http.StatusNoContent,
|
|
|
|
ResHeader: map[string]string{
|
2015-03-23 17:15:05 +00:00
|
|
|
"Upload-Concat": "final; http://tus.io/files/a http://tus.io/files/b",
|
|
|
|
"Upload-Length": "10",
|
2015-03-31 21:51:28 +00:00
|
|
|
"Upload-Offset": "10",
|
2015-02-17 14:44:12 +00:00
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Name: "Concatenating non finished upload (id: c)",
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
2015-03-23 17:15:05 +00:00
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Concat": "final; http://tus.io/files/c",
|
2015-02-17 14:44:12 +00:00
|
|
|
},
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
}).Run(handler, t)
|
2015-02-17 13:19:56 +00:00
|
|
|
|
|
|
|
handler, _ = NewHandler(Config{
|
|
|
|
MaxSize: 9,
|
|
|
|
BasePath: "files",
|
|
|
|
DataStore: concatFinalStore{
|
2016-01-20 15:40:13 +00:00
|
|
|
t: assert.New(t),
|
2015-02-17 13:19:56 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2015-02-17 14:44:12 +00:00
|
|
|
(&httpTest{
|
|
|
|
Name: "Exceeding MaxSize",
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
2015-03-23 17:15:05 +00:00
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Concat": "final; http://tus.io/files/a /files/b/",
|
2015-02-17 14:44:12 +00:00
|
|
|
},
|
|
|
|
Code: http.StatusRequestEntityTooLarge,
|
|
|
|
}).Run(handler, t)
|
2015-02-17 13:19:56 +00:00
|
|
|
}
|