2019-06-11 16:23:20 +00:00
|
|
|
package handler_test
|
2016-10-13 10:29:13 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
2023-03-08 12:03:18 +00:00
|
|
|
"github.com/tus/tusd/v2/pkg/handler"
|
2016-10-13 10:29:13 +00:00
|
|
|
)
|
|
|
|
|
2022-06-19 10:18:02 +00:00
|
|
|
//go:generate mockgen -package handler_test -source utils_test.go -destination=handler_mock_test.go
|
2016-10-14 09:52:51 +00:00
|
|
|
|
|
|
|
// FullDataStore is an interface combining most interfaces for data stores.
|
|
|
|
// This is used by mockgen(1) to generate a mocked data store used for testing
|
|
|
|
// (see https://github.com/golang/mock). The only interface excluded is
|
|
|
|
// LockerDataStore because else we would have to explicitly expect calls for
|
|
|
|
// locking in every single test which would result in more verbose code.
|
|
|
|
// Therefore it has been moved into its own type definition, the Locker.
|
2016-10-13 10:29:13 +00:00
|
|
|
type FullDataStore interface {
|
2019-06-11 16:23:20 +00:00
|
|
|
handler.DataStore
|
|
|
|
handler.TerminaterDataStore
|
|
|
|
handler.ConcaterDataStore
|
|
|
|
handler.LengthDeferrerDataStore
|
2016-10-13 10:29:13 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 13:14:51 +00:00
|
|
|
type FullUpload interface {
|
|
|
|
handler.Upload
|
|
|
|
handler.TerminatableUpload
|
|
|
|
handler.LengthDeclarableUpload
|
2019-09-19 10:14:25 +00:00
|
|
|
handler.ConcatableUpload
|
2019-08-24 13:14:51 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 10:37:43 +00:00
|
|
|
type FullLocker interface {
|
|
|
|
handler.Locker
|
|
|
|
}
|
|
|
|
|
|
|
|
type FullLock interface {
|
|
|
|
handler.Lock
|
2016-10-13 16:08:34 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 10:29:13 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (test *httpTest) Run(handler http.Handler, t *testing.T) *httptest.ResponseRecorder {
|
|
|
|
req, _ := http.NewRequest(test.Method, test.URL, test.ReqBody)
|
2019-09-19 09:15:48 +00:00
|
|
|
req.RequestURI = test.URL
|
2016-10-13 10:29:13 +00:00
|
|
|
|
|
|
|
// 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 {
|
|
|
|
t.Errorf("Expected %v %s as status code (got %v %s)", test.Code, http.StatusText(test.Code), w.Code, http.StatusText(w.Code))
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range test.ResHeader {
|
2019-10-08 08:30:19 +00:00
|
|
|
header := w.Header().Get(key)
|
2016-10-13 10:29:13 +00:00
|
|
|
|
|
|
|
if value != header {
|
|
|
|
t.Errorf("Expected '%s' as '%s' (got '%s')", value, key, header)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-09 12:44:13 +00:00
|
|
|
if test.ResBody != "" && w.Body.String() != test.ResBody {
|
|
|
|
t.Errorf("Expected '%s' as body (got '%s'", test.ResBody, w.Body.String())
|
2016-10-13 10:29:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2016-10-13 16:08:34 +00:00
|
|
|
type readerMatcher struct {
|
2016-10-13 10:29:13 +00:00
|
|
|
expect string
|
|
|
|
}
|
|
|
|
|
2016-10-14 09:52:51 +00:00
|
|
|
// NewReaderMatcher returns a gomock.Matcher which can be used in tests for
|
|
|
|
// expecting io.Readers as arguments. It will only report an argument x as
|
|
|
|
// matching if it's an io.Reader which, if fully read, equals the string `expect`.
|
2016-10-13 10:29:13 +00:00
|
|
|
func NewReaderMatcher(expect string) gomock.Matcher {
|
2016-10-13 16:08:34 +00:00
|
|
|
return readerMatcher{
|
2016-10-13 10:29:13 +00:00
|
|
|
expect: expect,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-13 16:08:34 +00:00
|
|
|
func (m readerMatcher) Matches(x interface{}) bool {
|
2016-10-13 10:29:13 +00:00
|
|
|
input, ok := x.(io.Reader)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes, err := ioutil.ReadAll(input)
|
2019-05-26 19:56:51 +00:00
|
|
|
// Handle closed pipes similar to how EOF are handled by ioutil.ReadAll,
|
|
|
|
// we handle this error as if the stream ended normally.
|
|
|
|
if err == io.ErrClosedPipe {
|
|
|
|
err = nil
|
|
|
|
}
|
2016-10-13 10:29:13 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
readStr := string(bytes)
|
|
|
|
return reflect.DeepEqual(m.expect, readStr)
|
|
|
|
}
|
|
|
|
|
2016-10-13 16:08:34 +00:00
|
|
|
func (m readerMatcher) String() string {
|
2016-10-13 10:29:13 +00:00
|
|
|
return fmt.Sprintf("reads to %s", m.expect)
|
|
|
|
}
|