diff --git a/api/agent/protocol/json.go b/api/agent/protocol/json.go index b52a00e9e..53c656bf9 100644 --- a/api/agent/protocol/json.go +++ b/api/agent/protocol/json.go @@ -33,6 +33,8 @@ func writeString(err error, dst io.Writer, str string) error { return err } +// TODO(xxx): headers, query parameters, body - what else should we add to func's payload? +// TODO(xxx): get rid of request body buffering somehow func (h *JSONProtocol) DumpJSON(req *http.Request) error { stdin := json.NewEncoder(h.in) bb := new(bytes.Buffer) @@ -52,6 +54,12 @@ func (h *JSONProtocol) DumpJSON(req *http.Request) error { return err } err = stdin.Encode(req.Header) + err = writeString(err, h.in, ",") + err = writeString(err, h.in, `"query_parameters":`) + if err != nil { + return err + } + err = stdin.Encode(req.URL.RawQuery) err = writeString(err, h.in, "}") return err } diff --git a/api/agent/protocol/json_test.go b/api/agent/protocol/json_test.go index cbe21e000..bf389c9e7 100644 --- a/api/agent/protocol/json_test.go +++ b/api/agent/protocol/json_test.go @@ -15,11 +15,13 @@ type RequestData struct { A string `json:"a"` } -type fuckReed struct { - Body RequestData `json:"body"` +type funcRequestBody struct { + Body string `json:"body"` + Headers http.Header `json:"headers"` + QueryParameters string `json:"query_parameters"` } -func TestJSONProtocolDumpJSONRequestWithData(t *testing.T) { +func setupRequest(data interface{}) *http.Request { req := &http.Request{ Method: http.MethodPost, URL: &url.URL{ @@ -38,10 +40,17 @@ func TestJSONProtocolDumpJSONRequestWithData(t *testing.T) { Host: "localhost:8080", } var buf bytes.Buffer - rDataBefore := RequestData{A: "a"} - json.NewEncoder(&buf).Encode(rDataBefore) - req.Body = ioutil.NopCloser(&buf) + if data != nil { + _ = json.NewEncoder(&buf).Encode(data) + } + req.Body = ioutil.NopCloser(&buf) + return req +} + +func TestJSONProtocolDumpJSONRequestWithData(t *testing.T) { + rDataBefore := RequestData{A: "a"} + req := setupRequest(rDataBefore) r, w := io.Pipe() proto := JSONProtocol{w, r} go func() { @@ -51,7 +60,7 @@ func TestJSONProtocolDumpJSONRequestWithData(t *testing.T) { } w.Close() }() - incomingReq := new(jsonio) + incomingReq := new(funcRequestBody) bb := new(bytes.Buffer) _, err := bb.ReadFrom(r) @@ -74,25 +83,7 @@ func TestJSONProtocolDumpJSONRequestWithData(t *testing.T) { } func TestJSONProtocolDumpJSONRequestWithoutData(t *testing.T) { - req := &http.Request{ - Method: http.MethodPost, - URL: &url.URL{ - Scheme: "http", - Host: "localhost:8080", - Path: "/v1/apps", - RawQuery: "something=something&etc=etc", - }, - ProtoMajor: 1, - ProtoMinor: 1, - Header: http.Header{ - "Host": []string{"localhost:8080"}, - "User-Agent": []string{"curl/7.51.0"}, - "Content-Type": []string{"application/json"}, - }, - Host: "localhost:8080", - } - var buf bytes.Buffer - req.Body = ioutil.NopCloser(&buf) + req := setupRequest(nil) r, w := io.Pipe() proto := JSONProtocol{w, r} @@ -103,7 +94,7 @@ func TestJSONProtocolDumpJSONRequestWithoutData(t *testing.T) { } w.Close() }() - incomingReq := new(jsonio) + incomingReq := new(funcRequestBody) bb := new(bytes.Buffer) _, err := bb.ReadFrom(r) @@ -114,9 +105,41 @@ func TestJSONProtocolDumpJSONRequestWithoutData(t *testing.T) { if err != nil { t.Error(err.Error()) } + if incomingReq.Body != "" { + t.Errorf("Request body assertion mismatch: expected: %s, got %s", + "", incomingReq.Body) + } if ok := reflect.DeepEqual(req.Header, incomingReq.Headers); !ok { t.Errorf("Request headers assertion mismatch: expected: %s, got %s", req.Header, incomingReq.Headers) - + } +} + +func TestJSONProtocolDumpJSONRequestWithQuery(t *testing.T) { + req := setupRequest(nil) + + r, w := io.Pipe() + proto := JSONProtocol{w, r} + go func() { + err := proto.DumpJSON(req) + if err != nil { + t.Error(err.Error()) + } + w.Close() + }() + incomingReq := new(funcRequestBody) + bb := new(bytes.Buffer) + + _, err := bb.ReadFrom(r) + if err != nil { + t.Error(err.Error()) + } + err = json.Unmarshal(bb.Bytes(), incomingReq) + if err != nil { + t.Error(err.Error()) + } + if incomingReq.QueryParameters != req.URL.RawQuery { + t.Errorf("Request query string assertion mismatch: expected: %s, got %s", + req.URL.RawQuery, incomingReq.QueryParameters) } } diff --git a/examples/formats/json/go/func.go b/examples/formats/json/go/func.go index f61222d2f..1cdf2bba4 100644 --- a/examples/formats/json/go/func.go +++ b/examples/formats/json/go/func.go @@ -15,9 +15,10 @@ type Person struct { } type JSON struct { - Headers http.Header `json:"headers"` - Body string `json:"body,omitempty"` - StatusCode int `json:"status,omitempty"` + Headers http.Header `json:"headers"` + Body string `json:"body,omitempty"` + StatusCode int `json:"status,omitempty"` + QueryParameters string `json:"query_parameters,omitempty"` } func main() {