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) } }