2016-04-06 18:33:57 +01:00
|
|
|
package mock
|
|
|
|
|
|
|
|
import (
|
2018-03-03 11:53:52 +00:00
|
|
|
"context"
|
2016-04-06 18:33:57 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestClient(t *testing.T) {
|
2016-04-06 18:38:33 +01:00
|
|
|
type TestResponse struct {
|
2016-04-06 18:37:46 +01:00
|
|
|
Param string
|
|
|
|
}
|
|
|
|
|
2016-04-06 18:33:57 +01:00
|
|
|
response := []MockResponse{
|
2019-01-10 21:25:31 +00:00
|
|
|
{Endpoint: "Foo.Bar", Response: map[string]interface{}{"foo": "bar"}},
|
|
|
|
{Endpoint: "Foo.Struct", Response: &TestResponse{Param: "aparam"}},
|
|
|
|
{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"} }},
|
2019-05-02 18:02:24 +02:00
|
|
|
{Endpoint: "Foo.FuncWithReqBody", Response: func(req interface{}) string {
|
|
|
|
if req.(map[string]string)["foo"] == "bar" {
|
|
|
|
return "string"
|
|
|
|
}
|
|
|
|
return "wrong"
|
|
|
|
}},
|
2016-04-06 18:33:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
c := NewClient(Response("go.mock", response))
|
|
|
|
|
|
|
|
for _, r := range response {
|
2019-05-02 18:02:24 +02:00
|
|
|
req := c.NewRequest("go.mock", r.Endpoint, map[string]string{"foo": "bar"})
|
2016-04-06 18:37:46 +01:00
|
|
|
var rsp interface{}
|
2016-04-06 18:33:57 +01:00
|
|
|
|
|
|
|
err := c.Call(context.TODO(), req, &rsp)
|
|
|
|
|
|
|
|
if err != r.Error {
|
|
|
|
t.Fatalf("Expecter error %v got %v", r.Error, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log(rsp)
|
2019-05-02 18:02:24 +02:00
|
|
|
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)
|
|
|
|
}
|
2016-04-06 18:33:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|