api/handler/rpc: process all methods and merge url params to json body (#1427)

* api/handler/rpc: process all methods and merge url params to json body

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>

* add merge json test

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-03-27 10:59:31 +03:00
parent bf74b4394e
commit da31e36f93
2 changed files with 38 additions and 1 deletions

22
rpc.go
View File

@ -8,6 +8,7 @@ import (
"strconv" "strconv"
"strings" "strings"
jsonpatch "github.com/evanphx/json-patch/v5"
"github.com/joncalhoun/qson" "github.com/joncalhoun/qson"
"github.com/micro/go-micro/v2/api" "github.com/micro/go-micro/v2/api"
"github.com/micro/go-micro/v2/api/handler" "github.com/micro/go-micro/v2/api/handler"
@ -273,13 +274,32 @@ func requestPayload(r *http.Request) ([]byte, error) {
if len(r.URL.RawQuery) > 0 { if len(r.URL.RawQuery) > 0 {
return qson.ToJSON(r.URL.RawQuery) return qson.ToJSON(r.URL.RawQuery)
} }
case "PATCH", "POST": case "PATCH", "POST", "PUT", "DELETE":
urlParams := []byte("{}")
bodyParams := []byte("{}")
var err error
if len(r.URL.RawQuery) > 0 {
if urlParams, err = qson.ToJSON(r.URL.RawQuery); err != nil {
return nil, err
}
}
buf := bufferPool.Get() buf := bufferPool.Get()
defer bufferPool.Put(buf) defer bufferPool.Put(buf)
if _, err := buf.ReadFrom(r.Body); err != nil { if _, err := buf.ReadFrom(r.Body); err != nil {
return nil, err return nil, err
} }
if b := buf.Bytes(); len(b) > 0 {
bodyParams = b
}
if out, err := jsonpatch.MergeMergePatches(urlParams, bodyParams); err == nil {
return out, nil
}
//fallback to previous unknown behaviour
return buf.Bytes(), nil return buf.Bytes(), nil
} }
return []byte{}, nil return []byte{}, nil

View File

@ -27,6 +27,23 @@ func TestRequestPayloadFromRequest(t *testing.T) {
t.Fatal("Failed to marshal proto to JSON ", err) t.Fatal("Failed to marshal proto to JSON ", err)
} }
jsonUrlBytes := []byte(`{"key1":"val1","key2":"val2","name":"Test"}`)
t.Run("extracting a json from a POST request with url params", func(t *testing.T) {
r, err := http.NewRequest("POST", "http://localhost/my/path?key1=val1&key2=val2", bytes.NewReader(jsonBytes))
if err != nil {
t.Fatalf("Failed to created http.Request: %v", err)
}
extByte, err := requestPayload(r)
if err != nil {
t.Fatalf("Failed to extract payload from request: %v", err)
}
if string(extByte) != string(jsonUrlBytes) {
t.Fatalf("Expected %v and %v to match", string(extByte), jsonUrlBytes)
}
})
t.Run("extracting a proto from a POST request", func(t *testing.T) { t.Run("extracting a proto from a POST request", func(t *testing.T) {
r, err := http.NewRequest("POST", "http://localhost/my/path", bytes.NewReader(protoBytes)) r, err := http.NewRequest("POST", "http://localhost/my/path", bytes.NewReader(protoBytes))
if err != nil { if err != nil {