Няма описание
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package test
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "testing"
  9. "time"
  10. "git.x2erp.com/qdy/go-base/types"
  11. )
  12. // type ExchangeRequest struct {
  13. // ChannelName string `json:"channel_name"`
  14. // ExchangeName string `json:"exchange_name"`
  15. // ExchangeType string `json:"exchange_type"`
  16. // Durable bool `json:"durable"`
  17. // Internal bool `json:"internal"`
  18. // AutoDelete bool `json:"auto_delete"`
  19. // NoWait bool `json:"no_wait"`
  20. // Arguments map[string]string `json:"arguments"`
  21. // }
  22. func TestCreateexchange(t *testing.T) {
  23. // 1. 准备请求数据
  24. requestData := types.ExchangeRequest{
  25. ChannelName: "v_bdx_channel",
  26. ExchangeName: "v_bdx_exchange",
  27. ExchangeType: "topic", // direct, fanout, topic, headers
  28. Durable: true,
  29. Internal: false,
  30. AutoDelete: false,
  31. NoWait: false,
  32. Arguments: map[string]string{
  33. "x-delayed-type": "topic",
  34. },
  35. }
  36. // 消息队列
  37. queueRequest := types.QueueRequest{
  38. QueueName: "vbdx_queue_count",
  39. ChannelName: requestData.ChannelName,
  40. }
  41. queueBindRequest := types.QueueBindRequest{
  42. //ChannelName: requestData.ChannelName,
  43. QueueName: queueRequest.QueueName,
  44. ExchangeName: requestData.ExchangeName,
  45. RoutingKey: "v-bdx.#",
  46. }
  47. // 2. 将数据转换为JSON
  48. jsonData, err := json.Marshal(requestData)
  49. if err != nil {
  50. fmt.Printf("JSON编码失败: %v\n", err)
  51. return
  52. }
  53. // 3. 发送HTTP POST请求
  54. url := "http://localhost:9090/api/rabbitmq/exchange/create"
  55. POST(jsonData, url)
  56. //建立队列
  57. CreateQueue(queueRequest)
  58. //绑定
  59. CreateQueueBind(queueBindRequest)
  60. }
  61. func CreateQueue(queueRequest types.QueueRequest) {
  62. // 2. 将数据转换为JSON
  63. jsonData, err := json.Marshal(queueRequest)
  64. if err != nil {
  65. fmt.Printf("JSON编码失败: %v\n", err)
  66. return
  67. }
  68. url := "http://localhost:9090/api/rabbitmq/queue/create"
  69. POST(jsonData, url)
  70. }
  71. func CreateQueueBind(queueBindRequest types.QueueBindRequest) {
  72. // 2. 将数据转换为JSON
  73. jsonData, err := json.Marshal(queueBindRequest)
  74. if err != nil {
  75. fmt.Printf("JSON编码失败: %v\n", err)
  76. return
  77. }
  78. url := "http://localhost:9090/api/rabbitmq/queue/bind"
  79. POST(jsonData, url)
  80. }
  81. func POST(jsonData []byte, url string) {
  82. // 如果服务端需要认证,添加JWT token
  83. token := "123" // 替换为实际的JWT token
  84. req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
  85. if err != nil {
  86. fmt.Printf("创建请求失败: %v\n", err)
  87. return
  88. }
  89. // 添加请求头
  90. req.Header.Set("Content-Type", "application/json")
  91. if token != "" {
  92. req.Header.Set("Authorization", "Bearer "+token)
  93. }
  94. // 4. 发送请求并获取响应
  95. client := &http.Client{
  96. Timeout: 10 * time.Second,
  97. }
  98. resp, err := client.Do(req)
  99. if err != nil {
  100. fmt.Printf("请求失败: %v\n", err)
  101. return
  102. }
  103. defer resp.Body.Close()
  104. // 5. 读取响应
  105. body, err := io.ReadAll(resp.Body)
  106. if err != nil {
  107. fmt.Printf("读取响应失败: %v\n", err)
  108. return
  109. }
  110. // 6. 输出结果
  111. fmt.Printf("状态码: %d\n", resp.StatusCode)
  112. fmt.Printf("响应头: %v\n", resp.Header)
  113. fmt.Printf("响应体: %s\n", string(body))
  114. }