2015-12-25 21:33:27 +00:00
|
|
|
package tusd_test
|
2015-02-01 13:57:57 +00:00
|
|
|
|
|
|
|
import (
|
2016-10-13 09:33:20 +00:00
|
|
|
"fmt"
|
2015-02-01 13:57:57 +00:00
|
|
|
"io"
|
2016-10-13 09:33:20 +00:00
|
|
|
"io/ioutil"
|
2015-02-17 14:44:12 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2016-10-13 09:33:20 +00:00
|
|
|
"reflect"
|
2015-10-06 16:27:40 +00:00
|
|
|
"strings"
|
2015-02-17 14:44:12 +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
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
"github.com/tus/tusd"
|
2015-12-25 21:33:27 +00:00
|
|
|
. "github.com/tus/tusd"
|
2015-02-01 13:57:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type zeroStore struct{}
|
|
|
|
|
2015-02-16 16:53:50 +00:00
|
|
|
func (store zeroStore) NewUpload(info FileInfo) (string, error) {
|
2015-02-01 13:57:57 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
2015-03-23 18:02:12 +00:00
|
|
|
func (store zeroStore) WriteChunk(id string, offset int64, src io.Reader) (int64, error) {
|
|
|
|
return 0, nil
|
2015-02-01 13:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store zeroStore) GetInfo(id string) (FileInfo, error) {
|
|
|
|
return FileInfo{}, nil
|
|
|
|
}
|
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
type FullDataStore interface {
|
|
|
|
tusd.DataStore
|
|
|
|
tusd.TerminaterDataStore
|
|
|
|
tusd.ConcaterDataStore
|
|
|
|
tusd.GetReaderDataStore
|
|
|
|
}
|
|
|
|
|
2015-02-17 14:44:12 +00:00
|
|
|
type httpTest struct {
|
|
|
|
Name string
|
|
|
|
|
|
|
|
Method string
|
|
|
|
URL string
|
|
|
|
|
|
|
|
ReqBody io.Reader
|
|
|
|
ReqHeader map[string]string
|
|
|
|
|
|
|
|
Code int
|
|
|
|
ResBody string
|
|
|
|
ResHeader map[string]string
|
|
|
|
}
|
|
|
|
|
2015-11-26 15:25:34 +00:00
|
|
|
func (test *httpTest) Run(handler http.Handler, t *testing.T) *httptest.ResponseRecorder {
|
2015-02-17 14:44:12 +00:00
|
|
|
req, _ := http.NewRequest(test.Method, test.URL, test.ReqBody)
|
|
|
|
|
|
|
|
// Add headers
|
|
|
|
for key, value := range test.ReqHeader {
|
|
|
|
req.Header.Set(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Host = "tus.io"
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
if w.Code != test.Code {
|
2015-03-23 17:17:21 +00:00
|
|
|
t.Errorf("Expected %v %s as status code (got %v %s)", test.Code, http.StatusText(test.Code), w.Code, http.StatusText(w.Code))
|
2015-02-17 14:44:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range test.ResHeader {
|
|
|
|
header := w.HeaderMap.Get(key)
|
|
|
|
|
2016-09-29 19:18:12 +00:00
|
|
|
if value != header {
|
2015-02-17 14:44:12 +00:00
|
|
|
t.Errorf("Expected '%s' as '%s' (got '%s')", value, key, header)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if test.ResBody != "" && string(w.Body.Bytes()) != test.ResBody {
|
|
|
|
t.Errorf("Expected '%s' as body (got '%s'", test.ResBody, string(w.Body.Bytes()))
|
|
|
|
}
|
2015-11-26 15:25:34 +00:00
|
|
|
|
|
|
|
return w
|
2015-02-17 14:44:12 +00:00
|
|
|
}
|
2015-10-06 16:27:40 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
func SubTest(t *testing.T, name string, runTest func(*testing.T, *MockFullDataStore)) {
|
|
|
|
t.Run(name, func(subT *testing.T) {
|
|
|
|
//subT.Parallel()
|
|
|
|
|
|
|
|
ctrl := gomock.NewController(subT)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
store := NewMockFullDataStore(ctrl)
|
|
|
|
|
|
|
|
runTest(subT, store)
|
|
|
|
})
|
2015-10-06 16:27:40 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
type ReaderMatcher struct {
|
|
|
|
expect string
|
|
|
|
}
|
2015-10-06 16:27:40 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
func NewReaderMatcher(expect string) gomock.Matcher {
|
|
|
|
return ReaderMatcher{
|
|
|
|
expect: expect,
|
|
|
|
}
|
2015-10-06 16:27:40 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
func (m ReaderMatcher) Matches(x interface{}) bool {
|
|
|
|
input, ok := x.(io.Reader)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes, err := ioutil.ReadAll(input)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
readStr := string(bytes)
|
|
|
|
return reflect.DeepEqual(m.expect, readStr)
|
|
|
|
}
|
2015-10-06 16:27:40 +00:00
|
|
|
|
2016-10-13 09:33:20 +00:00
|
|
|
func (m ReaderMatcher) String() string {
|
|
|
|
return fmt.Sprintf("reads to %s", m.expect)
|
2015-10-06 16:27:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMethodOverride(t *testing.T) {
|
2016-10-13 09:33:20 +00:00
|
|
|
mockCtrl := gomock.NewController(t)
|
|
|
|
defer mockCtrl.Finish()
|
|
|
|
|
|
|
|
store := NewMockFullDataStore(mockCtrl)
|
|
|
|
|
|
|
|
store.EXPECT().GetInfo("yes").Return(FileInfo{
|
|
|
|
Offset: 5,
|
|
|
|
Size: 20,
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
store.EXPECT().WriteChunk("yes", int64(5), NewReaderMatcher("hello")).Return(int64(5), nil)
|
|
|
|
|
2015-10-06 16:27:40 +00:00
|
|
|
handler, _ := NewHandler(Config{
|
|
|
|
DataStore: store,
|
|
|
|
})
|
|
|
|
|
|
|
|
(&httpTest{
|
|
|
|
Name: "Successful request",
|
|
|
|
Method: "POST",
|
|
|
|
URL: "yes",
|
|
|
|
ReqHeader: map[string]string{
|
|
|
|
"Tus-Resumable": "1.0.0",
|
|
|
|
"Upload-Offset": "5",
|
2015-11-04 09:56:32 +00:00
|
|
|
"Content-Type": "application/offset+octet-stream",
|
2015-10-06 16:27:40 +00:00
|
|
|
"X-HTTP-Method-Override": "PATCH",
|
|
|
|
},
|
|
|
|
ReqBody: strings.NewReader("hello"),
|
|
|
|
Code: http.StatusNoContent,
|
|
|
|
ResHeader: map[string]string{
|
|
|
|
"Upload-Offset": "10",
|
|
|
|
},
|
|
|
|
}).Run(handler, t)
|
|
|
|
}
|