Skip to content

Commit

Permalink
feat: add Content-Type
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 547f233 commit 619e5d2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
7 changes: 5 additions & 2 deletions registry/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package registry

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -72,12 +73,14 @@ func (s *registryRouter) manifestHandler(ctx context.Context, w http.ResponseWri
return errdefs.NotFound(xerrors.Errorf("unknown image: %s", filePath))
}

b, err := img.RawManifest()
m, err := img.Manifest()
if err != nil {
return errdefs.Unavailable(err)
}

if _, err = w.Write(b); err != nil {
w.Header().Set("Content-Type", string(m.MediaType))
w.WriteHeader(http.StatusOK)
if err = json.NewEncoder(w).Encode(m); err != nil {
return errdefs.Unavailable(err)
}
return nil
Expand Down
5 changes: 3 additions & 2 deletions registry/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ 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 619e5d2

Please sign in to comment.