| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- package main
-
- import (
- "bytes"
- "encoding/json"
- "io"
- "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{}
-
- // 首先登录获取有效token
- loginURL := "http://localhost:8080/api/login/user"
- loginData := map[string]string{
- "user_id": "test-user-001",
- "password": "password123",
- }
- loginJSON, _ := json.Marshal(loginData)
-
- loginReq, err := http.NewRequest("POST", loginURL, bytes.NewReader(loginJSON))
- if err != nil {
- t.Fatalf("创建登录请求失败: %v", err)
- }
- loginReq.Header.Set("Content-Type", "application/json")
-
- loginResp, err := httpClient.Do(loginReq)
- if err != nil {
- t.Fatalf("发送登录请求失败: %v", err)
- }
- defer loginResp.Body.Close()
-
- loginBody, err := io.ReadAll(loginResp.Body)
- if err != nil {
- t.Fatalf("读取登录响应失败: %v", err)
- }
-
- var loginResult map[string]interface{}
- if err := json.Unmarshal(loginBody, &loginResult); err != nil {
- t.Fatalf("解析登录响应失败: %v", err)
- }
-
- if !loginResult["success"].(bool) {
- t.Fatalf("登录失败: %v", loginResult)
- }
-
- authToken := loginResult["data"].(string)
- t.Logf("获取到认证token: %s...", authToken[:50])
-
- // 使用获取的token调用API
- 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)
- if err != nil {
- t.Fatalf("JSON序列化失败: %v", err)
- }
-
- req, err := http.NewRequest("POST", url, bytes.NewReader(jsonData))
- if err != nil {
- t.Fatalf("创建请求失败: %v", err)
- }
-
- req.Header.Set("Authorization", "Bearer "+authToken)
- req.Header.Set("Content-Type", "application/json")
-
- resp, err := httpClient.Do(req)
- if err != nil {
- t.Fatalf("发送请求失败: %v", err)
- }
- defer resp.Body.Close()
-
- // 读取响应
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- t.Fatalf("读取响应失败: %v", err)
- }
-
- var result map[string]interface{}
- if err := json.Unmarshal(body, &result); err != nil {
- t.Fatalf("解析响应失败: %v", err)
- }
-
- t.Logf("状态码: %d", resp.StatusCode)
- t.Logf("响应体: %s", string(body))
-
- if resp.StatusCode != 200 {
- t.Errorf("期望状态码200,实际: %d", resp.StatusCode)
- }
-
- if !result["success"].(bool) {
- t.Errorf("创建SVC启动配置失败: %v", result)
- }
- }
-
- 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)
- if err != nil {
- t.Fatalf("JSON序列化失败: %v", err)
- }
-
- req, err := http.NewRequest("POST", url, bytes.NewReader(jsonData))
- if err != nil {
- t.Fatalf("创建请求失败: %v", err)
- }
- // Basic Auth 认证
- req.SetBasicAuth("admin", "123")
-
- req.Header.Set("Content-Type", "application/json")
-
- resp, err := httpClient.Do(req)
- if err != nil {
- t.Fatalf("发送请求失败: %v", err)
- }
- defer resp.Body.Close()
-
- // 读取响应
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- t.Fatalf("读取响应失败: %v", err)
- }
-
- var result map[string]interface{}
- if err := json.Unmarshal(body, &result); err != nil {
- t.Fatalf("解析响应失败: %v", err)
- }
-
- t.Logf("状态码: %d", resp.StatusCode)
- t.Logf("响应体: %s", string(body))
-
- if resp.StatusCode != 200 {
- t.Errorf("期望状态码200,实际: %d", resp.StatusCode)
- }
-
- if !result["success"].(bool) {
- t.Errorf("删除SVC启动配置失败: %v", result)
- }
- }
|