Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions transport/http/proto/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package proto

import (
"bytes"
"context"
"errors"
"io/ioutil"
"net/http"

httptransport "github.com/go-kit/kit/transport/http"
"github.com/golang/protobuf/proto"
)

// EncodeProtoRequest is an EncodeRequestFunc that serializes the request as Protobuf.
// If the request implements Headerer, the provided headers will be applied
// to the request. If the given request does not implement proto.Message, an error will
// be returned.
func EncodeProtoRequest(_ context.Context, r *http.Request, preq interface{}) error {
r.Header.Set("Content-Type", "application/x-protobuf")
if headerer, ok := preq.(httptransport.Headerer); ok {
for k := range headerer.Headers() {
r.Header.Set(k, headerer.Headers().Get(k))
}
}
req, ok := preq.(proto.Message)
if !ok {
return errors.New("response does not implement proto.Message")
}

b, err := proto.Marshal(req)
if err != nil {
return err
}
r.ContentLength = int64(len(b))
r.Body = ioutil.NopCloser(bytes.NewReader(b))
return nil
}
94 changes: 94 additions & 0 deletions transport/http/proto/proto_pb_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 95 additions & 0 deletions transport/http/proto/proto_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package proto

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"

"github.com/golang/protobuf/proto"
)

func TestEncodeProtoRequest(t *testing.T) {
cat := &Cat{Name: "Ziggy", Age: 13, Breed: "Lumpy"}

r := httptest.NewRequest(http.MethodGet, "/cat", nil)

err := EncodeProtoRequest(nil, r, cat)
if err != nil {
t.Errorf("expected no encoding errors but got: %s", err)
return
}

const xproto = "application/x-protobuf"
if typ := r.Header.Get("Content-Type"); typ != xproto {
t.Errorf("expected content type of %q, got %q", xproto, typ)
return
}

bod, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Errorf("expected no read errors but got: %s", err)
return
}
defer r.Body.Close()

var got Cat
err = proto.Unmarshal(bod, &got)
if err != nil {
t.Errorf("expected no proto errors but got: %s", err)
return
}

if !proto.Equal(&got, cat) {
t.Errorf("expected cats to be equal but got:\n\n%#v\n\nwant:\n\n%#v", got, cat)
return
}
}

func TestEncodeProtoResponse(t *testing.T) {
cat := &Cat{Name: "Ziggy", Age: 13, Breed: "Lumpy"}

wr := httptest.NewRecorder()

err := EncodeProtoResponse(nil, wr, cat)
if err != nil {
t.Errorf("expected no encoding errors but got: %s", err)
return
}

w := wr.Result()

const xproto = "application/x-protobuf"
if typ := w.Header.Get("Content-Type"); typ != xproto {
t.Errorf("expected content type of %q, got %q", xproto, typ)
return
}

if w.StatusCode != http.StatusTeapot {
t.Errorf("expected status code of %d, got %d", http.StatusTeapot, w.StatusCode)
return
}

bod, err := ioutil.ReadAll(w.Body)
if err != nil {
t.Errorf("expected no read errors but got: %s", err)
return
}
defer w.Body.Close()

var got Cat
err = proto.Unmarshal(bod, &got)
if err != nil {
t.Errorf("expected no proto errors but got: %s", err)
return
}

if !proto.Equal(&got, cat) {
t.Errorf("expected cats to be equal but got:\n\n%#v\n\nwant:\n\n%#v", got, cat)
return
}
}

func (c *Cat) StatusCode() int {
return http.StatusTeapot
}
7 changes: 7 additions & 0 deletions transport/http/proto/proto_test.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax = "proto3";

message Cat {
int32 Age = 1;
string Breed = 2;
string Name = 3;
}
47 changes: 47 additions & 0 deletions transport/http/proto/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package proto

import (
"context"
"errors"
"net/http"

httptransport "github.com/go-kit/kit/transport/http"
"github.com/golang/protobuf/proto"
)

// EncodeProtoResponse is an EncodeResponseFunc that serializes the response as Protobuf.
// Many Proto-over-HTTP services can use it as a sensible default. If the response
// implements Headerer, the provided headers will be applied to the response. If the
// response implements StatusCoder, the provided StatusCode will be used instead of 200.
func EncodeProtoResponse(ctx context.Context, w http.ResponseWriter, pres interface{}) error {
res, ok := pres.(proto.Message)
if !ok {
return errors.New("response does not implement proto.Message")
}
w.Header().Set("Content-Type", "application/x-protobuf")
if headerer, ok := w.(httptransport.Headerer); ok {
for k := range headerer.Headers() {
w.Header().Set(k, headerer.Headers().Get(k))
}
}
code := http.StatusOK
if sc, ok := pres.(httptransport.StatusCoder); ok {
code = sc.StatusCode()
}
w.WriteHeader(code)
if code == http.StatusNoContent {
return nil
}
if res == nil {
return nil
}
b, err := proto.Marshal(res)
if err != nil {
return err
}
_, err = w.Write(b)
if err != nil {
return err
}
return nil
}