Skip to content

Commit

Permalink
test(proxy): fix /v2 endpoint test
Browse files Browse the repository at this point in the history
  • Loading branch information
plaffitt authored and donch committed Dec 1, 2023
1 parent 914fbdd commit 0acf3f0
Showing 1 changed file with 2 additions and 30 deletions.
32 changes: 2 additions & 30 deletions internal/proxy/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"net/http/httptest"
"testing"

"github.com/enix/kube-image-keeper/internal/registry"
"github.com/gin-gonic/gin"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/ghttp"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -36,46 +34,20 @@ func TestNew(t *testing.T) {
func Test_v2Endpoint(t *testing.T) {
g := NewWithT(t)

// mock server
gh := ghttp.NewGHTTPWithGomega(g)
server := ghttp.NewServer()
defer server.Close()
server.AppendHandlers(
ghttp.CombineHandlers(
gh.VerifyRequest(http.MethodGet, "/v2"),
gh.RespondWith(401, nil, http.Header{
"Www-Authenticate": []string{
"Bearer realm=\"" + server.URL() + "/token\",service=\"registry.docker.io\",scope=\"repository:samalba/my-app:pull,push\"",
},
}),
),
ghttp.CombineHandlers(
gh.VerifyRequest(http.MethodGet, "/token"),
gh.RespondWithJSONEncoded(http.StatusOK, &Bearer{
Token: "token",
ExpiresIn: "3600",
}),
),
ghttp.CombineHandlers(
gh.VerifyRequest(http.MethodGet, "/v2"),
gh.RespondWith(200, "v2 endpoint"),
),
)

// mock proxy server
recorder := httptest.NewRecorder()
ctx, r := gin.CreateTestContext(recorder)
proxy := NewWithEngine(dummyK8sClient, r).Serve()

// mock request
registry.Endpoint = server.Addr()
request, err := http.NewRequest("GET", "/v2", nil)
g.Expect(err).To(BeNil())
ctx.Request = request
ctx.Writer = &ResponseWriterPatched{ctx.Writer}

proxy.v2Endpoint(ctx)
g.Expect(recorder.Body.String()).To(Equal("v2 endpoint"))
g.Expect(recorder.Result().StatusCode).To(Equal(http.StatusOK))
g.Expect(recorder.Body.String()).To(Equal("{}"))
}

func Test_handleOriginRegistryPort(t *testing.T) {
Expand Down

0 comments on commit 0acf3f0

Please sign in to comment.