From 888049ce500e771cfb1522b82f3931eb2af6219a Mon Sep 17 00:00:00 2001 From: Marius Date: Thu, 13 Oct 2016 18:38:43 +0200 Subject: [PATCH] Add test for attempting empty download --- get_test.go | 24 ++++++++++++++++++++++++ unrouted_handler.go | 11 ++++++----- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/get_test.go b/get_test.go index 53c1ae3..87bfe02 100644 --- a/get_test.go +++ b/get_test.go @@ -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) + }) } diff --git a/unrouted_handler.go b/unrouted_handler.go index 8442aec..8ceaf3e 100644 --- a/unrouted_handler.go +++ b/unrouted_handler.go @@ -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)