Skip to content

Commit

Permalink
return 404 resp for patch when entry does not exist (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccapurso committed Nov 23, 2021
1 parent 36251b0 commit 16933c8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
6 changes: 2 additions & 4 deletions path_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,8 @@ func (b *versionedKVBackend) pathDataPatch() framework.OperationFunc {
return nil, err
}

// Returning a nil logical.Response and error will ultimately
// result in a 404 HTTP response status
if meta == nil {
return nil, nil
return logical.RespondWithStatusCode(nil, req, http.StatusNotFound)
}

config, err := b.config(ctx, req.Storage)
Expand All @@ -427,7 +425,7 @@ func (b *versionedKVBackend) pathDataPatch() framework.OperationFunc {
versionMetadata := meta.Versions[currentVersion]

if versionMetadata == nil {
return nil, nil
return logical.RespondWithStatusCode(nil, req, http.StatusNotFound)
}

// Like the read handler, initialize a resp with the version metadata
Expand Down
18 changes: 13 additions & 5 deletions path_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,12 @@ func TestVersionedKV_Patch_NotFound(t *testing.T) {
}

resp, err := b.HandleRequest(context.Background(), req)
if err != nil || resp != nil {
t.Fatalf("expected nil response for PatchOperation - err:%s resp:%#v\n", err, resp)
if err != nil || resp == nil || resp.IsError() {
t.Fatalf("PatchOperation failed - err:%s resp:%#v", err, resp)
}

if resp.Data["http_status_code"] != 404 {
t.Fatalf("expected 404 response for PatchOperation: resp:%#v", resp)
}

metadata := map[string]interface{}{
Expand All @@ -634,7 +638,7 @@ func TestVersionedKV_Patch_NotFound(t *testing.T) {

resp, err = b.HandleRequest(context.Background(), req)
if err != nil || resp != nil {
t.Fatalf("metadata CreateOperation request failed - err:%s resp:%#v\n", err, resp)
t.Fatalf("metadata CreateOperation request failed - err:%s resp:%#v", err, resp)
}

req = &logical.Request{
Expand All @@ -645,8 +649,12 @@ func TestVersionedKV_Patch_NotFound(t *testing.T) {
}

resp, err = b.HandleRequest(context.Background(), req)
if err != nil || resp != nil {
t.Fatalf("expected nil response for PatchOperation - err:%s resp:%#v\n", err, resp)
if err != nil || resp == nil || resp.IsError() {
t.Fatalf("PatchOperation failed - err:%s resp:%#v", err, resp)
}

if resp.Data["http_status_code"] != 404 {
t.Fatalf("expected 404 response for PatchOperation: resp:%#v", resp)
}
}

Expand Down

0 comments on commit 16933c8

Please sign in to comment.