removed SetContentLastModified and added lastModified time and name to WriteContent function

This commit is contained in:
Andrew Orban 2022-04-06 00:06:56 +00:00
parent c54abf5d1a
commit 9bb24bd815
2 changed files with 29 additions and 23 deletions

View file

@ -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
}

View file

@ -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"))
}