tusd/handler_test.go

79 lines
1.6 KiB
Go
Raw Normal View History

package tusd
import (
"io"
2015-02-17 14:44:12 +00:00
"net/http"
"net/http/httptest"
"testing"
)
type zeroStore struct{}
func (store zeroStore) NewUpload(info FileInfo) (string, error) {
return "", nil
}
func (store zeroStore) WriteChunk(id string, offset int64, src io.Reader) error {
return nil
}
func (store zeroStore) GetInfo(id string) (FileInfo, error) {
return FileInfo{}, nil
}
2015-02-06 21:05:33 +00:00
func (store zeroStore) GetReader(id string) (io.Reader, error) {
return nil, ErrNotImplemented
}
2015-02-17 14:44:12 +00:00
2015-02-28 13:47:39 +00:00
func (store zeroStore) Terminate(id string) error {
return ErrNotImplemented
}
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
}
func (test *httpTest) Run(handler http.Handler, t *testing.T) {
t.Log(test.Name)
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)
if value == "" && header == "" {
t.Errorf("Expected '%s' in response", key)
}
if value != "" && value != header {
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()))
}
}