59 lines
1.6 KiB
Go
59 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestValidateJWT(t *testing.T) {
|
|
// Case 1: Strong Secret
|
|
err := ValidateJWT("12345678901234567890123456789012", "production")
|
|
if err != nil {
|
|
t.Errorf("Expected nil error for strong secret, got %v", err)
|
|
}
|
|
|
|
// Case 2: Weak Secret (Development)
|
|
err = ValidateJWT("weak", "development")
|
|
if err == nil {
|
|
t.Error("Expected warning error for weak secret")
|
|
} else if strings.HasPrefix(err.Error(), "FATAL") {
|
|
t.Error("Did not expect FATAL error for weak secret in dev")
|
|
}
|
|
|
|
// Case 3: Weak Secret (Production)
|
|
err = ValidateJWT("weak", "production")
|
|
if err == nil {
|
|
t.Error("Expected error for weak secret")
|
|
} else if !strings.HasPrefix(err.Error(), "FATAL") {
|
|
t.Error("Expected FATAL error for weak secret in production")
|
|
}
|
|
}
|
|
|
|
func TestConfigureSwagger(t *testing.T) {
|
|
// Case 1: HTTPS
|
|
host, schemes := ConfigureSwagger("https://api.example.com")
|
|
if host != "api.example.com" {
|
|
t.Errorf("Expected api.example.com, got %s", host)
|
|
}
|
|
if len(schemes) != 1 || schemes[0] != "https" {
|
|
t.Errorf("Expected [https], got %v", schemes)
|
|
}
|
|
|
|
// Case 2: HTTP
|
|
host, schemes = ConfigureSwagger("http://localhost:8080")
|
|
if host != "localhost:8080" {
|
|
t.Errorf("Expected localhost:8080, got %s", host)
|
|
}
|
|
if len(schemes) != 1 || schemes[0] != "http" {
|
|
t.Errorf("Expected [http], got %v", schemes)
|
|
}
|
|
|
|
// Case 3: No Scheme
|
|
host, schemes = ConfigureSwagger("api.example.com")
|
|
if host != "api.example.com" {
|
|
t.Errorf("Expected api.example.com, got %s", host)
|
|
}
|
|
if len(schemes) != 2 {
|
|
t.Errorf("Expected default schemes, got %v", schemes)
|
|
}
|
|
}
|