-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[version compatibility] fix up when response.ResultCode is 200 but re…
…sponse.Success is false (#699) * fix up when response.ResultCode is 200 but is not success * fix up when response.ResultCode is 200 but is not success * fix up when response.ResultCode is 200 but is not success * fix up when response.ResultCode is 200 but is not success * tiny fix
- Loading branch information
Showing
5 changed files
with
80 additions
and
4 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
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,10 @@ | ||
package rpc_response | ||
|
||
type ResponseCode int | ||
|
||
const ( | ||
ResponseSuccessCode ResponseCode = 200 | ||
ResponseFailCode ResponseCode = 500 | ||
|
||
ResponseSuccessField = "success" | ||
) |
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
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 rpc_response | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestRpcResponseIsSuccess(t *testing.T) { | ||
responseBody0 := `{"resultCode":200,"errorCode":0}` | ||
responseBody1 := `{"resultCode":200,"errorCode":0,"success":true}` | ||
responseBody2 := `{"resultCode":200,"errorCode":0,"success":"true"}` | ||
responseBody3 := `{"resultCode":200,"errorCode":0,"success":false}` | ||
responseBody4 := `{"resultCode":500,"errorCode":0,"success":true}` | ||
responseBody5 := `{"resultCode":500,"errorCode":0,"success":false}` | ||
|
||
responseBodyList := make([]string, 0) | ||
responseBodyList = append(responseBodyList, responseBody0, responseBody1, responseBody2, responseBody3, responseBody4, responseBody5) | ||
for k, v := range ClientResponseMapping { | ||
t.Run("test "+k, func(t *testing.T) { | ||
for index, responseBody := range responseBodyList { | ||
response, err := InnerResponseJsonUnmarshal([]byte(responseBody), v) | ||
switch index { | ||
case 0, 1, 4: | ||
assert.True(t, response.IsSuccess()) | ||
break | ||
case 3, 5: | ||
assert.False(t, response.IsSuccess()) | ||
break | ||
case 2: | ||
assert.Nil(t, response) | ||
assert.NotNil(t, err) | ||
t.Logf("handle %d failed with responseBody: %s", index, responseBody) | ||
break | ||
default: | ||
panic("unknown index") | ||
} | ||
} | ||
}) | ||
} | ||
} |
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,24 @@ | ||
package rpc_response | ||
|
||
import "encoding/json" | ||
|
||
func InnerResponseJsonUnmarshal(responseBody []byte, responseFunc func() IResponse) (IResponse, error) { | ||
response := responseFunc() | ||
err := json.Unmarshal(responseBody, response) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !response.IsSuccess() { | ||
tempFiledMap := make(map[string]interface{}) | ||
err = json.Unmarshal(responseBody, &tempFiledMap) | ||
if err != nil { | ||
return response, nil | ||
} | ||
if _, ok := tempFiledMap[ResponseSuccessField]; !ok { | ||
response.SetSuccess(response.GetResultCode() == int(ResponseSuccessCode)) | ||
} | ||
} | ||
return response, err | ||
|
||
} |