Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate ContentType field on create/modify ops #1141

Merged
merged 3 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions fakestorage/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -993,9 +993,10 @@ func (s *Server) updateObject(r *http.Request) jsonResponse {
}

var payload struct {
Metadata map[string]string `json:"metadata"`
CustomTime string
Acl []acls
Metadata map[string]string `json:"metadata"`
ContentType string `json:"contentType"`
CustomTime string
Acl []acls
}
err := json.NewDecoder(r.Body).Decode(&payload)
if err != nil {
Expand All @@ -1009,7 +1010,7 @@ func (s *Server) updateObject(r *http.Request) jsonResponse {

attrsToUpdate.Metadata = payload.Metadata
attrsToUpdate.CustomTime = payload.CustomTime

attrsToUpdate.ContentType = payload.ContentType
if len(payload.Acl) > 0 {
attrsToUpdate.ACL = []storage.ACLRule{}
for _, aclData := range payload.Acl {
Expand Down
51 changes: 51 additions & 0 deletions fakestorage/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1669,6 +1669,57 @@ func TestServerClientObjectUpdateCustomTime(t *testing.T) {
})
}

func TestServerClientObjectUpdateContentType(t *testing.T) {
const (
bucketName = "some-bucket"
objectName = "data.txt"
content = "some nice content"
contentType = "some content-type"
)
objs := []Object{
{
ObjectAttrs: ObjectAttrs{
BucketName: bucketName,
Name: objectName,
ContentType: contentType,
},
Content: []byte(content),
},
}
url := fmt.Sprintf("https://storage.googleapis.com/storage/v1/b/%s/o/%s", bucketName, objectName)
runServersTest(t, runServersOptions{objs: objs}, func(t *testing.T, server *Server) {
client := server.HTTPClient()
jsonBody := []byte(`{"ContentType": "another content-type"}`)
bodyReader := bytes.NewReader(jsonBody)
req, err := http.NewRequest(http.MethodPost, url, bodyReader)
if err != nil {
t.Fatal(err)
}
resp, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("wrong status returned\nwant %d\ngot %d", http.StatusOK, resp.StatusCode)
}
data, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
var respJsonBody ObjectAttrs
err = json.Unmarshal(data, &respJsonBody)
if err != nil {
t.Fatal(err)
}
updatedContentType := respJsonBody.ContentType
expectedContentType := "another content-type"
if updatedContentType != expectedContentType {
t.Errorf("unexpected content type time\nwant %q\ngot %q", expectedContentType, updatedContentType)
}
})
}

func TestServerClientObjectPatchCustomTime(t *testing.T) {
const (
bucketName = "some-bucket"
Expand Down
7 changes: 6 additions & 1 deletion fakestorage/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,12 @@ func (s *Server) uploadFileContent(r *http.Request) jsonResponse {
obj.Crc32c = checksum.EncodedCrc32cChecksum(obj.Content)
obj.Md5Hash = checksum.EncodedMd5Hash(obj.Content)
obj.Etag = fmt.Sprintf("%q", obj.Md5Hash)
obj.ContentType = r.Header.Get(contentTypeHeader)
contentTypeHeader := r.Header.Get(contentTypeHeader)
if contentTypeHeader != "" {
obj.ContentType = contentTypeHeader
} else {
obj.ContentType = "application/octet-stream"
}
responseHeader := make(http.Header)
if contentRange := r.Header.Get("Content-Range"); contentRange != "" {
parsed, err := parseContentRange(contentRange)
Expand Down
Loading