serve: improve head metadata test

This commit is contained in:
hiddenmarten 2025-04-12 19:36:21 +02:00
parent 5e869c9ad1
commit 9e44143353
2 changed files with 31 additions and 8 deletions

View File

@ -191,11 +191,12 @@ var _ fs.Fs = MemoryFs
// MemoryObject is an in memory object
type MemoryObject struct {
remote string
modTime time.Time
content []byte
meta fs.Metadata
fs fs.Fs
remote string
modTime time.Time
content []byte
meta fs.Metadata
fs fs.Fs
mimeType string
}
// NewMemoryObject returns an in memory Object with the modTime and content passed in
@ -214,6 +215,12 @@ func (o *MemoryObject) WithMetadata(meta fs.Metadata) *MemoryObject {
return o
}
// WithMimeType adds mimeType to the MemoryObject
func (o *MemoryObject) WithMimeType(mimeType string) *MemoryObject {
o.mimeType = mimeType
return o
}
// Content returns the underlying buffer
func (o *MemoryObject) Content() []byte {
return o.content
@ -329,8 +336,13 @@ func (o *MemoryObject) Metadata(ctx context.Context) (fs.Metadata, error) {
return o.meta, nil
}
func (o *MemoryObject) MimeType(ctx context.Context) string {
return o.mimeType
}
// Check interfaces
var (
_ fs.Object = (*MemoryObject)(nil)
_ fs.MimeTyper = (*MemoryObject)(nil)
_ fs.Metadataer = (*MemoryObject)(nil)
)

View File

@ -85,12 +85,23 @@ func TestObjectBadRange(t *testing.T) {
assert.Equal(t, "Bad Request\n", string(body))
}
func TestObjectHEADContentDisposition(t *testing.T) {
func TestObjectHEADMetadata(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest("HEAD", "http://example.com/aFile", nil)
m := fs.Metadata{"content-disposition": "inline"}
o := object.NewMemoryObject("aFile", time.Now(), []byte("")).WithMetadata(m)
m := fs.Metadata{
"content-type": "text/plain; charset=utf-8",
"content-disposition": "inline",
"cache-control": "no-cache",
"content-language": "en",
"content-encoding": "gzip",
}
o := object.NewMemoryObject("aFile", time.Now(), []byte("")).
WithMetadata(m).WithMimeType("text/plain; charset=utf-8")
Object(w, r, o)
resp := w.Result()
assert.Equal(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
assert.Equal(t, "inline", resp.Header.Get("Content-Disposition"))
assert.Equal(t, "no-cache", resp.Header.Get("Cache-Control"))
assert.Equal(t, "en", resp.Header.Get("Content-Language"))
assert.Equal(t, "gzip", resp.Header.Get("Content-Encoding"))
}