From 9bb24bd815c006b1a1b814ee51e843eed82cbba9 Mon Sep 17 00:00:00 2001 From: Andrew Orban Date: Wed, 6 Apr 2022 00:06:56 +0000 Subject: [PATCH] removed SetContentLastModified and added lastModified time and name to WriteContent function --- context.go | 22 +++------------------- context_test.go | 30 ++++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/context.go b/context.go index 1c94480..b524c47 100644 --- a/context.go +++ b/context.go @@ -70,13 +70,7 @@ type Context interface { // WriteContent wraps http.ServeContent in order to handle serving streams // it will handle Range and Modified (like If-Unmodified-Since) headers. - WriteContent(content io.ReadSeeker) - - // SetContentLastModified sets the time the content was last modified for - // WriteContent requests. If set, WriteContent will add it as the - // Last-Modified header and will properly respond to Modified request - // headers. - SetContentLastModified(modTime time.Time) + WriteContent(name string, content io.ReadSeeker, lastModified time.Time) } type hcontext struct { @@ -89,7 +83,6 @@ type hcontext struct { docsPrefix string urlPrefix string disableSchemaProperty bool - modTime time.Time } func (c *hcontext) WithValue(key, value interface{}) Context { @@ -387,20 +380,11 @@ func selectContentType(r *http.Request) string { return ct } -func (c *hcontext) WriteContent(content io.ReadSeeker) { +func (c *hcontext) WriteContent(name string, content io.ReadSeeker, lastModified time.Time) { if c.closed { panic(fmt.Errorf("Trying to write to response after WriteModel, WriteError, or WriteContent for %s %s", c.r.Method, c.r.URL.Path)) } - // name is left blank, this is used by ServeContent to automatically - // determine Content-Type. Huma has opted to have handlers set Content-Type - // explicitly rather than introduce this into the method signature as name - // is not applicable to every ReadSeeker. Leaving this blank or setting the - // Content-Type on the request disables this functionality anyway. - http.ServeContent(c.ResponseWriter, c.r, "", c.modTime, content) + http.ServeContent(c.ResponseWriter, c.r, name, lastModified, content) c.closed = true } - -func (c *hcontext) SetContentLastModified(modTime time.Time) { - c.modTime = modTime -} diff --git a/context_test.go b/context_test.go index cb58677..c9d767c 100644 --- a/context_test.go +++ b/context_test.go @@ -281,7 +281,7 @@ func TestWriteContent(t *testing.T) { ).Run(func(ctx Context) { ctx.Header().Set("Content-Type", "application/octet-stream") content := bytes.NewReader(b) - ctx.WriteContent(content) + ctx.WriteContent("", content, time.Time{}) }) w := httptest.NewRecorder() @@ -303,7 +303,7 @@ func TestWriteContentRespectsRange(t *testing.T) { ).Run(func(ctx Context) { ctx.Header().Set("Content-Type", "application/octet-stream") content := bytes.NewReader(b) - ctx.WriteContent(content) + ctx.WriteContent("", content, time.Time{}) }) w := httptest.NewRecorder() @@ -329,8 +329,7 @@ func TestWriteContentLastModified(t *testing.T) { ).Run(func(ctx Context) { ctx.Header().Set("Content-Type", "application/octet-stream") content := bytes.NewReader(b) - ctx.SetContentLastModified(modTime) - ctx.WriteContent(content) + ctx.WriteContent("", content, modTime) }) w := httptest.NewRecorder() @@ -345,3 +344,26 @@ func TestWriteContentLastModified(t *testing.T) { assert.Equal(t, strTime, w.Header().Get("Last-Modified")) } + +func TestWriteContentName(t *testing.T) { + app := newTestRouter() + + b := []byte("Test Byte Data") + + app.Resource("/content").Get("test", "Test", + NewResponse(206, "desc").Model(Response{}), + ).Run(func(ctx Context) { + + content := bytes.NewReader(b) + ctx.WriteContent("/path/with/content.mp4", content, time.Time{}) + }) + + w := httptest.NewRecorder() + req, _ := http.NewRequest(http.MethodGet, "/content", nil) + app.ServeHTTP(w, req) + + // confirms that name is properly being forwarded to ServeContent. + // We'll assume the more advanced modTime use cases are properly tested + // in http library. + assert.Equal(t, "video/mp4", w.Header().Get("Content-Type")) +}