| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- package test
-
- import (
- "bytes"
- "encoding/json"
- "fmt"
- "io"
- "net/http"
- "testing"
- "time"
-
- "git.x2erp.com/qdy/go-base/types"
- )
-
- // type ExchangeRequest struct {
- // ChannelName string `json:"channel_name"`
- // ExchangeName string `json:"exchange_name"`
- // ExchangeType string `json:"exchange_type"`
- // Durable bool `json:"durable"`
- // Internal bool `json:"internal"`
- // AutoDelete bool `json:"auto_delete"`
- // NoWait bool `json:"no_wait"`
- // Arguments map[string]string `json:"arguments"`
- // }
-
- func TestCreateexchange(t *testing.T) {
- // 1. 准备请求数据
- requestData := types.ExchangeRequest{
- ChannelName: "v_bdx_channel",
- ExchangeName: "v_bdx_exchange",
- ExchangeType: "topic", // direct, fanout, topic, headers
- Durable: true,
- Internal: false,
- AutoDelete: false,
- NoWait: false,
- Arguments: map[string]string{
- "x-delayed-type": "topic",
- },
- }
-
- // 消息队列
- queueRequest := types.QueueRequest{
- QueueName: "vbdx_queue_count",
- ChannelName: requestData.ChannelName,
- }
-
- queueBindRequest := types.QueueBindRequest{
- //ChannelName: requestData.ChannelName,
- QueueName: queueRequest.QueueName,
- ExchangeName: requestData.ExchangeName,
- RoutingKey: "v-bdx.#",
- }
-
- // 2. 将数据转换为JSON
- jsonData, err := json.Marshal(requestData)
- if err != nil {
- fmt.Printf("JSON编码失败: %v\n", err)
- return
- }
-
- // 3. 发送HTTP POST请求
- url := "http://localhost:9090/api/rabbitmq/exchange/create"
-
- POST(jsonData, url)
-
- //建立队列
- CreateQueue(queueRequest)
-
- //绑定
- CreateQueueBind(queueBindRequest)
- }
-
- func CreateQueue(queueRequest types.QueueRequest) {
-
- // 2. 将数据转换为JSON
- jsonData, err := json.Marshal(queueRequest)
- if err != nil {
- fmt.Printf("JSON编码失败: %v\n", err)
- return
- }
- url := "http://localhost:9090/api/rabbitmq/queue/create"
-
- POST(jsonData, url)
-
- }
-
- func CreateQueueBind(queueBindRequest types.QueueBindRequest) {
-
- // 2. 将数据转换为JSON
- jsonData, err := json.Marshal(queueBindRequest)
- if err != nil {
- fmt.Printf("JSON编码失败: %v\n", err)
- return
- }
- url := "http://localhost:9090/api/rabbitmq/queue/bind"
-
- POST(jsonData, url)
-
- }
-
- func POST(jsonData []byte, url string) {
-
- // 如果服务端需要认证,添加JWT token
- token := "123" // 替换为实际的JWT token
-
- req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
- if err != nil {
- fmt.Printf("创建请求失败: %v\n", err)
- return
- }
-
- // 添加请求头
- req.Header.Set("Content-Type", "application/json")
- if token != "" {
- req.Header.Set("Authorization", "Bearer "+token)
- }
-
- // 4. 发送请求并获取响应
- client := &http.Client{
- Timeout: 10 * time.Second,
- }
-
- resp, err := client.Do(req)
- if err != nil {
- fmt.Printf("请求失败: %v\n", err)
- return
- }
- defer resp.Body.Close()
-
- // 5. 读取响应
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- fmt.Printf("读取响应失败: %v\n", err)
- return
- }
-
- // 6. 输出结果
- fmt.Printf("状态码: %d\n", resp.StatusCode)
- fmt.Printf("响应头: %v\n", resp.Header)
- fmt.Printf("响应体: %s\n", string(body))
- }
|