提交 36bb47eb 编写于 作者: S SampsonYe

chore: add unitest for TestTryLoginRegistry

上级 679c3936
......@@ -67,6 +67,7 @@ require (
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d // indirect
github.com/imdario/mergo v0.3.5 // indirect
github.com/isbrick/http-client v0.0.0-20210321135403-0a5df00fdb84 // indirect
github.com/jarcoal/httpmock v1.2.0 // indirect
github.com/json-iterator/go v1.1.8 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
......
package settings
import (
"fmt"
"github.com/jarcoal/httpmock"
"net/http"
"testing"
)
func TestTryLoginRegistry(t *testing.T) {
type args struct {
basicUrl string
username string
password string
insecure bool
authHead string
}
tests := []struct {
name string
args args
wantErr bool
}{
// TODO: Add test cases.
{name: "basic auth pass", args: args{
basicUrl: "www.unitest.com",
username: "abc",
password: "def",
insecure: false,
authHead: "Basic realm=Nexus Docker Registry",
}, wantErr: false},
{name: "basic upper auth pass", args: args{
basicUrl: "www.unitest.com",
username: "abc",
password: "def",
insecure: false,
authHead: "BASIC realm=Nexus Docker Registry",
}, wantErr: false},
{name: "bearer auth pass", args: args{
basicUrl: "www.unitest.com",
username: "abc",
password: "def",
insecure: false,
authHead: `Bearer realm=https://beaer.unitest.com,service="registry.unitest.com"`,
}, wantErr: false},
{name: "bearer complex auth pass", args: args{
basicUrl: "www.unitest.com",
username: "abc",
password: "def",
insecure: false,
authHead: `Bearer realm=https://beaer.unitest.com/auth?tartget=https://abc.def.com,service="registry.unitest.com"`,
}, wantErr: false},
}
httpmock.Activate()
defer httpmock.DeactivateAndReset()
for _, tt := range tests {
httpmock.RegisterResponder("GET", "https://"+tt.args.basicUrl, httpmock.NewStringResponder(200, "ok"))
httpmock.RegisterResponder("GET", fmt.Sprintf("https://%s/v2/", tt.args.basicUrl),
func(req *http.Request) (*http.Response, error) {
if req.Header.Get("Authorization") != "" {
return httpmock.NewStringResponse(200, ""), nil
}
resp := httpmock.NewStringResponse(401, "")
resp.Header.Add("Www-Authenticate", tt.args.authHead)
return resp, nil
},
)
httpmock.RegisterResponder("GET", `=~https://beaer\.unitest\.com.*`, httpmock.NewStringResponder(200, "ok"))
t.Run(tt.name, func(t *testing.T) {
if err := TryLoginRegistry(tt.args.basicUrl, tt.args.username, tt.args.password, tt.args.insecure); (err != nil) != tt.wantErr {
t.Errorf("TryLoginRegistry() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册