mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-11-20 07:53:34 +08:00
Merge 6aadf48f403e95425690469b49442f60c04bd253 into a101d85ec16cc21bec60005169725adc1ae5649a
This commit is contained in:
commit
ab18ec8186
@ -19,6 +19,7 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/callbackstruct"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||
@ -61,12 +62,12 @@ func NewWebhookClient(url string, options ...*memamq.MemoryQueue) *Client {
|
||||
}
|
||||
|
||||
func (c *Client) SyncPost(ctx context.Context, command string, req callbackstruct.CallbackReq, resp callbackstruct.CallbackResp, before *config.BeforeConfig) error {
|
||||
return c.post(ctx, command, req, resp, before.Timeout)
|
||||
return c.post(ctx, command, req, resp, before.Timeout, before.FailedContinue)
|
||||
}
|
||||
|
||||
func (c *Client) AsyncPost(ctx context.Context, command string, req callbackstruct.CallbackReq, resp callbackstruct.CallbackResp, after *config.AfterConfig) {
|
||||
if after.Enable {
|
||||
c.queue.Push(func() { c.post(ctx, command, req, resp, after.Timeout) })
|
||||
c.queue.Push(func() { c.post(ctx, command, req, resp, after.Timeout, false) })
|
||||
}
|
||||
}
|
||||
|
||||
@ -76,16 +77,28 @@ func (c *Client) AsyncPostWithQuery(ctx context.Context, command string, req cal
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) post(ctx context.Context, command string, input interface{}, output callbackstruct.CallbackResp, timeout int) error {
|
||||
func (c *Client) post(ctx context.Context, command string, input interface{}, output callbackstruct.CallbackResp, timeout int, failedContinue bool) error {
|
||||
ctx = mcontext.WithMustInfoCtx([]string{mcontext.GetOperationID(ctx), mcontext.GetOpUserID(ctx), mcontext.GetOpUserPlatform(ctx), mcontext.GetConnID(ctx)})
|
||||
fullURL := c.url + "/" + command
|
||||
log.ZInfo(ctx, "webhook", "url", fullURL, "input", input, "config", timeout)
|
||||
operationID, _ := ctx.Value(constant.OperationID).(string)
|
||||
b, err := c.client.Post(ctx, fullURL, map[string]string{constant.OperationID: operationID}, input, timeout)
|
||||
if err != nil {
|
||||
if failedContinue {
|
||||
log.ZWarn(ctx, "webhook failed but continue", err, "url", fullURL)
|
||||
return nil
|
||||
}
|
||||
return servererrs.ErrNetwork.WrapMsg(err.Error(), "post url", fullURL)
|
||||
}
|
||||
if err = json.Unmarshal(b, output); err != nil {
|
||||
if failedContinue {
|
||||
// Callers apply the response after a nil error, so drop any fields decoded before the failure.
|
||||
if v := reflect.ValueOf(output); v.Kind() == reflect.Pointer && !v.IsNil() {
|
||||
v.Elem().SetZero()
|
||||
}
|
||||
log.ZWarn(ctx, "webhook response format error but continue", err, "url", fullURL, "response", string(b))
|
||||
return nil
|
||||
}
|
||||
return servererrs.ErrData.WithDetail(err.Error() + " response format error")
|
||||
}
|
||||
if err := output.Parse(); err != nil {
|
||||
|
||||
@ -13,3 +13,64 @@
|
||||
// limitations under the License.
|
||||
|
||||
package webhook
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/callbackstruct"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/servererrs"
|
||||
"github.com/openimsdk/tools/errs"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestClient_SyncPost_FailedContinue(t *testing.T) {
|
||||
respond := func(body string) string {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
_, _ = w.Write([]byte(body))
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
return srv.URL
|
||||
}
|
||||
dropped := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
if conn, _, err := w.(http.Hijacker).Hijack(); err == nil {
|
||||
_ = conn.Close()
|
||||
}
|
||||
}))
|
||||
t.Cleanup(dropped.Close)
|
||||
bob := "bob"
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
url string
|
||||
failedContinue bool
|
||||
wantErr errs.CodeError
|
||||
wantNickname *string
|
||||
}{
|
||||
{"connection dropped", dropped.URL, false, servererrs.ErrNetwork, nil},
|
||||
{"connection dropped continue", dropped.URL, true, nil, nil},
|
||||
{"malformed response", respond("<html>bad gateway</html>"), false, servererrs.ErrData, nil},
|
||||
{"malformed response continue", respond("<html>bad gateway</html>"), true, nil, nil},
|
||||
{"partially decoded response continue", respond(`{"nickName":"bob","actionCode":"x"}`), true, nil, nil},
|
||||
{"accepted continue", respond(`{"actionCode":0,"nextCode":0,"nickName":"bob"}`), true, nil, &bob},
|
||||
{"rejected continue", respond(`{"actionCode":0,"nextCode":1,"errCode":5001,"errMsg":"denied"}`), true, errs.NewCodeError(5001, "denied"), nil},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
resp := &callbackstruct.CallbackBeforeUpdateUserInfoResp{}
|
||||
before := &config.BeforeConfig{Enable: true, Timeout: 5, FailedContinue: tt.failedContinue}
|
||||
err := NewWebhookClient(tt.url).SyncPost(context.Background(), "callbackBeforeUpdateUserInfoCommand", &callbackstruct.CallbackBeforeUpdateUserInfoReq{}, resp, before)
|
||||
if tt.wantErr == nil {
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tt.wantNickname, resp.Nickname)
|
||||
return
|
||||
}
|
||||
require.Error(t, err)
|
||||
assert.True(t, tt.wantErr.Is(err), "got %v", err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user