直连天下,v2插件
This commit is contained in:
parent
c6d95fc808
commit
622d1e6513
|
@ -10,6 +10,7 @@ import (
|
||||||
"gitee.com/chengdu_blue_brothers/openapi-go-sdk-v2/notify"
|
"gitee.com/chengdu_blue_brothers/openapi-go-sdk-v2/notify"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 插件通信信息,若不对应则会报错panic
|
// 插件通信信息,若不对应则会报错panic
|
||||||
|
@ -65,37 +66,19 @@ func (p *ZLTXV2Service) Query(ctx context.Context, request *proto.QueryRequest)
|
||||||
|
|
||||||
func (p *ZLTXV2Service) Notify(_ context.Context, request *proto.NotifyRequest) (*proto.NotifyResponse, error) {
|
func (p *ZLTXV2Service) Notify(_ context.Context, request *proto.NotifyRequest) (*proto.NotifyResponse, error) {
|
||||||
req := &http.Request{
|
req := &http.Request{
|
||||||
Method: "",
|
Header: nil,
|
||||||
URL: nil,
|
Body: nil,
|
||||||
Proto: "",
|
|
||||||
ProtoMajor: 0,
|
|
||||||
ProtoMinor: 0,
|
|
||||||
Header: nil,
|
|
||||||
Body: nil,
|
|
||||||
GetBody: nil,
|
|
||||||
ContentLength: 0,
|
|
||||||
TransferEncoding: nil,
|
|
||||||
Close: false,
|
|
||||||
Host: "",
|
|
||||||
Form: nil,
|
|
||||||
PostForm: nil,
|
|
||||||
MultipartForm: nil,
|
|
||||||
Trailer: nil,
|
|
||||||
RemoteAddr: "",
|
|
||||||
RequestURI: "",
|
|
||||||
TLS: nil,
|
|
||||||
Cancel: nil,
|
|
||||||
Response: nil,
|
|
||||||
}
|
}
|
||||||
headers := make(map[string]string)
|
httpHeaders := make(http.Header)
|
||||||
if err := json.Unmarshal(request.Headers, &headers); err != nil {
|
if err := json.Unmarshal(request.Headers, &httpHeaders); err != nil {
|
||||||
return nil, fmt.Errorf("headers Unmarshal err [%v]", err)
|
return nil, fmt.Errorf("headers Unmarshal err [%v]", err)
|
||||||
}
|
}
|
||||||
httpHeaders := make(http.Header, len(headers))
|
newHeaders := make(http.Header)
|
||||||
for k, value := range headers {
|
for key, values := range httpHeaders {
|
||||||
httpHeaders.Add(k, value)
|
newKey := strings.Title(strings.ToLower(key))
|
||||||
|
newHeaders[newKey] = values
|
||||||
}
|
}
|
||||||
req.Header = httpHeaders
|
req.Header = newHeaders
|
||||||
req.Body = ioutil.NopCloser(bytes.NewBuffer(request.Body))
|
req.Body = ioutil.NopCloser(bytes.NewBuffer(request.Body))
|
||||||
|
|
||||||
c, err := transConfig(request.Config)
|
c, err := transConfig(request.Config)
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"gitea.cdlsxd.cn/sdk/plugin/proto"
|
"gitea.cdlsxd.cn/sdk/plugin/proto"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"google.golang.org/grpc/metadata"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -78,7 +79,7 @@ func TestQuery(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNotify(t *testing.T) {
|
func TestNotify(t *testing.T) {
|
||||||
jsonData := `{
|
jsonData := []byte(`{
|
||||||
"tradeStatus": "SUCCESS",
|
"tradeStatus": "SUCCESS",
|
||||||
"orderNo": "C2081410801687044096",
|
"orderNo": "C2081410801687044096",
|
||||||
"tradeStateDesc": "成功",
|
"tradeStateDesc": "成功",
|
||||||
|
@ -89,16 +90,26 @@ func TestNotify(t *testing.T) {
|
||||||
"outTradeNo": "20240802212326527830",
|
"outTradeNo": "20240802212326527830",
|
||||||
"rechargeAccount": "17384082748",
|
"rechargeAccount": "17384082748",
|
||||||
"unitPrice":102.5
|
"unitPrice":102.5
|
||||||
}`
|
}`)
|
||||||
headers := `{"Authorization":"292e21f3683219369cf68dede0d45730","Content-Type":"application/json"}`
|
md := metadata.New(map[string]string{
|
||||||
|
"Authorization": "292e21f3683219369cf68dede0d45730",
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
})
|
||||||
|
ctx := metadata.NewIncomingContext(context.Background(), md)
|
||||||
|
headerData, ok := metadata.FromIncomingContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
t.Error("无法获取 Header 数据")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
HeaderBytes, _ := json.Marshal(headerData)
|
||||||
in := &proto.NotifyRequest{
|
in := &proto.NotifyRequest{
|
||||||
Config: config(),
|
Config: config(),
|
||||||
Queries: nil,
|
Queries: nil,
|
||||||
Headers: []byte(headers),
|
Headers: HeaderBytes,
|
||||||
Body: []byte(jsonData),
|
Body: jsonData,
|
||||||
}
|
}
|
||||||
t.Run("TestNotify", func(t *testing.T) {
|
t.Run("TestNotify", func(t *testing.T) {
|
||||||
got, err := zltx.Notify(context.Background(), in)
|
got, err := zltx.Notify(ctx, in)
|
||||||
if !assert.Nil(t, err) {
|
if !assert.Nil(t, err) {
|
||||||
t.Errorf("Notify() error = %v", err)
|
t.Errorf("Notify() error = %v", err)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue