| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- package subconfigs
-
- import (
- "testing"
- )
-
- // TestConfigureConfigValidation 测试配置中心配置验证
- func TestConfigureConfigValidation(t *testing.T) {
- tests := []struct {
- name string
- config *ConfigureConfig
- expectError bool
- description string
- }{
- {
- name: "Valid token authentication",
- config: &ConfigureConfig{
- Url: "http://localhost:8080",
- Token: "test-token-123",
- },
- expectError: false,
- description: "使用Token认证应该通过验证",
- },
- {
- name: "Valid basic authentication",
- config: &ConfigureConfig{
- Url: "http://localhost:8080",
- Username: "admin",
- Password: "123",
- },
- expectError: false,
- description: "使用Basic认证应该通过验证",
- },
- {
- name: "Valid both authentication methods",
- config: &ConfigureConfig{
- Url: "http://localhost:8080",
- Token: "test-token-123",
- Username: "admin",
- Password: "123",
- },
- expectError: false,
- description: "同时提供Token和Basic认证应该通过验证",
- },
- {
- name: "Missing URL",
- config: &ConfigureConfig{
- Url: "",
- Token: "test-token-123",
- },
- expectError: true,
- description: "缺少URL应该验证失败",
- },
- {
- name: "Missing authentication",
- config: &ConfigureConfig{
- Url: "http://localhost:8080",
- },
- expectError: true,
- description: "缺少认证方式应该验证失败",
- },
- {
- name: "Basic auth missing username",
- config: &ConfigureConfig{
- Url: "http://localhost:8080",
- Username: "",
- Password: "123",
- },
- expectError: true,
- description: "Basic认证缺少用户名应该验证失败",
- },
- {
- name: "Basic auth missing password",
- config: &ConfigureConfig{
- Url: "http://localhost:8080",
- Username: "admin",
- Password: "",
- },
- expectError: true,
- description: "Basic认证缺少密码应该验证失败",
- },
- {
- name: "Token auth empty token",
- config: &ConfigureConfig{
- Url: "http://localhost:8080",
- Token: "",
- },
- expectError: true,
- description: "Token认证token为空应该验证失败",
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- err := tt.config.Validate()
-
- if tt.expectError {
- if err == nil {
- t.Errorf("%s: 期望错误,但验证通过", tt.description)
- } else {
- t.Logf("%s: 验证正确返回错误: %v", tt.description, err)
- }
- } else {
- if err != nil {
- t.Errorf("%s: 期望验证通过,但得到错误: %v", tt.description, err)
- } else {
- t.Logf("%s: 验证通过", tt.description)
- }
- }
- })
- }
- }
-
- // TestConfigureConfigDefaults 测试配置默认值
- func TestConfigureConfigDefaults(t *testing.T) {
- config := &ConfigureConfig{}
- config.SetDefaults()
-
- if config.Url != "http://localhost:8080" {
- t.Errorf("期望默认URL: http://localhost:8080, 实际: %s", config.Url)
- }
-
- if config.Token != "123" {
- t.Errorf("期望默认Token: 123, 实际: %s", config.Token)
- }
-
- t.Logf("默认配置: URL=%s, Token=%s", config.Url, config.Token)
- }
-
- // TestConfigureConfigIsConfigured 测试配置检查
- func TestConfigureConfigIsConfigured(t *testing.T) {
- tests := []struct {
- name string
- config *ConfigureConfig
- expected bool
- }{
- {
- name: "Configured with URL",
- config: &ConfigureConfig{
- Url: "http://localhost:8080",
- },
- expected: true,
- },
- {
- name: "Not configured empty URL",
- config: &ConfigureConfig{
- Url: "",
- },
- expected: false,
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result := tt.config.IsConfigured()
- if result != tt.expected {
- t.Errorf("期望IsConfigured()返回 %v, 实际: %v", tt.expected, result)
- } else {
- t.Logf("IsConfigured()正确返回: %v", result)
- }
- })
- }
- }
-
- // TestConfigureConfigAuthFields 测试认证字段存在性
- func TestConfigureConfigAuthFields(t *testing.T) {
- // 测试结构体字段是否包含新的认证字段
- config := &ConfigureConfig{
- Url: "http://localhost:8080",
- Token: "test-token",
- Username: "test-user",
- Password: "test-pass",
- }
-
- if config.Url == "" {
- t.Error("URL字段不存在或为空")
- }
- if config.Token == "" {
- t.Error("Token字段不存在或为空")
- }
- if config.Username == "" {
- t.Error("Username字段不存在或为空")
- }
- if config.Password == "" {
- t.Error("Password字段不存在或为空")
- }
-
- t.Logf("配置中心配置包含所有认证字段: URL=%s, Token=%s, Username=%s, Password=%s",
- config.Url, config.Token, config.Username, config.Password)
- }
|