mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Addressing more comments + tests
This commit is contained in:
@@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
// This is sent into the function
|
// This is sent into the function
|
||||||
// All HTTP request headers should be set in env
|
// All HTTP request headers should be set in env
|
||||||
type JSONIO struct {
|
type jsonio struct {
|
||||||
Headers http.Header `json:"headers,omitempty"`
|
Headers http.Header `json:"headers,omitempty"`
|
||||||
Body string `json:"body"`
|
Body string `json:"body"`
|
||||||
StatusCode int `json:"status_code,omitempty"`
|
StatusCode int `json:"status_code,omitempty"`
|
||||||
@@ -25,65 +25,35 @@ func (p *JSONProtocol) IsStreamable() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
type RequestEncoder struct {
|
func writeString(err error, dst io.Writer, str string) error {
|
||||||
*json.Encoder
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = io.WriteString(dst, str)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *JSONProtocol) DumpJSON(w io.Writer, req *http.Request) error {
|
func (h *JSONProtocol) DumpJSON(req *http.Request) error {
|
||||||
stdin := json.NewEncoder(h.in)
|
stdin := json.NewEncoder(h.in)
|
||||||
_, err := io.WriteString(h.in, `{`)
|
|
||||||
if err != nil {
|
|
||||||
// this shouldn't happen
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
bb := new(bytes.Buffer)
|
bb := new(bytes.Buffer)
|
||||||
_, err = bb.ReadFrom(req.Body)
|
_, err := bb.ReadFrom(req.Body)
|
||||||
if err != nil {
|
err = writeString(err, h.in, "{")
|
||||||
return err
|
err = writeString(err, h.in, `"body":`)
|
||||||
}
|
err = stdin.Encode(bb.String())
|
||||||
reqData := bb.String()
|
err = writeString(err, h.in, ",")
|
||||||
if reqData != "" {
|
defer bb.Reset()
|
||||||
_, err := io.WriteString(h.in, `"body": `)
|
err = writeString(err, h.in, `"headers":`)
|
||||||
if err != nil {
|
|
||||||
// this shouldn't happen
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = stdin.Encode(reqData)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = io.WriteString(h.in, `,`)
|
|
||||||
if err != nil {
|
|
||||||
// this shouldn't happen
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer bb.Reset()
|
|
||||||
}
|
|
||||||
_, err = io.WriteString(h.in, `"headers:"`)
|
|
||||||
if err != nil {
|
|
||||||
// this shouldn't happen
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = stdin.Encode(req.Header)
|
err = stdin.Encode(req.Header)
|
||||||
if err != nil {
|
err = writeString(err, h.in, "}")
|
||||||
// this shouldn't happen
|
return err
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = io.WriteString(h.in, `"}`)
|
|
||||||
if err != nil {
|
|
||||||
// this shouldn't happen
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *JSONProtocol) Dispatch(w io.Writer, req *http.Request) error {
|
func (h *JSONProtocol) Dispatch(w io.Writer, req *http.Request) error {
|
||||||
err := h.DumpJSON(w, req)
|
err := h.DumpJSON(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
jout := new(JSONIO)
|
jout := new(jsonio)
|
||||||
dec := json.NewDecoder(h.out)
|
dec := json.NewDecoder(h.out)
|
||||||
if err := dec.Decode(jout); err != nil {
|
if err := dec.Decode(jout); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
102
api/agent/protocol/json_test.go
Normal file
102
api/agent/protocol/json_test.go
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
package protocol
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"testing"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"io/ioutil"
|
||||||
|
"io"
|
||||||
|
"encoding/json"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RequestData struct {
|
||||||
|
A string `json:"a"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestJSONProtocolDumpJSONRequestWithData(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
|
||||||
|
json.NewEncoder(&buf).Encode(RequestData{A: "a"})
|
||||||
|
req.Body = ioutil.NopCloser(&buf)
|
||||||
|
|
||||||
|
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(jsonio)
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
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(jsonio)
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user