mirror of
https://github.com/genuinetools/reg.git
synced 2024-09-28 11:46:20 -04:00
65b2c0329d
Rather than returning an error that requires pattern-matching from `parseChallenge` when the challenge header requires basic authentication, return a distinguished error value. This makes checking for this error a bit easier. This commit also updates the check in `r.Headers` to use the new error value and adds a couple of regression tests.
38 lines
922 B
Go
38 lines
922 B
Go
package registry
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
)
|
|
|
|
func TestErrBasicAuth(t *testing.T) {
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/" {
|
|
w.Header().Set("www-authenticate", `Basic realm="Registry Realm",service="Docker registry"`)
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
} else {
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
}))
|
|
defer ts.Close()
|
|
|
|
authConfig := types.AuthConfig{
|
|
Username: "j3ss",
|
|
Password: "ss3j",
|
|
ServerAddress: ts.URL,
|
|
}
|
|
r, err := New(authConfig, Opt{Insecure: true, Debug: true})
|
|
if err != nil {
|
|
t.Fatalf("expected no error creating client, got %v", err)
|
|
}
|
|
token, err := r.Token(ts.URL)
|
|
if err != ErrBasicAuth {
|
|
t.Fatalf("expected ErrBasicAuth getting token, got %v", err)
|
|
}
|
|
if token != "" {
|
|
t.Fatalf("expected empty token, got %v", err)
|
|
}
|
|
}
|