2015-12-25 21:33:27 +00:00
|
|
|
package tusd_test
|
2015-02-09 18:37:06 +00:00
|
|
|
|
|
|
|
import (
|
2016-08-28 20:06:37 +00:00
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2015-02-09 18:37:06 +00:00
|
|
|
"net/http"
|
2016-08-28 20:06:37 +00:00
|
|
|
"strings"
|
2015-02-09 18:37:06 +00:00
|
|
|
"testing"
|
2015-12-25 21:33:27 +00:00
|
|
|
|
2016-08-28 20:06:37 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2015-12-25 21:33:27 +00:00
|
|
|
. "github.com/tus/tusd"
|
2015-02-09 18:37:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type postStore struct {
|
2016-08-28 20:06:37 +00:00
|
|
|
t *assert.Assertions
|
2015-02-09 18:37:06 +00:00
|
|
|
zeroStore
|
|
|
|
}
|
|
|
|
|
2015-02-16 16:53:50 +00:00
|
|
|
func (s postStore) NewUpload(info FileInfo) (string, error) {
|
2016-08-28 20:06:37 +00:00
|
|
|
s.t.Equal(int64(300), info.Size)
|
2015-02-09 18:37:06 +00:00
|
|
|
|
2015-02-16 16:53:50 +00:00
|
|
|
metaData := info.MetaData
|
2016-08-28 20:06:37 +00:00
|
|
|
s.t.Equal(2, len(metaData))
|
|
|
|
s.t.Equal("hello", metaData["foo"])
|
|
|
|
s.t.Equal("world", metaData["bar"])
|
2015-02-09 18:37:06 +00:00
|
|
|
|
2016-08-28 20:06:37 +00:00
|
|
|
return "foo", nil
|
|
|
|
}
|
2015-02-09 18:37:06 +00:00
|
|
|
|
2016-08-28 20:06:37 +00:00
|
|
|
func (s postStore) WriteChunk(id string, offset int64, src io.Reader) (int64, error) {
|
|
|
|
s.t.Equal(int64(0), offset)
|
2015-02-09 18:37:06 +00:00
|
|
|
|
2016-08-28 20:06:37 +00:00
|
|
|
data, err := ioutil.ReadAll(src)
|
|
|
|
s.t.Nil(err)
|
|
|
|
s.t.Equal("hello", string(data))
|
|
|
|
|
|
|
|
return 5, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s postStore) ConcatUploads(id string, uploads []string) error {
|
|
|
|
s.t.True(false, "concatenation should not be attempted")
|
|
|
|
return nil
|
2015-02-09 18:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPost(t *testing.T) {
|
2016-08-28 20:06:37 +00:00
|
|
|
a := assert.New(t)
|
|
|
|
|
2015-02-09 18:37:06 +00:00
|
|
|
handler, _ := NewHandler(Config{
|
|
|
|
MaxSize: 400,
|
|
|
|
BasePath: "files",
|
|
|
|
DataStore: postStore{
|
2016-08-28 20:06:37 +00:00
|
|
|
t: a,
|
2015-02-09 18:37:06 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2015-02-17 14:44:12 +00:00
|
|
|
(&httpTest{
|
|
|
|
Name: "Successful request",
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
2015-03-23 17:15:05 +00:00
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "300",
|
|
|
|
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
2015-02-17 14:44:12 +00:00
|
|
|
},
|
|
|
|
Code: http.StatusCreated,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Location": "http://tus.io/files/foo",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
2015-02-09 18:37:06 +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-Length": "500",
|
|
|
|
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
2015-02-17 14:44:12 +00:00
|
|
|
},
|
|
|
|
Code: http.StatusRequestEntityTooLarge,
|
|
|
|
}).Run(handler, t)
|
2016-01-16 14:27:35 +00:00
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Name: "Ignore Forwarded headers",
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "300",
|
|
|
|
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
|
|
|
"X-Forwarded-Host": "foo.com",
|
|
|
|
"X-Forwarded-Proto": "https",
|
|
|
|
},
|
|
|
|
Code: http.StatusCreated,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Location": "http://tus.io/files/foo",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
|
|
|
|
|
|
|
handler, _ = NewHandler(Config{
|
|
|
|
MaxSize: 400,
|
|
|
|
BasePath: "files",
|
|
|
|
DataStore: postStore{
|
2016-08-28 20:06:37 +00:00
|
|
|
t: a,
|
2016-01-16 14:27:35 +00:00
|
|
|
},
|
|
|
|
RespectForwardedHeaders: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Name: "Respect X-Forwarded-* headers",
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "300",
|
|
|
|
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
|
|
|
"X-Forwarded-Host": "foo.com",
|
|
|
|
"X-Forwarded-Proto": "https",
|
|
|
|
},
|
|
|
|
Code: http.StatusCreated,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Location": "https://foo.com/files/foo",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Name: "Respect Forwarded headers",
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "300",
|
|
|
|
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
|
|
|
"X-Forwarded-Host": "bar.com",
|
|
|
|
"X-Forwarded-Proto": "http",
|
|
|
|
"Forwarded": "proto=https,host=foo.com",
|
|
|
|
},
|
|
|
|
Code: http.StatusCreated,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Location": "https://foo.com/files/foo",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Name: "Filter forwarded protocol",
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "300",
|
|
|
|
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
|
|
|
"X-Forwarded-Proto": "aaa",
|
|
|
|
"Forwarded": "proto=bbb",
|
|
|
|
},
|
|
|
|
Code: http.StatusCreated,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Location": "http://tus.io/files/foo",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
2015-02-09 18:37:06 +00:00
|
|
|
}
|
2016-08-28 20:06:37 +00:00
|
|
|
|
|
|
|
func TestPostWithUpload(t *testing.T) {
|
|
|
|
a := assert.New(t)
|
|
|
|
|
|
|
|
handler, _ := NewHandler(Config{
|
|
|
|
MaxSize: 400,
|
|
|
|
BasePath: "files",
|
|
|
|
DataStore: postStore{
|
|
|
|
t: a,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Name: "Successful request",
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "300",
|
|
|
|
"Content-Type": "application/offset+octet-stream",
|
|
|
|
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
|
|
|
},
|
|
|
|
ReqBody: strings.NewReader("hello"),
|
|
|
|
Code: http.StatusCreated,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Location": "http://tus.io/files/foo",
|
|
|
|
"Upload-Offset": "5",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Name: "Exceeding upload size",
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "300",
|
|
|
|
"Content-Type": "application/offset+octet-stream",
|
|
|
|
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
|
|
|
},
|
|
|
|
ReqBody: bytes.NewReader(make([]byte, 400)),
|
|
|
|
Code: http.StatusRequestEntityTooLarge,
|
|
|
|
}).Run(handler, t)
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Name: "Incorrect content type",
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
2016-09-29 19:20:51 +00:00
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "300",
|
|
|
|
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
|
|
|
"Content-Type": "application/false",
|
2016-08-28 20:06:37 +00:00
|
|
|
},
|
|
|
|
ReqBody: strings.NewReader("hello"),
|
2016-09-29 19:20:51 +00:00
|
|
|
Code: http.StatusCreated,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Location": "http://tus.io/files/foo",
|
|
|
|
"Upload-Offset": "",
|
|
|
|
},
|
2016-08-28 20:06:37 +00:00
|
|
|
}).Run(handler, t)
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Name: "Upload and final concatenation",
|
|
|
|
Method: "POST",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Length": "300",
|
|
|
|
"Content-Type": "application/offset+octet-stream",
|
|
|
|
"Upload-Metadata": "foo aGVsbG8=, bar d29ybGQ=",
|
|
|
|
"Upload-Concat": "final; http://tus.io/files/a http://tus.io/files/b",
|
|
|
|
},
|
|
|
|
ReqBody: strings.NewReader("hello"),
|
|
|
|
Code: http.StatusForbidden,
|
|
|
|
}).Run(handler, t)
|
|
|
|
}
|