Skip to content

Commit

Permalink
Add application/json content type where required (#452)
Browse files Browse the repository at this point in the history
* Add application/json content type where required

* Move content-type closer to encode

* Move content header closer to encode
  • Loading branch information
cgreening authored Mar 15, 2021
1 parent a0465b3 commit 6e18c77
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions fakestorage/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func (s *Server) createBucketByPost(w http.ResponseWriter, r *http.Request) {
return
}
resp := newBucketResponse(bucket)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(resp)
}

Expand All @@ -94,6 +95,7 @@ func (s *Server) listBuckets(w http.ResponseWriter, r *http.Request) {
return
}
resp := newListBucketsResponse(buckets)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(resp)
}

Expand All @@ -108,6 +110,7 @@ func (s *Server) getBucket(w http.ResponseWriter, r *http.Request) {
return
}
resp := newBucketResponse(bucket)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
encoder.Encode(resp)
}
Expand Down
9 changes: 9 additions & 0 deletions fakestorage/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,16 @@ func (s *Server) listObjects(w http.ResponseWriter, r *http.Request) {
versions := r.URL.Query().Get("versions")

objs, prefixes, err := s.ListObjects(bucketName, prefix, delimiter, versions == "true")

encoder := json.NewEncoder(w)
if err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
errResp := newErrorResponse(http.StatusNotFound, http.StatusText(http.StatusNotFound), nil)
encoder.Encode(errResp)
return
}
w.Header().Set("Content-Type", "application/json")
encoder.Encode(newListObjectsResponse(objs, prefixes))
}

Expand All @@ -387,6 +390,7 @@ func (s *Server) getObject(w http.ResponseWriter, r *http.Request) {
message = err.Error()
}
errResp := newErrorResponse(statusCode, message, nil)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
encoder.Encode(errResp)
return
Expand All @@ -398,8 +402,10 @@ func (s *Server) getObject(w http.ResponseWriter, r *http.Request) {
func (s *Server) deleteObject(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
err := s.backend.DeleteObject(vars["bucketName"], vars["objectName"])

if err != nil {
errResp := newErrorResponse(http.StatusNotFound, http.StatusText(http.StatusNotFound), nil)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(errResp)
return
Expand All @@ -409,13 +415,15 @@ func (s *Server) deleteObject(w http.ResponseWriter, r *http.Request) {

func (s *Server) listObjectACL(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)

obj, err := s.GetObject(vars["bucketName"], vars["objectName"])
if err != nil {
http.Error(w, "not found", http.StatusNotFound)
return
}

response := newACLListResponse(obj)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}

Expand Down Expand Up @@ -449,6 +457,7 @@ func (s *Server) setObjectACL(w http.ResponseWriter, r *http.Request) {
s.CreateObject(obj)

response := newACLListResponse(obj)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}

Expand Down
12 changes: 11 additions & 1 deletion fakestorage/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ type contentRange struct {

func (s *Server) insertObject(w http.ResponseWriter, r *http.Request) {
bucketName := mux.Vars(r)["bucketName"]

if _, err := s.backend.GetBucket(bucketName); err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
err := newErrorResponse(http.StatusNotFound, "Not found", nil)
json.NewEncoder(w).Encode(err)
Expand Down Expand Up @@ -87,12 +89,14 @@ func (s *Server) checkUploadPreconditions(w http.ResponseWriter, r *http.Request

if ifGenerationMatch == "0" {
if _, err := s.backend.GetObject(bucketName, objectName); err == nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusPreconditionFailed)
err := newErrorResponse(http.StatusPreconditionFailed, "Precondition failed", nil)
json.NewEncoder(w).Encode(err)
return false
}
} else if ifGenerationMatch != "" || r.URL.Query().Get("ifGenerationNotMatch") != "" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotImplemented)
err := newErrorResponse(http.StatusNotImplemented, "Precondition support not implemented", nil)
json.NewEncoder(w).Encode(err)
Expand All @@ -103,6 +107,7 @@ func (s *Server) checkUploadPreconditions(w http.ResponseWriter, r *http.Request
}

func (s *Server) simpleUpload(bucketName string, w http.ResponseWriter, r *http.Request) {

defer r.Body.Close()
name := r.URL.Query().Get("name")
predefinedACL := r.URL.Query().Get("predefinedAcl")
Expand Down Expand Up @@ -131,11 +136,13 @@ func (s *Server) simpleUpload(bucketName string, w http.ResponseWriter, r *http.
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(obj)
}

func (s *Server) signedUpload(bucketName string, w http.ResponseWriter, r *http.Request) {

defer r.Body.Close()
name := mux.Vars(r)["objectName"]
predefinedACL := r.URL.Query().Get("predefinedAcl")
Expand Down Expand Up @@ -175,6 +182,7 @@ func (s *Server) signedUpload(bucketName string, w http.ResponseWriter, r *http.
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(obj)
}
Expand Down Expand Up @@ -285,12 +293,13 @@ func (s *Server) multipartUpload(bucketName string, w http.ResponseWriter, r *ht
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(obj)
}

func (s *Server) resumableUpload(bucketName string, w http.ResponseWriter, r *http.Request) {

predefinedACL := r.URL.Query().Get("predefinedAcl")
contentEncoding := r.URL.Query().Get("contentEncoding")
metadata, err := loadMetadata(r.Body)
Expand Down Expand Up @@ -320,6 +329,7 @@ func (s *Server) resumableUpload(bucketName string, w http.ResponseWriter, r *ht
w.Header().Set("X-Goog-Upload-URL", s.URL()+"/upload/resumable/"+uploadID)
w.Header().Set("X-Goog-Upload-Status", "active")
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(obj)
}
Expand Down

0 comments on commit 6e18c77

Please sign in to comment.