Skip to content

Commit

Permalink
Give MgsPack, expect MsgPack
Browse files Browse the repository at this point in the history
  • Loading branch information
joe94 committed Nov 13, 2017
1 parent f369d2d commit 5bda297
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/tr1d1um/communication.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ func (tr1 *Tr1SendAndHandle) HandleResponse(err error, respFromServer *http.Resp
return
}

if RDKResponse, encodingErr := tr1.ExtractPayload(respFromServer.Body, wrp.JSON); encodingErr == nil {
if RDKResponse, encodingErr := tr1.ExtractPayload(respFromServer.Body, wrp.Msgpack); encodingErr == nil {
if RDKRespCode, RDKErr := GetStatusCodeFromRDKResponse(RDKResponse); RDKErr == nil && RDKRespCode != http.StatusInternalServerError {
tr1Resp.Code = RDKRespCode
}
tr1Resp.Body = RDKResponse
} else {
ReportError(encodingErr, tr1Resp)
errorLogger.Log(logging.ErrorKey(), err)
errorLogger.Log(logging.MessageKey(), "could not extract payload from wrp body", logging.ErrorKey(), encodingErr)
}

ForwardHeadersByPrefix("X", respFromServer, tr1Resp)
Expand Down
10 changes: 5 additions & 5 deletions src/tr1d1um/communication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestHandleResponse(t *testing.T) {

t.Run("ExtractPayloadFail", func(t *testing.T) {
fakeResponse := &http.Response{StatusCode: http.StatusOK}
mockEncoding.On("ExtractPayload", fakeResponse.Body, wrp.JSON).Return([]byte(""),
mockEncoding.On("ExtractPayload", fakeResponse.Body, wrp.Msgpack).Return([]byte(""),
errors.New(errMsg)).Once()
recorder := Tr1d1umResponse{}.New()
tr1.HandleResponse(nil, fakeResponse, recorder, false)
Expand All @@ -129,7 +129,7 @@ func TestHandleResponse(t *testing.T) {

t.Run("ExtractPayloadTimeout", func(t *testing.T) {
fakeResponse := &http.Response{StatusCode: http.StatusOK}
mockEncoding.On("ExtractPayload", fakeResponse.Body, wrp.JSON).Return([]byte(""),
mockEncoding.On("ExtractPayload", fakeResponse.Body, wrp.Msgpack).Return([]byte(""),
context.Canceled).Once()
recorder := Tr1d1umResponse{}.New()
tr1.HandleResponse(nil, fakeResponse, recorder, false)
Expand All @@ -150,14 +150,14 @@ func TestHandleResponse(t *testing.T) {

assert.EqualValues(http.StatusOK, recorder.Code)
assert.EqualValues(bodyString, string(recorder.Body))
mockEncoding.AssertNotCalled(t, "ExtractPayload", fakeResponse.Body, wrp.JSON)
mockEncoding.AssertNotCalled(t, "ExtractPayload", fakeResponse.Body, wrp.Msgpack)
})

t.Run("GoodRDKResponse", func(t *testing.T) {
fakeResponse := &http.Response{StatusCode: http.StatusOK}
extractedData := []byte(`{"statusCode": 202}`)

mockEncoding.On("ExtractPayload", fakeResponse.Body, wrp.JSON).Return(extractedData, nil).Once()
mockEncoding.On("ExtractPayload", fakeResponse.Body, wrp.Msgpack).Return(extractedData, nil).Once()
recorder := Tr1d1umResponse{}.New()
tr1.HandleResponse(nil, fakeResponse, recorder, false)

Expand All @@ -170,7 +170,7 @@ func TestHandleResponse(t *testing.T) {
fakeResponse := &http.Response{StatusCode: http.StatusOK}
extractedData := []byte(`{"statusCode": 500}`)

mockEncoding.On("ExtractPayload", fakeResponse.Body, wrp.JSON).Return(extractedData, nil).Once()
mockEncoding.On("ExtractPayload", fakeResponse.Body, wrp.Msgpack).Return(extractedData, nil).Once()
recorder := Tr1d1umResponse{}.New()
tr1.HandleResponse(nil, fakeResponse, recorder, false)

Expand Down
5 changes: 2 additions & 3 deletions src/tr1d1um/retries.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ import (
var (
errInvalidMaxRetriesVal = errors.New("retry: MaxRetries should be >= 1")
errUndefinedShouldRetry = errors.New("retry: ShouldRetry function is required")
errUndefinedLogger = errors.New("retry: logger is undefined")
errUndefinedRetryOp = errors.New("retry: operation to retry is undefined")
errUndefinedLogger = errors.New("retry: logger is undefined")
errUndefinedRetryOp = errors.New("retry: operation to retry is undefined")
)

//RetryStrategy defines
type RetryStrategy interface {
Execute(func(...interface{}) (interface{}, error), ...interface{}) (interface{}, error)
}


//Retry is the realization of RetryStrategy. It
type Retry struct {
log.Logger
Expand Down
2 changes: 1 addition & 1 deletion src/tr1d1um/retries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestRetryCheckDependencies(t *testing.T) {
retry := Retry{
Interval: time.Second,
MaxRetries: 1,
Logger: logging.DefaultLogger(),
Logger: logging.DefaultLogger(),
ShouldRetry: func(_ interface{}, _ error) bool { return true },
OnInternalFail: func() interface{} {
return -1
Expand Down
1 change: 0 additions & 1 deletion src/tr1d1um/tr1d1um_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ func WriteResponse(message string, statusCode int, tr1Resp *Tr1d1umResponse) {
tr1Resp.Body = []byte(fmt.Sprintf(`{"message":"%s"}`, message))
}


//WriteResponseWriter is a tiny helper function that passes responses (In Json format only for now)
//to a caller through a ResponseWriter
func WriteResponseWriter(message string, statusCode int, origin http.ResponseWriter) {
Expand Down

0 comments on commit 5bda297

Please sign in to comment.