From c1c0a8fb30e02579e7a0eaf04ad444a1232ecb33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hunyadv=C3=A1ri=20P=C3=A9ter?= Date: Thu, 2 May 2019 17:14:41 +0200 Subject: [PATCH 1/3] send requestBody to mock function if it can handle it --- client/mock/mock.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/client/mock/mock.go b/client/mock/mock.go index e9022c71..27317391 100644 --- a/client/mock/mock.go +++ b/client/mock/mock.go @@ -83,7 +83,11 @@ func (m *MockClient) Call(ctx context.Context, req client.Request, rsp interface } response := r.Response if t := reflect.TypeOf(r.Response); t.Kind() == reflect.Func { - response = reflect.ValueOf(r.Response).Call([]reflect.Value{})[0].Interface() + var reqBody []reflect.Value + if t.NumIn() == 0 { + reqBody = append(reqBody, reflect.ValueOf(req.Body())) + } + response = reflect.ValueOf(r.Response).Call(reqBody)[0].Interface() } v.Set(reflect.ValueOf(response)) From 08d70c9d0a8b8e47e3ba757567737a4dc6356bb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hunyadv=C3=A1ri=20P=C3=A9ter?= Date: Thu, 2 May 2019 18:02:24 +0200 Subject: [PATCH 2/3] fix and add tests --- client/mock/mock.go | 2 +- client/mock/mock_test.go | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/client/mock/mock.go b/client/mock/mock.go index 27317391..f10b3f9a 100644 --- a/client/mock/mock.go +++ b/client/mock/mock.go @@ -84,7 +84,7 @@ func (m *MockClient) Call(ctx context.Context, req client.Request, rsp interface response := r.Response if t := reflect.TypeOf(r.Response); t.Kind() == reflect.Func { var reqBody []reflect.Value - if t.NumIn() == 0 { + if t.NumIn() == 1 { reqBody = append(reqBody, reflect.ValueOf(req.Body())) } response = reflect.ValueOf(r.Response).Call(reqBody)[0].Interface() diff --git a/client/mock/mock_test.go b/client/mock/mock_test.go index 5dc5de1b..4f00f8a9 100644 --- a/client/mock/mock_test.go +++ b/client/mock/mock_test.go @@ -18,12 +18,18 @@ func TestClient(t *testing.T) { {Endpoint: "Foo.Fail", Error: errors.InternalServerError("go.mock", "failed")}, {Endpoint: "Foo.Func", Response: func() string { return "string" }}, {Endpoint: "Foo.FuncStruct", Response: func() *TestResponse { return &TestResponse{Param: "aparam"} }}, + {Endpoint: "Foo.FuncWithReqBody", Response: func(req interface{}) string { + if req.(map[string]string)["foo"] == "bar" { + return "string" + } + return "wrong" + }}, } c := NewClient(Response("go.mock", response)) for _, r := range response { - req := c.NewRequest("go.mock", r.Endpoint, map[string]interface{}{"foo": "bar"}) + req := c.NewRequest("go.mock", r.Endpoint, map[string]string{"foo": "bar"}) var rsp interface{} err := c.Call(context.TODO(), req, &rsp) @@ -33,6 +39,20 @@ func TestClient(t *testing.T) { } t.Log(rsp) + if r.Endpoint == "Foo.FuncWithReqBody" { + req := c.NewRequest("go.mock", r.Endpoint, map[string]string{"foo": "wrong"}) + var rsp interface{} + + err := c.Call(context.TODO(), req, &rsp) + + if err != r.Error { + t.Fatalf("Expecter error %v got %v", r.Error, err) + } + if rsp.(string) != "wrong" { + t.Fatalf("Expecter response 'wrong' got %v", rsp) + } + t.Log(rsp) + } } } From aaf4a5c51a77e8d35bcc59b4c26094be80b98b4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hunyadv=C3=A1ri=20P=C3=A9ter?= Date: Thu, 2 May 2019 18:06:43 +0200 Subject: [PATCH 3/3] rename reqBody variable to request --- client/mock/mock.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/client/mock/mock.go b/client/mock/mock.go index f10b3f9a..6ee8f1f8 100644 --- a/client/mock/mock.go +++ b/client/mock/mock.go @@ -83,11 +83,11 @@ func (m *MockClient) Call(ctx context.Context, req client.Request, rsp interface } response := r.Response if t := reflect.TypeOf(r.Response); t.Kind() == reflect.Func { - var reqBody []reflect.Value + var request []reflect.Value if t.NumIn() == 1 { - reqBody = append(reqBody, reflect.ValueOf(req.Body())) + request = append(request, reflect.ValueOf(req.Body())) } - response = reflect.ValueOf(r.Response).Call(reqBody)[0].Interface() + response = reflect.ValueOf(r.Response).Call(request)[0].Interface() } v.Set(reflect.ValueOf(response))