From ab8cf0bebe03dfca59600973979b0ffe6cb51991 Mon Sep 17 00:00:00 2001 From: Rohan Kumar Date: Mon, 20 Jan 2025 21:11:17 +0530 Subject: [PATCH] test (validations) : Add some unit tests around proxy validation (#4411) Add some unit tests to test various values provided to these validation methods: - validateHTTPProxy - validateHTTPSProxy - validateNoProxy Signed-off-by: Rohan Kumar --- pkg/crc/config/validations_test.go | 83 ++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/pkg/crc/config/validations_test.go b/pkg/crc/config/validations_test.go index 5eb60847e3..ad372d3833 100644 --- a/pkg/crc/config/validations_test.go +++ b/pkg/crc/config/validations_test.go @@ -53,3 +53,86 @@ func TestValidationPreset_WhenOKDProvidedOnArmArchitecture_thenValidationFailure assert.Equal(t, false, validationPass) assert.Equal(t, fmt.Sprintf("preset 'okd' is not supported on %s architecture, please use different preset value", runtime.GOARCH), validationMessage) } + +func TestValidateHTTPProxy(t *testing.T) { + tests := []struct { + name string + noProxyValue string + expectedValidationResult bool + expectedValidationStr string + }{ + {"empty value", "", true, ""}, + {"valid http url", "http://proxy.example.com", true, ""}, + {"valid https url", "https://proxy.example.com", false, "HTTP proxy URL 'https://proxy.example.com' is not valid: url should start with http://"}, + {"valid socks5 url", "socks5://proxy.example.com", false, "HTTP proxy URL 'socks5://proxy.example.com' is not valid: url should start with http://"}, + {"type in http scheme", "htp://proxy.example.com", false, "HTTP proxy URL 'htp://proxy.example.com' is not valid: url should start with http://"}, + {"no scheme", "proxy.example.com", false, "HTTP proxy URL 'proxy.example.com' is not valid: url should start with http://"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + actualValidationResult, actualValidationStr := validateHTTPProxy(tt.noProxyValue) + if actualValidationStr != tt.expectedValidationStr { + t.Errorf("validateHTTPProxy(%s) : got %v, want %v", tt.noProxyValue, actualValidationStr, tt.expectedValidationStr) + } + if actualValidationResult != tt.expectedValidationResult { + t.Errorf("validateHTTPProxy(%s) : got %v, want %v", tt.noProxyValue, actualValidationResult, tt.expectedValidationResult) + } + }) + } +} + +func TestValidateHTTPSProxy(t *testing.T) { + tests := []struct { + name string + noProxyValue string + expectedValidationResult bool + expectedValidationStr string + }{ + {"empty value", "", true, ""}, + {"valid https url", "https://proxy.example.com", true, ""}, + {"valid http url", "http://proxy.example.com", true, ""}, + {"valid socks5 url", "socks5://proxy.example.com", false, "HTTPS proxy URL 'socks5://proxy.example.com' is not valid: url should start with http:// or https://"}, + {"type in https scheme", "htps://proxy.example.com", false, "HTTPS proxy URL 'htps://proxy.example.com' is not valid: url should start with http:// or https://"}, + {"no scheme", "proxy.example.com", false, "HTTPS proxy URL 'proxy.example.com' is not valid: url should start with http:// or https://"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + actualValidationResult, actualValidationStr := validateHTTPSProxy(tt.noProxyValue) + if actualValidationStr != tt.expectedValidationStr { + t.Errorf("validateHTTPSProxy(%s) : got %v, want %v", tt.noProxyValue, actualValidationStr, tt.expectedValidationStr) + } + if actualValidationResult != tt.expectedValidationResult { + t.Errorf("validateHTTPSProxy(%s) : got %v, want %v", tt.noProxyValue, actualValidationResult, tt.expectedValidationResult) + } + }) + } +} + +func TestValidateNoProxy(t *testing.T) { + tests := []struct { + name string + noProxyValue string + expectedValidationResult bool + expectedValidationStr string + }{ + {"empty value", "", true, ""}, + {"valid single", "example.com", true, ""}, + {"valid multiple", "localhost,127.0.0.1,example.com", true, ""}, + {"space in single entry", "example .com", false, "NoProxy string can't contain spaces"}, + {"space in between multiple entries", "localhost, , example.com", false, "NoProxy string can't contain spaces"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + actualValidationResult, actualValidationStr := validateNoProxy(tt.noProxyValue) + if actualValidationStr != tt.expectedValidationStr { + t.Errorf("validateNoProxy(%s) : got %v, want %v", tt.noProxyValue, actualValidationStr, tt.expectedValidationStr) + } + if actualValidationResult != tt.expectedValidationResult { + t.Errorf("validateNoProxy(%s) : got %v, want %v", tt.noProxyValue, actualValidationResult, tt.expectedValidationResult) + } + }) + } +}