package playback import ( "errors" "strings" "testing" "github.com/stretchr/testify/require" ) var errNoMatches = errors.New("no matches found") func withoutValue(data map[string]string, field string) map[string]string { result := make(map[string]string) for k, v := range data { result[k] = v } result[field] = "" return result } func TestParseAuthHeader(t *testing.T) { defaultHeader := "AWS4-HMAC-SHA256 Credential=oid0cid/20210809/us-east-1/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=2811ccb9e242f41426738fb1f" defaultAuthInfo := map[string]string{ "access_key_id": "oid0cid", "service": "s3", "region": "us-east-1", "v4_signature": "2811ccb9e242f41426738fb1f", "signed_header_fields": "host;x-amz-content-sha256;x-amz-date", "date": "20210809", } for _, tc := range []struct { title string header string err error expected map[string]string }{ { title: "correct full header", header: defaultHeader, err: nil, expected: defaultAuthInfo, }, { title: "correct with empty region", header: strings.Replace(defaultHeader, "/us-east-1/", "//", 1), err: nil, expected: withoutValue(defaultAuthInfo, "region"), }, { title: "empty access key", header: strings.Replace(defaultHeader, "oid0cid", "", 1), err: errNoMatches, expected: nil, }, { title: "empty service", header: strings.Replace(defaultHeader, "/s3/", "//", 1), err: errNoMatches, expected: nil, }, { title: "empty date", header: strings.Replace(defaultHeader, "/20210809/", "//", 1), err: errNoMatches, expected: nil, }, { title: "empty v4_signature", header: strings.Replace(defaultHeader, "Signature=2811ccb9e242f41426738fb1f", "Signature=", 1), err: errNoMatches, expected: nil, }, { title: "empty signed_fields", header: strings.Replace(defaultHeader, "SignedHeaders=host;x-amz-content-sha256;x-amz-date", "SignedHeaders=", 1), err: errNoMatches, expected: nil, }, { title: "empty signed_fields", header: strings.Replace(defaultHeader, "SignedHeaders=host;x-amz-content-sha256;x-amz-date", "SignedHeaders=", 1), err: errNoMatches, expected: nil, }, } { t.Run(tc.title, func(t *testing.T) { authInfo, err := parseAuthHeader(tc.header) require.Equal(t, err, tc.err, tc.header) require.Equal(t, tc.expected, authInfo, tc.header) }) } }