Нема описа
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

my_config_test.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "log"
  8. "net/http"
  9. "os/exec"
  10. "testing"
  11. "git.x2erp.com/qdy/go-base/model/request/configreq"
  12. )
  13. func TestConfigMeta(t *testing.T) {
  14. httpClient := &http.Client{}
  15. url := "http://localhost:8080/api/init/config/meta"
  16. req, err := http.NewRequest("POST", url, nil)
  17. if err != nil {
  18. log.Printf("NewRequest:%v", err)
  19. }
  20. req.Header.Set("Authorization", "Bearer 123")
  21. req.Header.Set("Content-Type", "application/json")
  22. resp, err := httpClient.Do(req)
  23. if err != nil {
  24. fmt.Printf("发送请求失败: %v\n", err)
  25. return
  26. }
  27. defer resp.Body.Close()
  28. // 读取响应
  29. body, err := io.ReadAll(resp.Body)
  30. if err != nil {
  31. fmt.Printf("读取响应失败: %v\n", err)
  32. return
  33. }
  34. fmt.Printf("状态码: %d\n", resp.StatusCode)
  35. fmt.Printf("响应体: %s\n", string(body))
  36. }
  37. func TestCreateConfigStartup(t *testing.T) {
  38. // 清除测试缓存
  39. cmd := exec.Command("go", "clean", "-testcache")
  40. if err := cmd.Run(); err != nil {
  41. t.Logf("清除测试缓存失败: %v", err)
  42. // 继续执行测试
  43. }
  44. httpClient := &http.Client{}
  45. url := "http://localhost:8080/api/create/config/startup"
  46. data := configreq.ConfigRequest{
  47. YamlRoot: "service",
  48. ConfigFields: map[string]*configreq.ConfigField{
  49. "secret_key": {
  50. YamlName: "secret_key",
  51. YamlValue: "123wad",
  52. },
  53. "write_timeout": {
  54. YamlName: "write_timeout",
  55. YamlValue: "90",
  56. },
  57. "read_timeout": {
  58. YamlName: "read_timeout",
  59. YamlValue: "80",
  60. },
  61. },
  62. }
  63. jsonData, err := json.Marshal(data)
  64. req, err := http.NewRequest("POST", url, bytes.NewReader(jsonData))
  65. if err != nil {
  66. log.Printf("NewRequest:%v", err)
  67. }
  68. // Basic Auth 认证
  69. //req.SetBasicAuth("username", "password")
  70. req.Header.Set("Authorization", "Bearer 123")
  71. req.Header.Set("Content-Type", "application/json")
  72. resp, err := httpClient.Do(req)
  73. if err != nil {
  74. fmt.Printf("发送请求失败: %v\n", err)
  75. return
  76. }
  77. defer resp.Body.Close()
  78. // 读取响应
  79. body, err := io.ReadAll(resp.Body)
  80. if err != nil {
  81. fmt.Printf("读取响应失败: %v\n", err)
  82. return
  83. }
  84. fmt.Printf("状态码: %d\n", resp.StatusCode)
  85. fmt.Printf("响应体: %s\n", string(body))
  86. }
  87. func TestDelConfigStartup(t *testing.T) {
  88. httpClient := &http.Client{}
  89. url := "http://localhost:8080/api/delete/config/startup"
  90. data := configreq.ConfigRequest{
  91. YamlRoot: "service",
  92. ConfigFields: map[string]*configreq.ConfigField{
  93. "secret_key": {
  94. YamlName: "secret_key",
  95. },
  96. "write_timeout": {
  97. YamlName: "write_timeout",
  98. },
  99. "read_timeout": {
  100. YamlName: "read_timeout",
  101. },
  102. },
  103. }
  104. jsonData, err := json.Marshal(data)
  105. req, err := http.NewRequest("POST", url, bytes.NewReader(jsonData))
  106. if err != nil {
  107. log.Printf("NewRequest:%v", err)
  108. }
  109. // Basic Auth 认证
  110. req.SetBasicAuth("admin", "123")
  111. //req.Header.Set("Authorization", "Bearer 123")
  112. req.Header.Set("Content-Type", "application/json")
  113. resp, err := httpClient.Do(req)
  114. if err != nil {
  115. fmt.Printf("发送请求失败: %v\n", err)
  116. return
  117. }
  118. defer resp.Body.Close()
  119. // 读取响应
  120. body, err := io.ReadAll(resp.Body)
  121. if err != nil {
  122. fmt.Printf("读取响应失败: %v\n", err)
  123. return
  124. }
  125. fmt.Printf("状态码: %d\n", resp.StatusCode)
  126. fmt.Printf("响应体: %s\n", string(body))
  127. }