Invalid filter should throw BadRequest

- for GET `/allocations` and GET `/pins` an invalid filter now result
in BadRequest
- Added tests for the same
This commit is contained in:
Kishan Mohanbhai Sagathiya 2019-04-24 21:54:12 +05:30
parent b2796b951a
commit fa8d5c973c
2 changed files with 26 additions and 1 deletions

View File

@ -743,6 +743,12 @@ func (api *API) allocationsHandler(w http.ResponseWriter, r *http.Request) {
for _, f := range strings.Split(filterStr, ",") {
filter |= types.PinTypeFromString(f)
}
if filter == types.BadType {
api.sendResponse(w, http.StatusBadRequest, errors.New("invalid filter value"), nil)
return
}
var pins []*types.Pin
err := api.rpcClient.CallContext(
r.Context(),
@ -814,7 +820,7 @@ func (api *API) statusAllHandler(w http.ResponseWriter, r *http.Request) {
filterStr := queryValues.Get("filter")
filter := types.TrackerStatusFromString(filterStr)
if filter == types.TrackerStatusUndefined && filterStr != "" {
api.sendResponse(w, autoStatus, errors.New("invalid filter value"), nil)
api.sendResponse(w, http.StatusBadRequest, errors.New("invalid filter value"), nil)
return
}

View File

@ -748,6 +748,19 @@ func TestAPIAllocationsEndpoint(t *testing.T) {
!resp[2].Cid.Equals(test.Cid3) {
t.Error("unexpected pin list: ", resp)
}
makeGet(t, rest, url(rest)+"/allocations", &resp)
if len(resp) != 3 ||
!resp[0].Cid.Equals(test.Cid1) || !resp[1].Cid.Equals(test.Cid2) ||
!resp[2].Cid.Equals(test.Cid3) {
t.Error("unexpected pin list: ", resp)
}
errResp := api.Error{}
makeGet(t, rest, url(rest)+"/allocations?filter=invalid", &errResp)
if errResp.Code != http.StatusBadRequest {
t.Error("an invalid filter value should 400")
}
}
testBothEndpoints(t, tf)
@ -851,6 +864,12 @@ func TestAPIStatusAllEndpoint(t *testing.T) {
if len(resp7) != 2 {
t.Errorf("unexpected statusAll+filter=error,pinned resp:\n %+v", resp7)
}
var errorResp api.Error
makeGet(t, rest, url(rest)+"/pins?filter=invalid", &errorResp)
if errorResp.Code != http.StatusBadRequest {
t.Error("an invalid filter value should 400")
}
}
testBothEndpoints(t, tf)