package fetcher import ( "encoding/base64" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "net/http" "testing" ) func TestApplyBearerAuthentication(test *testing.T) { request, _ := http.NewRequest(http.MethodGet, "https://example.com/feed", nil) authenticationError := ApplyAuthentication(request, AuthenticationConfiguration{ AuthenticationType: "bearer", AuthenticationValue: "my-secret-token", }) require.NoError(test, authenticationError) assert.Equal(test, "Bearer my-secret-token", request.Header.Get("Authorization")) } func TestApplyBasicAuthentication(test *testing.T) { request, _ := http.NewRequest(http.MethodGet, "https://example.com/feed", nil) authenticationError := ApplyAuthentication(request, AuthenticationConfiguration{ AuthenticationType: "basic", AuthenticationValue: "user:pass", }) require.NoError(test, authenticationError) expectedEncoded := base64.StdEncoding.EncodeToString([]byte("user:pass")) assert.Equal(test, "Basic "+expectedEncoded, request.Header.Get("Authorization")) } func TestApplyQueryParamAuthentication(test *testing.T) { request, _ := http.NewRequest(http.MethodGet, "https://example.com/feed?existing=value", nil) authenticationError := ApplyAuthentication(request, AuthenticationConfiguration{ AuthenticationType: "query_param", AuthenticationValue: "api_key=abc123", }) require.NoError(test, authenticationError) assert.Equal(test, "abc123", request.URL.Query().Get("api_key")) assert.Equal(test, "value", request.URL.Query().Get("existing")) } func TestApplyEmptyAuthenticationType(test *testing.T) { request, _ := http.NewRequest(http.MethodGet, "https://example.com/feed", nil) authenticationError := ApplyAuthentication(request, AuthenticationConfiguration{ AuthenticationType: "", AuthenticationValue: "", }) require.NoError(test, authenticationError) assert.Empty(test, request.Header.Get("Authorization")) } func TestApplyUnsupportedAuthenticationType(test *testing.T) { request, _ := http.NewRequest(http.MethodGet, "https://example.com/feed", nil) authenticationError := ApplyAuthentication(request, AuthenticationConfiguration{ AuthenticationType: "oauth2", AuthenticationValue: "token", }) assert.Error(test, authenticationError) assert.Contains(test, authenticationError.Error(), "unsupported") } func TestApplyQueryParamInvalidFormat(test *testing.T) { request, _ := http.NewRequest(http.MethodGet, "https://example.com/feed", nil) authenticationError := ApplyAuthentication(request, AuthenticationConfiguration{ AuthenticationType: "query_param", AuthenticationValue: "no-equals-sign", }) assert.Error(test, authenticationError) assert.Contains(test, authenticationError.Error(), "param_name=value") } func TestApplyQueryParamWithEqualsInValue(test *testing.T) { request, _ := http.NewRequest(http.MethodGet, "https://example.com/feed", nil) authenticationError := ApplyAuthentication(request, AuthenticationConfiguration{ AuthenticationType: "query_param", AuthenticationValue: "token=abc=def", }) require.NoError(test, authenticationError) assert.Equal(test, "abc=def", request.URL.Query().Get("token")) }