82466d587d
iotlcm-upstream-proxy:渠道缓存与转发网关。 - 内联原私有依赖:yunze-public → internal/pub/(18 文件), yunze-pb/pb → internal/pb/(9 文件);Consul 注册改用官方 gogf/gf/contrib/registry/consul/v2(API 兼容) - Go module:yz-svr → git.5iot.com/iotlcm-public/iotlcm-upstream-proxy - 凭据与内部标识脱敏:MySQL/Redis/Consul 凭据、私网与生产 IP、内网域名 - 移除内部资产:CI 触发器 - Dockerfile 改用公共基础镜像,移除写死的 registry 凭据 - 补 Apache-2.0 LICENSE,重写 README,LF 行尾 + .gitattributes
78 lines
3.5 KiB
Go
78 lines
3.5 KiB
Go
package upstreamimpl_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
_ "git.5iot.com/iotlcm-public/iotlcm-upstream-proxy/internal/logic"
|
|
"git.5iot.com/iotlcm-public/iotlcm-upstream-proxy/internal/model"
|
|
"git.5iot.com/iotlcm-public/iotlcm-upstream-proxy/internal/model/entity"
|
|
"git.5iot.com/iotlcm-public/iotlcm-upstream-proxy/internal/service"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
"github.com/gogf/gf/v2/os/gcfg"
|
|
)
|
|
|
|
func TestMingqianProxyTemplate(t *testing.T) {
|
|
template, ok := service.UpstreamAPI().GetTemp("sim_mingqian_v3")
|
|
if !ok || template == nil {
|
|
t.Fatal("mingqian proxy template missing")
|
|
}
|
|
instance := template.NewTemp(context.Background(), &model.SimChannelConfigItemCache{Template: &entity.TSimChannelTemplate{TempKey: "sim_mingqian_v3"}, Channel: &entity.TSimChannels{ChanKey: "mingqian-test"}})
|
|
if instance == nil {
|
|
t.Fatal("mingqian proxy instance missing")
|
|
}
|
|
}
|
|
|
|
func TestMingqianProxyPreservesFlowFields(t *testing.T) {
|
|
const response = `{"code":0,"data":[{"code":0,"message":"","simKey":{"chanKey":"mingqian-test","iccid":"A"},"resData":{"dataUsage":1,"packageFlow":{"totalFlow":10240,"dataUsage":8192,"remainingFlow":2048}}},{"code":0,"message":"package flow unavailable","simKey":{"chanKey":"mingqian-test","iccid":"B"},"resData":{"dataUsage":2,"packageFlowError":"mingqian: business code 500"}},{"code":1,"message":"monthly usage unavailable","simKey":{"chanKey":"mingqian-test","iccid":"C"},"resData":{"dataUsage":0,"packageFlow":{"totalFlow":10,"dataUsage":8,"remainingFlow":2}}}]}`
|
|
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
|
if request.Method != http.MethodPost || request.URL.Path != "/sim/query/sim-datausage" {
|
|
t.Errorf("wrong gateway route: %s %s", request.Method, request.URL.Path)
|
|
}
|
|
var input model.SimBaseReqJson
|
|
if err := request.ParseForm(); err != nil {
|
|
t.Error(err)
|
|
}
|
|
if err := json.Unmarshal([]byte(request.Form.Get("simKeys")), &input.SimKeys); err != nil || len(input.SimKeys) != 3 || input.SimKeys[0].ICCID != "A" {
|
|
t.Errorf("request changed: %+v %v", input, err)
|
|
}
|
|
writer.Header().Set("Content-Type", "application/json")
|
|
fmt.Fprint(writer, response)
|
|
}))
|
|
t.Cleanup(server.Close)
|
|
config, err := gcfg.NewAdapterContent(fmt.Sprintf(`{"gateway":{"address":%q,"timeout":"1s"}}`, strings.TrimPrefix(server.URL, "http://")))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
original := g.Cfg().GetAdapter()
|
|
g.Cfg().SetAdapter(config)
|
|
t.Cleanup(func() { g.Cfg().SetAdapter(original) })
|
|
template, ok := service.UpstreamAPI().GetTemp("sim_mingqian_v3")
|
|
if !ok {
|
|
t.Fatal("missing Mingqian registration")
|
|
}
|
|
instance := template.NewTemp(context.Background(), &model.SimChannelConfigItemCache{Template: &entity.TSimChannelTemplate{TempKey: "sim_mingqian_v3"}, Channel: &entity.TSimChannels{ChanKey: "mingqian-test"}})
|
|
request := &ghttp.Request{Request: httptest.NewRequest(http.MethodPost, "/sim/query/sim-datausage", nil)}
|
|
output, err := instance.SimBase(request.Context(), &model.SimBaseReqJson{
|
|
SimKeys: []*model.SimKey{{ChanKey: "mingqian-test", ICCID: "A"}, {ChanKey: "mingqian-test", ICCID: "B"}, {ChanKey: "mingqian-test", ICCID: "C"}},
|
|
ReqData: json.RawMessage(`{}`),
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var expected model.SimResponseData[model.SimBaseResJson]
|
|
if err := json.Unmarshal([]byte(response), &expected); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(output, expected.Data) {
|
|
t.Fatalf("proxy dropped package fields or changed failure status: %+v", output)
|
|
}
|
|
}
|