Skip to content

Commit

Permalink
Move msgpack to example
Browse files Browse the repository at this point in the history
  • Loading branch information
bailantaotao committed Apr 16, 2016
1 parent 496d495 commit 4600fee
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
40 changes: 40 additions & 0 deletions examples/msgpack/msgpack_entity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package restPack

import (
restful "github.com/emicklei/go-restful"
"gopkg.in/vmihailenco/msgpack.v2"
)

// NewEntityAccessorMPack returns a new EntityReaderWriter for accessing MessagePack content.
// This package is not initialized with such an accessor using the MIME_MSGPACK contentType.
func NewEntityAccessorMsgPack(contentType string) restful.EntityReaderWriter {
return entityMsgPackAccess{ContentType: contentType}
}

// entityOctetAccess is a EntityReaderWriter for Octet encoding
type entityMsgPackAccess struct {
// This is used for setting the Content-Type header when writing
ContentType string
}

// Read unmarshalls the value from byte slice and using msgpack to unmarshal
func (e entityMsgPackAccess) Read(req *restful.Request, v interface{}) error {
return msgpack.NewDecoder(req.Request.Body).Decode(v)
}

// Write marshals the value to byte slice and set the Content-Type Header.
func (e entityMsgPackAccess) Write(resp *restful.Response, status int, v interface{}) error {
return writeMsgPack(resp, status, e.ContentType, v)
}

// writeMsgPack marshals the value to byte slice and set the Content-Type Header.
func writeMsgPack(resp *restful.Response, status int, contentType string, v interface{}) error {
if v == nil {
resp.WriteHeader(status)
// do not write a nil representation
return nil
}
resp.Header().Set(restful.HEADER_ContentType, contentType)
resp.WriteHeader(status)
return msgpack.NewEncoder(resp).Encode(v)
}
48 changes: 48 additions & 0 deletions examples/msgpack/msgpack_entity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package restPack

import (
"bytes"
"net/http"
"net/http/httptest"
"reflect"
"testing"

restful "github.com/emicklei/go-restful"
)

const MIME_MSGPACK = "application/x-msgpack" // Accept or Content-Type used in Consumes() and/or Produces()

func TestMsgPack(t *testing.T) {

// register msg pack entity
restful.RegisterEntityAccessor(MIME_MSGPACK, NewEntityAccessorMsgPack(MIME_MSGPACK))
type Tool struct {
Name string
Vendor string
}

// Write
httpWriter := httptest.NewRecorder()
mpack := &Tool{Name: "json", Vendor: "apple"}
resp := restful.NewResponse(httpWriter)
resp.SetRequestAccepts("application/x-msgpack,*/*;q=0.8")

err := resp.WriteEntity(mpack)
if err != nil {
t.Errorf("err %v", err)
}

// Read
bodyReader := bytes.NewReader(httpWriter.Body.Bytes())
httpRequest, _ := http.NewRequest("GET", "/test", bodyReader)
httpRequest.Header.Set("Content-Type", "application/x-msgpack; charset=UTF-8")
request := restful.NewRequest(httpRequest)
readMsgPack := new(Tool)
err = request.ReadEntity(&readMsgPack)
if err != nil {
t.Errorf("err %v", err)
}
if equal := reflect.DeepEqual(mpack, readMsgPack); !equal {
t.Fatalf("should not be error")
}
}

0 comments on commit 4600fee

Please sign in to comment.