Source file src/cmd/go/internal/auth/gitauth_test.go

     1  package auth
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestParseGitAuth(t *testing.T) {
     8  	testCases := []struct {
     9  		gitauth      string // contents of 'git credential fill'
    10  		wantPrefix   string
    11  		wantUsername string
    12  		wantPassword string
    13  	}{
    14  		{ // Standard case.
    15  			gitauth: `
    16  protocol=https
    17  host=example.com
    18  username=bob
    19  password=secr3t
    20  `,
    21  			wantPrefix:   "https://example.com",
    22  			wantUsername: "bob",
    23  			wantPassword: "secr3t",
    24  		},
    25  		{ // Should not use an invalid url.
    26  			gitauth: `
    27  protocol=https
    28  host=example.com
    29  username=bob
    30  password=secr3t
    31  url=invalid
    32  `,
    33  			wantPrefix:   "https://example.com",
    34  			wantUsername: "bob",
    35  			wantPassword: "secr3t",
    36  		},
    37  		{ // Should use the new url.
    38  			gitauth: `
    39  protocol=https
    40  host=example.com
    41  username=bob
    42  password=secr3t
    43  url=https://go.dev
    44  `,
    45  			wantPrefix:   "https://go.dev",
    46  			wantUsername: "bob",
    47  			wantPassword: "secr3t",
    48  		},
    49  		{ // Empty data.
    50  			gitauth: `
    51  `,
    52  			wantPrefix:   "",
    53  			wantUsername: "",
    54  			wantPassword: "",
    55  		},
    56  		{ // Does not follow the '=' format.
    57  			gitauth: `
    58  protocol:https
    59  host:example.com
    60  username:bob
    61  password:secr3t
    62  `,
    63  			wantPrefix:   "",
    64  			wantUsername: "",
    65  			wantPassword: "",
    66  		},
    67  	}
    68  	for _, tc := range testCases {
    69  		parsedPrefix, username, password := parseGitAuth([]byte(tc.gitauth))
    70  		if parsedPrefix != tc.wantPrefix {
    71  			t.Errorf("parseGitAuth(%s):\nhave %q\nwant %q", tc.gitauth, parsedPrefix, tc.wantPrefix)
    72  		}
    73  		if username != tc.wantUsername {
    74  			t.Errorf("parseGitAuth(%s):\nhave %q\nwant %q", tc.gitauth, username, tc.wantUsername)
    75  		}
    76  		if password != tc.wantPassword {
    77  			t.Errorf("parseGitAuth(%s):\nhave %q\nwant %q", tc.gitauth, password, tc.wantPassword)
    78  		}
    79  	}
    80  }
    81  

View as plain text