Add test for attempting empty download

This commit is contained in:
Marius 2016-10-13 18:38:43 +02:00
parent 5ff46eb2d7
commit 888049ce50
2 changed files with 30 additions and 5 deletions

View File

@ -66,4 +66,28 @@ func TestGet(t *testing.T) {
t.Error("expected reader to be closed")
}
})
SubTest(t, "EmptyDownload", func(t *testing.T, store *MockFullDataStore) {
store.EXPECT().GetInfo("yes").Return(FileInfo{
Offset: 0,
MetaData: map[string]string{
"filename": "file.jpg\"evil",
},
}, nil)
handler, _ := NewHandler(Config{
DataStore: store,
})
(&httpTest{
Method: "GET",
URL: "yes",
ResHeader: map[string]string{
"Content-Length": "0",
"Content-Disposition": `inline;filename="file.jpg\"evil"`,
},
Code: http.StatusNoContent,
ResBody: "",
}).Run(handler, t)
})
}

View File

@ -499,6 +499,12 @@ func (handler *UnroutedHandler) GetFile(w http.ResponseWriter, r *http.Request)
return
}
// Set headers before sending responses
w.Header().Set("Content-Length", strconv.FormatInt(info.Offset, 10))
if filename, ok := info.MetaData["filename"]; ok {
w.Header().Set("Content-Disposition", "inline;filename="+strconv.Quote(filename))
}
// Do not do anything if no data is stored yet.
if info.Offset == 0 {
handler.sendResp(w, r, http.StatusNoContent)
@ -512,11 +518,6 @@ func (handler *UnroutedHandler) GetFile(w http.ResponseWriter, r *http.Request)
return
}
if filename, ok := info.MetaData["filename"]; ok {
w.Header().Set("Content-Disposition", "inline;filename="+strconv.Quote(filename))
}
w.Header().Set("Content-Length", strconv.FormatInt(info.Offset, 10))
handler.sendResp(w, r, http.StatusOK)
io.Copy(w, src)