diff --git a/fakestorage/object.go b/fakestorage/object.go index 62bfd86b1b..3210d9b1c2 100644 --- a/fakestorage/object.go +++ b/fakestorage/object.go @@ -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 { @@ -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 { diff --git a/fakestorage/object_test.go b/fakestorage/object_test.go index 48c1263175..ac3cd0ab07 100644 --- a/fakestorage/object_test.go +++ b/fakestorage/object_test.go @@ -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" diff --git a/fakestorage/upload.go b/fakestorage/upload.go index 1fbb1ef849..fe1cceb641 100644 --- a/fakestorage/upload.go +++ b/fakestorage/upload.go @@ -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)