mirror of
https://github.com/docker/cli.git
synced 2026-11-06 13:10:41 -04:00
translateV2AuthError only unwraps a url.Error from Do(); a 401 status never went through it. Use errhttp.ToNative so Auth() treats that 401 as unauthorized and stops. Signed-off-by: Dean Chen <862469039@qq.com> Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
42 lines
1.2 KiB
Go
42 lines
1.2 KiB
Go
package registry
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"testing"
|
|
|
|
"github.com/containerd/errdefs"
|
|
"github.com/moby/moby/api/types/registry"
|
|
"gotest.tools/v3/assert"
|
|
is "gotest.tools/v3/assert/cmp"
|
|
)
|
|
|
|
func TestLoginV2BasicAuthUnauthorized(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
user, pass, ok := r.BasicAuth()
|
|
if !ok || user != "alice" || pass != "secret" {
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="test"`)
|
|
http.Error(w, "401 Unauthorized", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusOK)
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
|
|
u, err := url.Parse(srv.URL)
|
|
assert.NilError(t, err)
|
|
endpoint := APIEndpoint{URL: u}
|
|
ctx := context.Background()
|
|
|
|
_, err = loginV2(ctx, ®istry.AuthConfig{Username: "alice", Password: "wrong"}, endpoint, "docker-test")
|
|
assert.ErrorContains(t, err, "401")
|
|
assert.Check(t, errdefs.IsUnauthorized(err))
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsUnauthorized))
|
|
|
|
token, err := loginV2(ctx, ®istry.AuthConfig{Username: "alice", Password: "secret"}, endpoint, "docker-test")
|
|
assert.NilError(t, err)
|
|
assert.Check(t, is.Equal("", token))
|
|
}
|