Update json (#463)

* wip

* wip

* Added more fields to JSON and added blank line between objects.

* Update tests.

* wip

* Updated to represent recent discussions.

* Fixed up the json test

* More docs

* Changed from blank line to bracket, newline, open bracket.

* Blank line added back, easier for delimiting.
This commit is contained in:
Travis Reeder
2017-11-16 09:59:13 -08:00
committed by GitHub
parent 279e5248ba
commit 96cfc9f5c1
8 changed files with 266 additions and 81 deletions

View File

@@ -9,18 +9,14 @@ import (
"net/url"
"reflect"
"testing"
"github.com/fnproject/fn/api/models"
)
type RequestData struct {
A string `json:"a"`
}
type funcRequestBody struct {
Body string `json:"body"`
Headers http.Header `json:"headers"`
QueryParameters string `json:"query_parameters"`
}
func setupRequest(data interface{}) *http.Request {
req := &http.Request{
Method: http.MethodPost,
@@ -48,19 +44,21 @@ func setupRequest(data interface{}) *http.Request {
return req
}
func TestJSONProtocolDumpJSONRequestWithData(t *testing.T) {
func TestJSONProtocolwriteJSONInputRequestWithData(t *testing.T) {
rDataBefore := RequestData{A: "a"}
req := setupRequest(rDataBefore)
r, w := io.Pipe()
call := &models.Call{}
ci := &callInfoImpl{call, req}
proto := JSONProtocol{w, r}
go func() {
err := proto.DumpJSON(req)
err := proto.writeJSONToContainer(ci)
if err != nil {
t.Error(err.Error())
}
w.Close()
}()
incomingReq := new(funcRequestBody)
incomingReq := &jsonIn{}
bb := new(bytes.Buffer)
_, err := bb.ReadFrom(r)
@@ -82,19 +80,21 @@ func TestJSONProtocolDumpJSONRequestWithData(t *testing.T) {
}
}
func TestJSONProtocolDumpJSONRequestWithoutData(t *testing.T) {
func TestJSONProtocolwriteJSONInputRequestWithoutData(t *testing.T) {
req := setupRequest(nil)
call := &models.Call{}
r, w := io.Pipe()
ci := &callInfoImpl{call, req}
proto := JSONProtocol{w, r}
go func() {
err := proto.DumpJSON(req)
err := proto.writeJSONToContainer(ci)
if err != nil {
t.Error(err.Error())
}
w.Close()
}()
incomingReq := new(funcRequestBody)
incomingReq := &jsonIn{}
bb := new(bytes.Buffer)
_, err := bb.ReadFrom(r)
@@ -109,25 +109,27 @@ func TestJSONProtocolDumpJSONRequestWithoutData(t *testing.T) {
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.Protocol.Headers); !ok {
t.Errorf("Request headers assertion mismatch: expected: %s, got %s",
req.Header, incomingReq.Headers)
req.Header, incomingReq.Protocol.Headers)
}
}
func TestJSONProtocolDumpJSONRequestWithQuery(t *testing.T) {
func TestJSONProtocolwriteJSONInputRequestWithQuery(t *testing.T) {
req := setupRequest(nil)
r, w := io.Pipe()
call := &models.Call{}
ci := &callInfoImpl{call, req}
proto := JSONProtocol{w, r}
go func() {
err := proto.DumpJSON(req)
err := proto.writeJSONToContainer(ci)
if err != nil {
t.Error(err.Error())
}
w.Close()
}()
incomingReq := new(funcRequestBody)
incomingReq := &jsonIn{}
bb := new(bytes.Buffer)
_, err := bb.ReadFrom(r)
@@ -138,8 +140,8 @@ func TestJSONProtocolDumpJSONRequestWithQuery(t *testing.T) {
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)
if incomingReq.Protocol.RequestURL != req.URL.RequestURI() {
t.Errorf("Request URL does not match protocol URL: expected: %s, got %s",
req.URL.RequestURI(), incomingReq.Protocol.RequestURL)
}
}