forked from emicklei/go-restful
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
496d495
commit 4600fee
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |