Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Protobuf format response #1475

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 6 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/gin-contrib/sse"
"github.com/gin-gonic/gin/binding"
"github.com/gin-gonic/gin/render"
"github.com/golang/protobuf/proto"
)

// Content-Type MIME of the most common data formats.
Expand Down Expand Up @@ -835,6 +836,11 @@ func (c *Context) Stream(step func(w io.Writer) bool) {
}
}

// ProtoBuf serializes the given struct as ProtoBuf into the response body.
func (c *Context) ProtoBuf(code int, obj proto.Message) {
c.Render(code, render.ProtoBuf{Data: obj})
}

/************************************/
/******** CONTENT NEGOTIATION *******/
/************************************/
Expand Down
22 changes: 21 additions & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ import (
"testing"
"time"

"io"

"github.com/gin-contrib/sse"
"github.com/gin-gonic/gin/binding"
pb "github.com/gin-gonic/gin/examples/grpc/pb"
"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
"io"
)

var _ context.Context = &Context{}
Expand Down Expand Up @@ -953,6 +956,23 @@ func TestContextRenderYAML(t *testing.T) {
assert.Equal(t, "application/x-yaml; charset=utf-8", w.HeaderMap.Get("Content-Type"))
}

// TestContextRenderProtoBuf tests that the response is serialized as YAML
// and Content-Type is set to application/x-protobuf
// and we just use the example protobuf to check if the response is correct
func TestContextRenderProtoBuf(t *testing.T) {
w := httptest.NewRecorder()
c, _ := CreateTestContext(w)

c.ProtoBuf(201, &pb.HelloRequest{Name: "Hello"})

realProtoBuf, err := proto.Marshal(&pb.HelloRequest{Name: "Hello"})
assert.Nil(t, err)

assert.Equal(t, 201, w.Code)
assert.Equal(t, realProtoBuf, w.Body.Bytes())
assert.Equal(t, "application/x-protobuf", w.HeaderMap.Get("Content-Type"))
}

func TestContextHeaders(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
c.Header("Content-Type", "text/plain")
Expand Down
33 changes: 33 additions & 0 deletions render/protobuf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package render

import (
"net/http"

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

type ProtoBuf struct {
Data proto.Message
}

var protobufContentType = []string{"application/x-protobuf"}

func (r ProtoBuf) Render(w http.ResponseWriter) error {
r.WriteContentType(w)

bytes, err := proto.Marshal(r.Data)
if err != nil {
return err
}

w.Write(bytes)
return nil
}

func (r ProtoBuf) WriteContentType(w http.ResponseWriter) {
writeContentType(w, protobufContentType)
}
1 change: 1 addition & 0 deletions render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
_ Render = MsgPack{}
_ Render = Reader{}
_ Render = AsciiJSON{}
_ Render = ProtoBuf{}
)

func writeContentType(w http.ResponseWriter, value []string) {
Expand Down