Compare commits

..

No commits in common. "v3" and "master" have entirely different histories.
v3 ... master

24
mock.go
View File

@ -61,7 +61,6 @@ type ExpectedRequest struct {
delay time.Duration
rsp interface{}
req client.Request
rspct string
}
// WillDelayFor allows to specify duration for which it will delay result. May
@ -78,9 +77,8 @@ func (e *ExpectedRequest) WillReturnError(err error) *ExpectedRequest {
}
// WillReturnResponse allows to set a response for expected client.Call
func (e *ExpectedRequest) WillReturnResponse(ct string, rsp interface{}) *ExpectedRequest {
func (e *ExpectedRequest) WillReturnResponse(rsp interface{}) *ExpectedRequest {
e.rsp = rsp
e.rspct = ct
return e
}
@ -135,6 +133,11 @@ func (c *MockClient) Call(ctx context.Context, req client.Request, rsp interface
ct = options.ContentType
}
cf, err := c.newCodec(ct)
if err != nil {
return errors.BadRequest("go.micro.client", err.Error())
}
for _, e := range c.expected {
er, ok := e.(*ExpectedRequest)
if !ok {
@ -163,10 +166,6 @@ func (c *MockClient) Call(ctx context.Context, req client.Request, rsp interface
src := er.req.Body()
switch reqbody := er.req.Body().(type) {
case []byte:
cf, err := c.newCodec(ct)
if err != nil {
return errors.BadRequest("go.micro.client", err.Error())
}
src, err = rutil.Zero(req.Body())
if err == nil {
err = cf.Unmarshal(reqbody, src)
@ -174,6 +173,10 @@ func (c *MockClient) Call(ctx context.Context, req client.Request, rsp interface
if err != nil {
return errors.BadRequest("go.micro.client", err.Error())
}
case client.Request:
break
default:
return errors.BadRequest("go.micro.client", "unknown request passed: %v", reqbody)
}
if !reflect.DeepEqual(req.Body(), src) {
@ -186,13 +189,6 @@ func (c *MockClient) Call(ctx context.Context, req client.Request, rsp interface
switch rspbody := er.rsp.(type) {
case []byte:
if er.rspct != "" {
ct = er.rspct
}
cf, err := c.newCodec(ct)
if err != nil {
return errors.BadRequest("go.micro.client", err.Error())
}
if err = cf.Unmarshal(rspbody, rsp); err != nil {
return errors.BadRequest("go.micro.client", err.Error())
}