tusd/pkg/handler/f/head_test.go

133 lines
3.0 KiB
Go
Raw Normal View History

package handler_test
2015-02-09 18:37:06 +00:00
import (
"net/http"
"os"
"testing"
2015-12-25 21:33:27 +00:00
"github.com/golang/mock/gomock"
. "github.com/tus/tusd/pkg/handler"
2015-02-09 18:37:06 +00:00
)
func TestHead(t *testing.T) {
SubTest(t, "Status", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
locker := NewMockLocker(ctrl)
gomock.InOrder(
locker.EXPECT().LockUpload("yes"),
store.EXPECT().GetInfo("yes").Return(FileInfo{
Offset: 11,
Size: 44,
MetaData: map[string]string{
"name": "lunrjs.png",
"type": "image/png",
},
}, nil),
locker.EXPECT().UnlockUpload("yes"),
)
composer = NewStoreComposer()
composer.UseCore(store)
composer.UseLocker(locker)
2015-02-09 18:37:06 +00:00
handler, _ := NewHandler(Config{
StoreComposer: composer,
})
2015-02-09 18:37:06 +00:00
res := (&httpTest{
Method: "HEAD",
URL: "yes",
ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0",
},
Code: http.StatusOK,
ResHeader: map[string]string{
"Upload-Offset": "11",
"Upload-Length": "44",
"Cache-Control": "no-store",
},
}).Run(handler, t)
2015-02-09 18:37:06 +00:00
// Since the order of a map is not guaranteed in Go, we need to be prepared
// for the case, that the order of the metadata may have been changed
if v := res.Header().Get("Upload-Metadata"); v != "name bHVucmpzLnBuZw==,type aW1hZ2UvcG5n" &&
v != "type aW1hZ2UvcG5n,name bHVucmpzLnBuZw==" {
t.Errorf("Expected valid metadata (got '%s')", v)
}
2015-02-09 18:37:06 +00:00
})
SubTest(t, "UploadNotFoundFail", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
store.EXPECT().GetInfo("no").Return(FileInfo{}, os.ErrNotExist)
2015-02-17 14:44:12 +00:00
handler, _ := NewHandler(Config{
StoreComposer: composer,
})
2015-11-26 15:25:34 +00:00
res := (&httpTest{
Method: "HEAD",
URL: "no",
ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0",
},
Code: http.StatusNotFound,
ResHeader: map[string]string{
"Content-Length": "0",
},
}).Run(handler, t)
if string(res.Body.Bytes()) != "" {
t.Errorf("Expected empty body for failed HEAD request")
}
})
2018-04-28 19:39:46 +00:00
SubTest(t, "DeferLengthHeader", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
2018-04-28 19:39:46 +00:00
store.EXPECT().GetInfo("yes").Return(FileInfo{
SizeIsDeferred: true,
Size: 0,
2018-04-28 19:39:46 +00:00
}, nil)
handler, _ := NewHandler(Config{
StoreComposer: composer,
2018-04-28 19:39:46 +00:00
})
(&httpTest{
Method: "HEAD",
URL: "yes",
ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0",
},
Code: http.StatusOK,
ResHeader: map[string]string{
"Upload-Defer-Length": "1",
},
}).Run(handler, t)
})
SubTest(t, "NoDeferLengthHeader", func(t *testing.T, store *MockFullDataStore, composer *StoreComposer) {
2018-04-28 19:39:46 +00:00
gomock.InOrder(
store.EXPECT().GetInfo("yes").Return(FileInfo{
SizeIsDeferred: false,
Size: 10,
2018-04-28 19:39:46 +00:00
}, nil),
)
handler, _ := NewHandler(Config{
StoreComposer: composer,
2018-04-28 19:39:46 +00:00
})
(&httpTest{
Method: "HEAD",
URL: "yes",
ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0",
},
Code: http.StatusOK,
ResHeader: map[string]string{
"Upload-Defer-Length": "",
},
}).Run(handler, t)
})
2015-02-09 18:37:06 +00:00
}