From ff003928b2360304f6b9458d324df090917fab02 Mon Sep 17 00:00:00 2001 From: Miloslav Trmač Date: Sat, 11 Sep 2021 20:24:07 +0200 Subject: Add unit tests for singleAuthHeader MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also rename it to parseSingleAuthHeader Should not change behavior. Signed-off-by: Miloslav Trmač --- pkg/auth/auth_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'pkg/auth/auth_test.go') diff --git a/pkg/auth/auth_test.go b/pkg/auth/auth_test.go index 97e7fe1ec..a0b97b106 100644 --- a/pkg/auth/auth_test.go +++ b/pkg/auth/auth_test.go @@ -68,6 +68,43 @@ func TestAuthConfigsToAuthFile(t *testing.T) { } } +func TestParseSingleAuthHeader(t *testing.T) { + for _, tc := range []struct { + input string + shouldErr bool + expected map[string]types.DockerAuthConfig + }{ + { + input: "", // An empty (or missing) header + expected: map[string]types.DockerAuthConfig{"0": {}}, + }, + { + input: "null", + expected: map[string]types.DockerAuthConfig{"0": {}}, + }, + // Invalid JSON + {input: "@", shouldErr: true}, + // Success + { + input: base64.URLEncoding.EncodeToString([]byte(`{"username":"u1","password":"p1"}`)), + expected: map[string]types.DockerAuthConfig{ + "0": {Username: "u1", Password: "p1"}, + }, + }, + } { + req, err := http.NewRequest(http.MethodPost, "/", nil) + require.NoError(t, err, tc.input) + req.Header.Set(XRegistryAuthHeader.String(), tc.input) + res, err := parseSingleAuthHeader(req) + if tc.shouldErr { + assert.Error(t, err, tc.input) + } else { + require.NoError(t, err, tc.input) + assert.Equal(t, tc.expected, res, tc.input) + } + } +} + func TestParseMultiAuthHeader(t *testing.T) { for _, tc := range []struct { input string -- cgit v1.2.3-54-g00ecf