Merge PR #6525: x/bank: Refactor CLI & Tests

This commit is contained in:
Alexander Bezobchuk
2020-06-30 16:59:21 -04:00
committed by GitHub
parent 56af94e6ab
commit d5049413ef
35 changed files with 665 additions and 561 deletions
-14
View File
@@ -288,11 +288,6 @@ func PostProcessResponseBare(w http.ResponseWriter, ctx client.Context, body int
default:
resp, err = marshaler.MarshalJSON(body)
if ctx.Indent && err == nil {
resp, err = codec.MarshalIndentFromJSON(resp)
}
if CheckInternalServerError(w, err) {
return
}
@@ -332,11 +327,6 @@ func PostProcessResponse(w http.ResponseWriter, ctx client.Context, resp interfa
default:
result, err = marshaler.MarshalJSON(resp)
if ctx.Indent && err == nil {
result, err = codec.MarshalIndentFromJSON(result)
}
if CheckInternalServerError(w, err) {
return
}
@@ -345,10 +335,6 @@ func PostProcessResponse(w http.ResponseWriter, ctx client.Context, resp interfa
wrappedResp := NewResponseWithHeight(ctx.Height, result)
output, err := marshaler.MarshalJSON(wrappedResp)
if ctx.Indent && err == nil {
output, err = codec.MarshalIndentFromJSON(output)
}
if CheckInternalServerError(w, err) {
return
}
+6 -24
View File
@@ -212,9 +212,6 @@ func TestProcessPostResponse(t *testing.T) {
expectedNoIndent, err := ctx.Codec.MarshalJSON(respNoIndent)
require.Nil(t, err)
expectedWithIndent, err := codec.MarshalIndentFromJSON(expectedNoIndent)
require.Nil(t, err)
// check that negative height writes an error
w := httptest.NewRecorder()
ctx = ctx.WithHeight(-1)
@@ -223,10 +220,7 @@ func TestProcessPostResponse(t *testing.T) {
// check that height returns expected response
ctx = ctx.WithHeight(height)
runPostProcessResponse(t, ctx, acc, expectedNoIndent, false)
// check height with indent
runPostProcessResponse(t, ctx, acc, expectedWithIndent, true)
runPostProcessResponse(t, ctx, acc, expectedNoIndent)
}
func TestReadRESTReq(t *testing.T) {
@@ -329,7 +323,7 @@ func TestPostProcessResponseBare(t *testing.T) {
require.Equal(t, "text string", string(got))
// write struct and indent response
clientCtx = client.Context{Indent: true}.WithCodec(codec.New())
clientCtx = client.Context{}.WithCodec(codec.New())
w = httptest.NewRecorder()
data := struct {
X int `json:"x"`
@@ -345,13 +339,10 @@ func TestPostProcessResponseBare(t *testing.T) {
require.NoError(t, err)
t.Cleanup(func() { res.Body.Close() })
require.Equal(t, `{
"s": "test",
"x": "10"
}`, string(got))
require.Equal(t, "{\"x\":\"10\",\"s\":\"test\"}", string(got))
// write struct, don't indent response
clientCtx = client.Context{Indent: false}.WithCodec(codec.New())
clientCtx = client.Context{}.WithCodec(codec.New())
w = httptest.NewRecorder()
data = struct {
X int `json:"x"`
@@ -370,7 +361,7 @@ func TestPostProcessResponseBare(t *testing.T) {
require.Equal(t, `{"x":"10","s":"test"}`, string(got))
// test marshalling failure
clientCtx = client.Context{Indent: false}.WithCodec(codec.New())
clientCtx = client.Context{}.WithCodec(codec.New())
w = httptest.NewRecorder()
data2 := badJSONMarshaller{}
@@ -396,11 +387,7 @@ func (badJSONMarshaller) MarshalJSON() ([]byte, error) {
// asserts that ResponseRecorder returns the expected code and body
// runs PostProcessResponse on the objects regular interface and on
// the marshalled struct.
func runPostProcessResponse(t *testing.T, ctx client.Context, obj interface{}, expectedBody []byte, indent bool) {
if indent {
ctx.Indent = indent
}
func runPostProcessResponse(t *testing.T, ctx client.Context, obj interface{}, expectedBody []byte) {
// test using regular struct
w := httptest.NewRecorder()
@@ -417,11 +404,6 @@ func runPostProcessResponse(t *testing.T, ctx client.Context, obj interface{}, e
marshalled, err := ctx.Codec.MarshalJSON(obj)
require.NoError(t, err)
if indent {
marshalled, err = codec.MarshalIndentFromJSON(marshalled)
require.NoError(t, err)
}
// test using marshalled struct
w = httptest.NewRecorder()
rest.PostProcessResponse(w, ctx, marshalled)