add aditional check for nil message passed

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-03-24 10:44:53 +03:00
parent 2bb1299e56
commit 1bbdf03f60
2 changed files with 15 additions and 7 deletions

12
http.go
View File

@ -41,6 +41,7 @@ func newRequest(addr string, req client.Request, ct string, cf codec.Codec, msg
body := "*" // as like google api http annotation body := "*" // as like google api http annotation
var tags []string var tags []string
var scheme string
u, err := url.Parse(addr) u, err := url.Parse(addr)
if err != nil { if err != nil {
hreq.URL = &url.URL{ hreq.URL = &url.URL{
@ -49,6 +50,7 @@ func newRequest(addr string, req client.Request, ct string, cf codec.Codec, msg
Path: req.Endpoint(), Path: req.Endpoint(),
} }
hreq.Host = addr hreq.Host = addr
scheme = "http"
} else { } else {
ep := req.Endpoint() ep := req.Endpoint()
if opts.Context != nil { if opts.Context != nil {
@ -86,23 +88,25 @@ func newRequest(addr string, req client.Request, ct string, cf codec.Codec, msg
return nil, errors.BadRequest("go.micro.client", err.Error()) return nil, errors.BadRequest("go.micro.client", err.Error())
} }
if scheme != "" {
hreq.URL, err = url.Parse(scheme + "://" + addr + path)
} else {
hreq.URL, err = url.Parse(addr + path) hreq.URL, err = url.Parse(addr + path)
}
if err != nil { if err != nil {
return nil, errors.BadRequest("go.micro.client", err.Error()) return nil, errors.BadRequest("go.micro.client", err.Error())
} }
var b []byte
// marshal request is struct not empty // marshal request is struct not empty
if nmsg != nil { if nmsg != nil {
var b []byte
b, err = cf.Marshal(nmsg) b, err = cf.Marshal(nmsg)
if err != nil { if err != nil {
return nil, errors.BadRequest("go.micro.client", err.Error()) return nil, errors.BadRequest("go.micro.client", err.Error())
} }
}
hreq.Body = ioutil.NopCloser(bytes.NewBuffer(b)) hreq.Body = ioutil.NopCloser(bytes.NewBuffer(b))
hreq.ContentLength = int64(len(b)) hreq.ContentLength = int64(len(b))
}
return hreq, nil return hreq, nil
} }

View File

@ -28,6 +28,10 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
return "", nil, err return "", nil, err
} }
if len(tpl.Fields) > 0 && msg == nil {
return "", nil, fmt.Errorf("nil message but path params requested: %v", path)
}
fieldsmap := make(map[string]string, len(tpl.Fields)) fieldsmap := make(map[string]string, len(tpl.Fields))
for _, v := range tpl.Fields { for _, v := range tpl.Fields {
fieldsmap[v] = "" fieldsmap[v] = ""
@ -95,7 +99,7 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta
// check not filled stuff // check not filled stuff
for k, v := range fieldsmap { for k, v := range fieldsmap {
if v == "" { if v == "" {
return "", nil, fmt.Errorf("path param %s not filled %s", k, v) return "", nil, fmt.Errorf("path param %s not filled", k)
} }
} }