JSON protocol updating (#426)

* JSON protocol updating

 this patch adds HTTP query string into payload (see more TODOs in code)
 adds one more test to verify query

* Fixing FMT
This commit is contained in:
Denis Makogon
2017-10-12 23:10:21 +03:00
committed by GitHub
parent 583a67bef9
commit ce25adfddb
3 changed files with 63 additions and 31 deletions

View File

@@ -33,6 +33,8 @@ func writeString(err error, dst io.Writer, str string) error {
return err 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 { func (h *JSONProtocol) DumpJSON(req *http.Request) error {
stdin := json.NewEncoder(h.in) stdin := json.NewEncoder(h.in)
bb := new(bytes.Buffer) bb := new(bytes.Buffer)
@@ -52,6 +54,12 @@ func (h *JSONProtocol) DumpJSON(req *http.Request) error {
return err return err
} }
err = stdin.Encode(req.Header) 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, "}") err = writeString(err, h.in, "}")
return err return err
} }

View File

@@ -15,11 +15,13 @@ type RequestData struct {
A string `json:"a"` A string `json:"a"`
} }
type fuckReed struct { type funcRequestBody struct {
Body RequestData `json:"body"` 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{ req := &http.Request{
Method: http.MethodPost, Method: http.MethodPost,
URL: &url.URL{ URL: &url.URL{
@@ -38,10 +40,17 @@ func TestJSONProtocolDumpJSONRequestWithData(t *testing.T) {
Host: "localhost:8080", Host: "localhost:8080",
} }
var buf bytes.Buffer 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() r, w := io.Pipe()
proto := JSONProtocol{w, r} proto := JSONProtocol{w, r}
go func() { go func() {
@@ -51,7 +60,7 @@ func TestJSONProtocolDumpJSONRequestWithData(t *testing.T) {
} }
w.Close() w.Close()
}() }()
incomingReq := new(jsonio) incomingReq := new(funcRequestBody)
bb := new(bytes.Buffer) bb := new(bytes.Buffer)
_, err := bb.ReadFrom(r) _, err := bb.ReadFrom(r)
@@ -74,25 +83,7 @@ func TestJSONProtocolDumpJSONRequestWithData(t *testing.T) {
} }
func TestJSONProtocolDumpJSONRequestWithoutData(t *testing.T) { func TestJSONProtocolDumpJSONRequestWithoutData(t *testing.T) {
req := &http.Request{ req := setupRequest(nil)
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)
r, w := io.Pipe() r, w := io.Pipe()
proto := JSONProtocol{w, r} proto := JSONProtocol{w, r}
@@ -103,7 +94,7 @@ func TestJSONProtocolDumpJSONRequestWithoutData(t *testing.T) {
} }
w.Close() w.Close()
}() }()
incomingReq := new(jsonio) incomingReq := new(funcRequestBody)
bb := new(bytes.Buffer) bb := new(bytes.Buffer)
_, err := bb.ReadFrom(r) _, err := bb.ReadFrom(r)
@@ -114,9 +105,41 @@ func TestJSONProtocolDumpJSONRequestWithoutData(t *testing.T) {
if err != nil { if err != nil {
t.Error(err.Error()) t.Error(err.Error())
} }
if incomingReq.Body != "" {
t.Errorf("Request body assertion mismatch: expected: %s, got %s",
"<empty-string>", incomingReq.Body)
}
if ok := reflect.DeepEqual(req.Header, incomingReq.Headers); !ok { if ok := reflect.DeepEqual(req.Header, incomingReq.Headers); !ok {
t.Errorf("Request headers assertion mismatch: expected: %s, got %s", t.Errorf("Request headers assertion mismatch: expected: %s, got %s",
req.Header, incomingReq.Headers) 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)
} }
} }

View File

@@ -15,9 +15,10 @@ type Person struct {
} }
type JSON struct { type JSON struct {
Headers http.Header `json:"headers"` Headers http.Header `json:"headers"`
Body string `json:"body,omitempty"` Body string `json:"body,omitempty"`
StatusCode int `json:"status,omitempty"` StatusCode int `json:"status,omitempty"`
QueryParameters string `json:"query_parameters,omitempty"`
} }
func main() { func main() {