暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

configure_config_test.go 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package subconfigs
  2. import (
  3. "testing"
  4. )
  5. // TestConfigureConfigValidation 测试配置中心配置验证
  6. func TestConfigureConfigValidation(t *testing.T) {
  7. tests := []struct {
  8. name string
  9. config *ConfigureConfig
  10. expectError bool
  11. description string
  12. }{
  13. {
  14. name: "Valid token authentication",
  15. config: &ConfigureConfig{
  16. Url: "http://localhost:8080",
  17. Token: "test-token-123",
  18. },
  19. expectError: false,
  20. description: "使用Token认证应该通过验证",
  21. },
  22. {
  23. name: "Valid basic authentication",
  24. config: &ConfigureConfig{
  25. Url: "http://localhost:8080",
  26. Username: "admin",
  27. Password: "123",
  28. },
  29. expectError: false,
  30. description: "使用Basic认证应该通过验证",
  31. },
  32. {
  33. name: "Valid both authentication methods",
  34. config: &ConfigureConfig{
  35. Url: "http://localhost:8080",
  36. Token: "test-token-123",
  37. Username: "admin",
  38. Password: "123",
  39. },
  40. expectError: false,
  41. description: "同时提供Token和Basic认证应该通过验证",
  42. },
  43. {
  44. name: "Missing URL",
  45. config: &ConfigureConfig{
  46. Url: "",
  47. Token: "test-token-123",
  48. },
  49. expectError: true,
  50. description: "缺少URL应该验证失败",
  51. },
  52. {
  53. name: "Missing authentication",
  54. config: &ConfigureConfig{
  55. Url: "http://localhost:8080",
  56. },
  57. expectError: true,
  58. description: "缺少认证方式应该验证失败",
  59. },
  60. {
  61. name: "Basic auth missing username",
  62. config: &ConfigureConfig{
  63. Url: "http://localhost:8080",
  64. Username: "",
  65. Password: "123",
  66. },
  67. expectError: true,
  68. description: "Basic认证缺少用户名应该验证失败",
  69. },
  70. {
  71. name: "Basic auth missing password",
  72. config: &ConfigureConfig{
  73. Url: "http://localhost:8080",
  74. Username: "admin",
  75. Password: "",
  76. },
  77. expectError: true,
  78. description: "Basic认证缺少密码应该验证失败",
  79. },
  80. {
  81. name: "Token auth empty token",
  82. config: &ConfigureConfig{
  83. Url: "http://localhost:8080",
  84. Token: "",
  85. },
  86. expectError: true,
  87. description: "Token认证token为空应该验证失败",
  88. },
  89. }
  90. for _, tt := range tests {
  91. t.Run(tt.name, func(t *testing.T) {
  92. err := tt.config.Validate()
  93. if tt.expectError {
  94. if err == nil {
  95. t.Errorf("%s: 期望错误,但验证通过", tt.description)
  96. } else {
  97. t.Logf("%s: 验证正确返回错误: %v", tt.description, err)
  98. }
  99. } else {
  100. if err != nil {
  101. t.Errorf("%s: 期望验证通过,但得到错误: %v", tt.description, err)
  102. } else {
  103. t.Logf("%s: 验证通过", tt.description)
  104. }
  105. }
  106. })
  107. }
  108. }
  109. // TestConfigureConfigDefaults 测试配置默认值
  110. func TestConfigureConfigDefaults(t *testing.T) {
  111. config := &ConfigureConfig{}
  112. config.SetDefaults()
  113. if config.Url != "http://localhost:8080" {
  114. t.Errorf("期望默认URL: http://localhost:8080, 实际: %s", config.Url)
  115. }
  116. if config.Token != "123" {
  117. t.Errorf("期望默认Token: 123, 实际: %s", config.Token)
  118. }
  119. t.Logf("默认配置: URL=%s, Token=%s", config.Url, config.Token)
  120. }
  121. // TestConfigureConfigIsConfigured 测试配置检查
  122. func TestConfigureConfigIsConfigured(t *testing.T) {
  123. tests := []struct {
  124. name string
  125. config *ConfigureConfig
  126. expected bool
  127. }{
  128. {
  129. name: "Configured with URL",
  130. config: &ConfigureConfig{
  131. Url: "http://localhost:8080",
  132. },
  133. expected: true,
  134. },
  135. {
  136. name: "Not configured empty URL",
  137. config: &ConfigureConfig{
  138. Url: "",
  139. },
  140. expected: false,
  141. },
  142. }
  143. for _, tt := range tests {
  144. t.Run(tt.name, func(t *testing.T) {
  145. result := tt.config.IsConfigured()
  146. if result != tt.expected {
  147. t.Errorf("期望IsConfigured()返回 %v, 实际: %v", tt.expected, result)
  148. } else {
  149. t.Logf("IsConfigured()正确返回: %v", result)
  150. }
  151. })
  152. }
  153. }
  154. // TestConfigureConfigAuthFields 测试认证字段存在性
  155. func TestConfigureConfigAuthFields(t *testing.T) {
  156. // 测试结构体字段是否包含新的认证字段
  157. config := &ConfigureConfig{
  158. Url: "http://localhost:8080",
  159. Token: "test-token",
  160. Username: "test-user",
  161. Password: "test-pass",
  162. }
  163. if config.Url == "" {
  164. t.Error("URL字段不存在或为空")
  165. }
  166. if config.Token == "" {
  167. t.Error("Token字段不存在或为空")
  168. }
  169. if config.Username == "" {
  170. t.Error("Username字段不存在或为空")
  171. }
  172. if config.Password == "" {
  173. t.Error("Password字段不存在或为空")
  174. }
  175. t.Logf("配置中心配置包含所有认证字段: URL=%s, Token=%s, Username=%s, Password=%s",
  176. config.Url, config.Token, config.Username, config.Password)
  177. }