Fmt and docs

This commit is contained in:
Denis Makogon
2017-10-07 02:59:08 +03:00
parent 6141344e5f
commit e4684096f7
3 changed files with 24 additions and 23 deletions

View File

@@ -42,17 +42,17 @@ func (h *JSONProtocol) DumpJSON(req *http.Request) error {
} }
err = writeString(err, h.in, "{") err = writeString(err, h.in, "{")
err = writeString(err, h.in, `"body":`) err = writeString(err, h.in, `"body":`)
err = stdin.Encode(bb.String())
if err != nil { if err != nil {
return err return err
} }
err = stdin.Encode(bb.String())
err = writeString(err, h.in, ",") err = writeString(err, h.in, ",")
defer bb.Reset() defer bb.Reset()
err = writeString(err, h.in, `"headers":`) err = writeString(err, h.in, `"headers":`)
err = stdin.Encode(req.Header)
if err != nil { if err != nil {
return err return err
} }
err = stdin.Encode(req.Header)
err = writeString(err, h.in, "}") err = writeString(err, h.in, "}")
return err return err
} }

View File

@@ -2,33 +2,37 @@ package protocol
import ( import (
"bytes" "bytes"
"testing" "encoding/json"
"io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"io/ioutil"
"io"
"encoding/json"
"reflect" "reflect"
"testing"
) )
type RequestData struct { type RequestData struct {
A string `json:"a"` A string `json:"a"`
} }
type fuckReed struct {
Body RequestData `json:"body"`
}
func TestJSONProtocolDumpJSONRequestWithData(t *testing.T) { func TestJSONProtocolDumpJSONRequestWithData(t *testing.T) {
req := &http.Request{ req := &http.Request{
Method: http.MethodPost, Method: http.MethodPost,
URL: &url.URL{ URL: &url.URL{
Scheme: "http", Scheme: "http",
Host: "localhost:8080", Host: "localhost:8080",
Path: "/v1/apps", Path: "/v1/apps",
RawQuery: "something=something&etc=etc", RawQuery: "something=something&etc=etc",
}, },
ProtoMajor: 1, ProtoMajor: 1,
ProtoMinor: 1, ProtoMinor: 1,
Header: http.Header{ Header: http.Header{
"Host": []string{"localhost:8080"}, "Host": []string{"localhost:8080"},
"User-Agent": []string{"curl/7.51.0"}, "User-Agent": []string{"curl/7.51.0"},
"Content-Type": []string{"application/json"}, "Content-Type": []string{"application/json"},
}, },
Host: "localhost:8080", Host: "localhost:8080",
@@ -39,7 +43,7 @@ func TestJSONProtocolDumpJSONRequestWithData(t *testing.T) {
req.Body = ioutil.NopCloser(&buf) req.Body = ioutil.NopCloser(&buf)
r, w := io.Pipe() r, w := io.Pipe()
proto := JSONProtocol{w,r} proto := JSONProtocol{w, r}
go func() { go func() {
err := proto.DumpJSON(req) err := proto.DumpJSON(req)
if err != nil { if err != nil {
@@ -73,16 +77,16 @@ func TestJSONProtocolDumpJSONRequestWithoutData(t *testing.T) {
req := &http.Request{ req := &http.Request{
Method: http.MethodPost, Method: http.MethodPost,
URL: &url.URL{ URL: &url.URL{
Scheme: "http", Scheme: "http",
Host: "localhost:8080", Host: "localhost:8080",
Path: "/v1/apps", Path: "/v1/apps",
RawQuery: "something=something&etc=etc", RawQuery: "something=something&etc=etc",
}, },
ProtoMajor: 1, ProtoMajor: 1,
ProtoMinor: 1, ProtoMinor: 1,
Header: http.Header{ Header: http.Header{
"Host": []string{"localhost:8080"}, "Host": []string{"localhost:8080"},
"User-Agent": []string{"curl/7.51.0"}, "User-Agent": []string{"curl/7.51.0"},
"Content-Type": []string{"application/json"}, "Content-Type": []string{"application/json"},
}, },
Host: "localhost:8080", Host: "localhost:8080",
@@ -91,7 +95,7 @@ func TestJSONProtocolDumpJSONRequestWithoutData(t *testing.T) {
req.Body = ioutil.NopCloser(&buf) req.Body = ioutil.NopCloser(&buf)
r, w := io.Pipe() r, w := io.Pipe()
proto := JSONProtocol{w,r} proto := JSONProtocol{w, r}
go func() { go func() {
err := proto.DumpJSON(req) err := proto.DumpJSON(req)
if err != nil { if err != nil {

View File

@@ -78,9 +78,7 @@ Fn accepts request data of the following format:
```json ```json
{ {
"body": { "some": "input"
"some": "input"
}
} }
``` ```
@@ -88,12 +86,11 @@ Internally function receives data in following format:
```json ```json
{ {
"body": "{\"some\":\"data\"}\n", "body": "{\"some\":\"input\"}\n",
"headers": { "headers": {
"yo": ["dawg"] "yo": ["dawg"]
} }
} }
``` ```
Function's output format should have following format: Function's output format should have following format: