暫無描述
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_svc_test.go 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 TestCreateConfigStartupSVC(t *testing.T) {
  14. // 清除测试缓存
  15. cmd := exec.Command("go", "clean", "-testcache")
  16. if err := cmd.Run(); err != nil {
  17. t.Logf("清除测试缓存失败: %v", err)
  18. // 继续执行测试
  19. }
  20. httpClient := &http.Client{}
  21. url := "http://localhost:8080/api/create/config/startup/svc-mcp"
  22. data := configreq.ConfigRequest{
  23. YamlRoot: "service",
  24. ConfigFields: map[string]*configreq.ConfigField{
  25. "secret_key": {
  26. YamlName: "secret_key",
  27. YamlValue: "123wad",
  28. },
  29. "write_timeout": {
  30. YamlName: "write_timeout",
  31. YamlValue: "90",
  32. },
  33. "read_timeout": {
  34. YamlName: "read_timeout",
  35. YamlValue: "80",
  36. },
  37. },
  38. }
  39. jsonData, err := json.Marshal(data)
  40. req, err := http.NewRequest("POST", url, bytes.NewReader(jsonData))
  41. if err != nil {
  42. log.Printf("NewRequest:%v", err)
  43. }
  44. // Basic Auth 认证
  45. //req.SetBasicAuth("username", "password")
  46. req.Header.Set("Authorization", "Bearer 123")
  47. req.Header.Set("Content-Type", "application/json")
  48. resp, err := httpClient.Do(req)
  49. if err != nil {
  50. fmt.Printf("发送请求失败: %v\n", err)
  51. return
  52. }
  53. defer resp.Body.Close()
  54. // 读取响应
  55. body, err := io.ReadAll(resp.Body)
  56. if err != nil {
  57. fmt.Printf("读取响应失败: %v\n", err)
  58. return
  59. }
  60. fmt.Printf("状态码: %d\n", resp.StatusCode)
  61. fmt.Printf("响应体: %s\n", string(body))
  62. }
  63. func TestDelConfigStartupSVC(t *testing.T) {
  64. httpClient := &http.Client{}
  65. url := "http://localhost:8080/api/delete/config/startup/svc-mcp"
  66. data := configreq.ConfigRequest{
  67. YamlRoot: "service",
  68. ConfigFields: map[string]*configreq.ConfigField{
  69. "secret_key": {
  70. YamlName: "secret_key",
  71. },
  72. "write_timeout": {
  73. YamlName: "write_timeout",
  74. },
  75. },
  76. }
  77. jsonData, err := json.Marshal(data)
  78. req, err := http.NewRequest("POST", url, bytes.NewReader(jsonData))
  79. if err != nil {
  80. log.Printf("NewRequest:%v", err)
  81. }
  82. // Basic Auth 认证
  83. req.SetBasicAuth("admin", "123")
  84. //req.Header.Set("Authorization", "Bearer 123")
  85. req.Header.Set("Content-Type", "application/json")
  86. resp, err := httpClient.Do(req)
  87. if err != nil {
  88. fmt.Printf("发送请求失败: %v\n", err)
  89. return
  90. }
  91. defer resp.Body.Close()
  92. // 读取响应
  93. body, err := io.ReadAll(resp.Body)
  94. if err != nil {
  95. fmt.Printf("读取响应失败: %v\n", err)
  96. return
  97. }
  98. fmt.Printf("状态码: %d\n", resp.StatusCode)
  99. fmt.Printf("响应体: %s\n", string(body))
  100. }