| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package main
-
- import (
- "bytes"
- "encoding/json"
- "fmt"
- "io"
- "log"
- "net/http"
- "os/exec"
- "testing"
-
- "git.x2erp.com/qdy/go-base/model/request/configreq"
- )
-
- func TestCreateConfigStartupSVC(t *testing.T) {
-
- // 清除测试缓存
- cmd := exec.Command("go", "clean", "-testcache")
- if err := cmd.Run(); err != nil {
- t.Logf("清除测试缓存失败: %v", err)
- // 继续执行测试
- }
- httpClient := &http.Client{}
- url := "http://localhost:8080/api/create/config/startup/svc-mcp"
-
- data := configreq.ConfigRequest{
- YamlRoot: "service",
- ConfigFields: map[string]*configreq.ConfigField{
- "secret_key": {
- YamlName: "secret_key",
- YamlValue: "123wad",
- },
- "write_timeout": {
- YamlName: "write_timeout",
- YamlValue: "90",
- },
- "read_timeout": {
- YamlName: "read_timeout",
- YamlValue: "80",
- },
- },
- }
- jsonData, err := json.Marshal(data)
-
- req, err := http.NewRequest("POST", url, bytes.NewReader(jsonData))
- if err != nil {
- log.Printf("NewRequest:%v", err)
- }
-
- // Basic Auth 认证
- //req.SetBasicAuth("username", "password")
-
- req.Header.Set("Authorization", "Bearer 123")
- req.Header.Set("Content-Type", "application/json")
-
- resp, err := httpClient.Do(req)
-
- if err != nil {
- fmt.Printf("发送请求失败: %v\n", err)
- return
- }
- defer resp.Body.Close()
-
- // 读取响应
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- fmt.Printf("读取响应失败: %v\n", err)
- return
- }
-
- fmt.Printf("状态码: %d\n", resp.StatusCode)
- fmt.Printf("响应体: %s\n", string(body))
-
- }
-
- func TestDelConfigStartupSVC(t *testing.T) {
-
- httpClient := &http.Client{}
- url := "http://localhost:8080/api/delete/config/startup/svc-mcp"
-
- data := configreq.ConfigRequest{
- YamlRoot: "service",
- ConfigFields: map[string]*configreq.ConfigField{
- "secret_key": {
- YamlName: "secret_key",
- },
- "write_timeout": {
- YamlName: "write_timeout",
- },
- },
- }
- jsonData, err := json.Marshal(data)
-
- req, err := http.NewRequest("POST", url, bytes.NewReader(jsonData))
- if err != nil {
- log.Printf("NewRequest:%v", err)
- }
- // Basic Auth 认证
- req.SetBasicAuth("admin", "123")
-
- //req.Header.Set("Authorization", "Bearer 123")
- req.Header.Set("Content-Type", "application/json")
-
- resp, err := httpClient.Do(req)
-
- if err != nil {
- fmt.Printf("发送请求失败: %v\n", err)
- return
- }
- defer resp.Body.Close()
-
- // 读取响应
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- fmt.Printf("读取响应失败: %v\n", err)
- return
- }
-
- fmt.Printf("状态码: %d\n", resp.StatusCode)
- fmt.Printf("响应体: %s\n", string(body))
-
- }
|