Merge pull request 'allow to specify content-type for response' (#2) from improve into v3
Reviewed-on: #2
This commit is contained in:
commit
307c6f7d0e
24
mock.go
24
mock.go
@ -61,6 +61,7 @@ type ExpectedRequest struct {
|
|||||||
delay time.Duration
|
delay time.Duration
|
||||||
rsp interface{}
|
rsp interface{}
|
||||||
req client.Request
|
req client.Request
|
||||||
|
rspct string
|
||||||
}
|
}
|
||||||
|
|
||||||
// WillDelayFor allows to specify duration for which it will delay result. May
|
// WillDelayFor allows to specify duration for which it will delay result. May
|
||||||
@ -77,8 +78,9 @@ func (e *ExpectedRequest) WillReturnError(err error) *ExpectedRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WillReturnResponse allows to set a response for expected client.Call
|
// WillReturnResponse allows to set a response for expected client.Call
|
||||||
func (e *ExpectedRequest) WillReturnResponse(rsp interface{}) *ExpectedRequest {
|
func (e *ExpectedRequest) WillReturnResponse(ct string, rsp interface{}) *ExpectedRequest {
|
||||||
e.rsp = rsp
|
e.rsp = rsp
|
||||||
|
e.rspct = ct
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,11 +135,6 @@ func (c *MockClient) Call(ctx context.Context, req client.Request, rsp interface
|
|||||||
ct = options.ContentType
|
ct = options.ContentType
|
||||||
}
|
}
|
||||||
|
|
||||||
cf, err := c.newCodec(ct)
|
|
||||||
if err != nil {
|
|
||||||
return errors.BadRequest("go.micro.client", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, e := range c.expected {
|
for _, e := range c.expected {
|
||||||
er, ok := e.(*ExpectedRequest)
|
er, ok := e.(*ExpectedRequest)
|
||||||
if !ok {
|
if !ok {
|
||||||
@ -166,6 +163,10 @@ func (c *MockClient) Call(ctx context.Context, req client.Request, rsp interface
|
|||||||
src := er.req.Body()
|
src := er.req.Body()
|
||||||
switch reqbody := er.req.Body().(type) {
|
switch reqbody := er.req.Body().(type) {
|
||||||
case []byte:
|
case []byte:
|
||||||
|
cf, err := c.newCodec(ct)
|
||||||
|
if err != nil {
|
||||||
|
return errors.BadRequest("go.micro.client", err.Error())
|
||||||
|
}
|
||||||
src, err = rutil.Zero(req.Body())
|
src, err = rutil.Zero(req.Body())
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = cf.Unmarshal(reqbody, src)
|
err = cf.Unmarshal(reqbody, src)
|
||||||
@ -173,10 +174,6 @@ func (c *MockClient) Call(ctx context.Context, req client.Request, rsp interface
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.BadRequest("go.micro.client", err.Error())
|
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) {
|
if !reflect.DeepEqual(req.Body(), src) {
|
||||||
@ -189,6 +186,13 @@ func (c *MockClient) Call(ctx context.Context, req client.Request, rsp interface
|
|||||||
|
|
||||||
switch rspbody := er.rsp.(type) {
|
switch rspbody := er.rsp.(type) {
|
||||||
case []byte:
|
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 {
|
if err = cf.Unmarshal(rspbody, rsp); err != nil {
|
||||||
return errors.BadRequest("go.micro.client", err.Error())
|
return errors.BadRequest("go.micro.client", err.Error())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user