Skip to content

Commit

Permalink
Revert "fix PatchInstanceMethod"
Browse files Browse the repository at this point in the history
This reverts commit 2f94d77.
  • Loading branch information
samanhappy committed Aug 12, 2024
1 parent 2f94d77 commit 390a1bf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
5 changes: 3 additions & 2 deletions monkey/monkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package monkey

import (
"fmt"
"reflect"

"github.com/agiledragon/gomonkey/v2"
)
Expand Down Expand Up @@ -50,7 +51,7 @@ func Unpatch(target interface{}) bool {
}

// PatchInstanceMethod replaces an instance method methodName for the type target with replacement
func PatchInstanceMethod(target interface{}, methodName string, replacement interface{}) *gomonkey.Patches {
func PatchInstanceMethod(target reflect.Type, methodName string, replacement interface{}) *gomonkey.Patches {
key := fmt.Sprintf("%v:%v", target, methodName)
existingPatches, ok := patchesMap[key]
if ok {
Expand All @@ -62,7 +63,7 @@ func PatchInstanceMethod(target interface{}, methodName string, replacement inte
}

// UnpatchInstanceMethod unpatch a patch
func UnpatchInstanceMethod(target interface{}, methodName string) bool {
func UnpatchInstanceMethod(target reflect.Type, methodName string) bool {
key := fmt.Sprintf("%v:%v", target, methodName)
patches, ok := patchesMap[key]
if !ok {
Expand Down
11 changes: 6 additions & 5 deletions notify/lark/lark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"io"
"net/http"
"reflect"
"strings"
"testing"

Expand All @@ -43,7 +44,7 @@ func TestLark(t *testing.T) {
assert.Equal(t, "lark", conf.Kind())

var client *http.Client
monkey.PatchInstanceMethod(client, "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
monkey.PatchInstanceMethod(reflect.TypeOf(client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
r := io.NopCloser(strings.NewReader(`{"StatusCode": 0}`))
return &http.Response{
StatusCode: 200,
Expand All @@ -54,7 +55,7 @@ func TestLark(t *testing.T) {
assert.NoError(t, err)

// bad response
monkey.PatchInstanceMethod(client, "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
monkey.PatchInstanceMethod(reflect.TypeOf(client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
r := io.NopCloser(strings.NewReader(`{"StatusCode": "100"}`))
return &http.Response{
StatusCode: 200,
Expand All @@ -66,7 +67,7 @@ func TestLark(t *testing.T) {
assert.Contains(t, err.Error(), "Error response from Lark")
assert.Contains(t, err.Error(), "code [0] - msg []")

monkey.PatchInstanceMethod(client, "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
monkey.PatchInstanceMethod(reflect.TypeOf(client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
r := io.NopCloser(strings.NewReader(`{"StatusCode": "100", "code": 10, "msg": "lark error"}`))
return &http.Response{
StatusCode: 200,
Expand All @@ -78,7 +79,7 @@ func TestLark(t *testing.T) {
assert.Contains(t, err.Error(), "Error response from Lark")
assert.Contains(t, err.Error(), "code [10] - msg [lark error]")

monkey.PatchInstanceMethod(client, "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
monkey.PatchInstanceMethod(reflect.TypeOf(client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
r := io.NopCloser(strings.NewReader(`bad : json format`))
return &http.Response{
StatusCode: 200,
Expand All @@ -94,7 +95,7 @@ func TestLark(t *testing.T) {
err = conf.SendLark("title", "message")
assertError(t, err, "read error")

monkey.PatchInstanceMethod(client, "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
monkey.PatchInstanceMethod(reflect.TypeOf(client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
return nil, errors.New("http error")
})
err = conf.SendLark("title", "message")
Expand Down

0 comments on commit 390a1bf

Please sign in to comment.