Skip to content

Commit

Permalink
fix: use json.Marshal
Browse files Browse the repository at this point in the history
Signed-off-by: knqyf263 <[email protected]>
  • Loading branch information
knqyf263 committed Jul 29, 2024
1 parent 619e5d2 commit 6f64974
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
8 changes: 7 additions & 1 deletion registry/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ func (s *registryRouter) manifestHandler(ctx context.Context, w http.ResponseWri

w.Header().Set("Content-Type", string(m.MediaType))
w.WriteHeader(http.StatusOK)
if err = json.NewEncoder(w).Encode(m); err != nil {

// Use json.Marshal instead of json.NewEncoder to avoid writing a newline
b, err := json.Marshal(m)
if err != nil {
return errdefs.Unavailable(err)
}
if _, err = w.Write(b); err != nil {
return errdefs.Unavailable(err)
}
return nil
Expand Down
5 changes: 2 additions & 3 deletions registry/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ func TestNewDockerRegistry_manifestHandler(t *testing.T) {
"v2/alpine:ref123": "testdata/alpine/alpine.tar",
},
},
expectedStatusCode: http.StatusOK,
expectedResponseBody: `{"schemaVersion":2,"mediaType":"application/vnd.docker.distribution.manifest.v2+json","config":{"mediaType":"application/vnd.docker.container.image.v1+json","size":1512,"digest":"sha256:af341ccd2df8b0e2d67cf8dd32e087bfda4e5756ebd1c76bbf3efa0dc246590e"},"layers":[{"mediaType":"application/vnd.docker.image.rootfs.diff.tar.gzip","size":3029607,"digest":"sha256:988beb990993123f9c14951440e468cb469f9f1f4fe512fd9095b48f9c9e7130"}]}
`,
expectedStatusCode: http.StatusOK,
expectedResponseBody: `{"schemaVersion":2,"mediaType":"application/vnd.docker.distribution.manifest.v2+json","config":{"mediaType":"application/vnd.docker.container.image.v1+json","size":1512,"digest":"sha256:af341ccd2df8b0e2d67cf8dd32e087bfda4e5756ebd1c76bbf3efa0dc246590e"},"layers":[{"mediaType":"application/vnd.docker.image.rootfs.diff.tar.gzip","size":3029607,"digest":"sha256:988beb990993123f9c14951440e468cb469f9f1f4fe512fd9095b48f9c9e7130"}]}`,
},
{
name: "sad path, image not found",
Expand Down

0 comments on commit 6f64974

Please sign in to comment.