Skip to content

Commit

Permalink
Replace all REST query empty result HTTP Status codes to 200 (#4065)
Browse files Browse the repository at this point in the history
All query params that are empty now returns success
(http.StatusOK) instead of mixed http status codes
(http.StatusNoContent/http.StatusBadRequest/
http.StatusInternalServerError)

Closes: #2007
  • Loading branch information
MarinX authored and alessio committed Apr 10, 2019
1 parent 8550d87 commit b50b25d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#2007 Return 200 status code on empty results
25 changes: 24 additions & 1 deletion client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestCoinSend(t *testing.T) {

// query empty
res, body := Request(t, port, "GET", fmt.Sprintf("/auth/accounts/%s", someFakeAddr), nil)
require.Equal(t, http.StatusNoContent, res.StatusCode, body)
require.Equal(t, http.StatusOK, res.StatusCode, body)

acc := getAccount(t, port, addr)
initialBalance := acc.GetCoins()
Expand Down Expand Up @@ -1032,3 +1032,26 @@ func TestMintingQueries(t *testing.T) {
var annualProvisions sdk.Dec
require.NoError(t, cdc.UnmarshalJSON([]byte(body), &annualProvisions))
}

func TestAccountBalanceQuery(t *testing.T) {
kb, err := keys.NewKeyBaseFromDir(InitClientHome(t, ""))
require.NoError(t, err)
addr, _ := CreateAddr(t, name1, pw, kb)
cleanup, _, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr}, true)
defer cleanup()

bz, err := hex.DecodeString("8FA6AB57AD6870F6B5B2E57735F38F2F30E73CB6")
require.NoError(t, err)
someFakeAddr := sdk.AccAddress(bz)

// empty account
res, body := Request(t, port, "GET", fmt.Sprintf("/auth/accounts/%s", someFakeAddr), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
require.Contains(t, body, `"type":"auth/Account"`)

// empty account balance
res, body = Request(t, port, "GET", fmt.Sprintf("/bank/balances/%s", someFakeAddr), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
require.Contains(t, body, "[]")

}
6 changes: 3 additions & 3 deletions x/auth/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func QueryAccountRequestHandlerFn(
return
}

// the query will return empty if there is no data for this account
// the query will return empty account if there is no data
if len(res) == 0 {
w.WriteHeader(http.StatusNoContent)
rest.PostProcessResponse(w, cdc, auth.BaseAccount{}, cliCtx.Indent)
return
}

Expand Down Expand Up @@ -88,7 +88,7 @@ func QueryBalancesRequestHandlerFn(

// the query will return empty if there is no data for this account
if len(res) == 0 {
w.WriteHeader(http.StatusNoContent)
rest.PostProcessResponse(w, cdc, sdk.Coins{}, cliCtx.Indent)
return
}

Expand Down
11 changes: 3 additions & 8 deletions x/slashing/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ func signingInfoHandlerFn(cliCtx context.CLIContext, storeName string, cdc *code
return
}

if code == http.StatusNoContent {
w.WriteHeader(http.StatusNoContent)
return
}

rest.PostProcessResponse(w, cdc, signingInfo, cliCtx.Indent)
}
}
Expand Down Expand Up @@ -81,7 +76,7 @@ func signingInfoHandlerListFn(cliCtx context.CLIContext, storeName string, cdc *
}

if len(validators.Validators) == 0 {
w.WriteHeader(http.StatusNoContent)
rest.PostProcessResponse(w, cdc, signingInfoList, cliCtx.Indent)
return
}

Expand All @@ -100,7 +95,7 @@ func signingInfoHandlerListFn(cliCtx context.CLIContext, storeName string, cdc *
}

if len(signingInfoList) == 0 {
w.WriteHeader(http.StatusNoContent)
rest.PostProcessResponse(w, cdc, signingInfoList, cliCtx.Indent)
return
}

Expand Down Expand Up @@ -132,7 +127,7 @@ func getSigningInfo(cliCtx context.CLIContext, storeName string, cdc *codec.Code
}

if len(res) == 0 {
code = http.StatusNoContent
code = http.StatusOK
return
}

Expand Down

0 comments on commit b50b25d

Please sign in to comment.