Commit ca52e7ff authored by Geoff Simmons's avatar Geoff Simmons

REST API error response body on Not Found for DELETE.

parent c80e65e2
...@@ -31,6 +31,7 @@ package rest ...@@ -31,6 +31,7 @@ package rest
import ( import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"os"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
...@@ -83,6 +84,11 @@ var ( ...@@ -83,6 +84,11 @@ var (
Detail: "/v1/pems/ URL path does not match " + Detail: "/v1/pems/ URL path does not match " +
"/v1/pems/{namespace}/{name}", "/v1/pems/{namespace}/{name}",
} }
errPemDeleteNotFound = ErrorDetails{
Type: "/errors/pems/delete/notFound",
Title: "PEM file to be deleted not found",
Detail: "",
}
) )
// Problem Details object per RFC7807 // Problem Details object per RFC7807
...@@ -238,8 +244,10 @@ func (h *pemsHndlr) ServeHTTP(resp http.ResponseWriter, req *http.Request) { ...@@ -238,8 +244,10 @@ func (h *pemsHndlr) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
ns, name := matches[1], matches[2] ns, name := matches[1], matches[2]
if req.Method == http.MethodDelete { if req.Method == http.MethodDelete {
if exist, err := h.files.Delete(ns, name); !exist { if exist, err := h.files.Delete(ns, name); !exist ||
status = http.StatusNotFound (err != nil && os.IsNotExist(err)) {
h.errorResponse(resp, req, now, http.StatusNotFound,
errPemDeleteNotFound, err)
} else if err != nil { } else if err != nil {
// XXX problem description in body // XXX problem description in body
status = http.StatusInternalServerError status = http.StatusInternalServerError
......
...@@ -53,6 +53,8 @@ import ( ...@@ -53,6 +53,8 @@ import (
"k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/cache"
) )
var errInstancePattern = regexp.MustCompile(`^/log/errors/\d+$`)
func TestHealthz(t *testing.T) { func TestHealthz(t *testing.T) {
version := "47.11" version := "47.11"
hndlr := &healthzHndlr{ hndlr := &healthzHndlr{
...@@ -656,10 +658,40 @@ func TestDeletePem(t *testing.T) { ...@@ -656,10 +658,40 @@ func TestDeletePem(t *testing.T) {
t.Errorf("2nd DELETE /v1/pems/ns1/secret1 status: "+ t.Errorf("2nd DELETE /v1/pems/ns1/secret1 status: "+
"got %d want %d", rr.Code, http.StatusNotFound) "got %d want %d", rr.Code, http.StatusNotFound)
} }
if rr.Result().Header.Get("Content-Length") != "0" { if rr.Header().Get("Content-Type") != problemContentType {
t.Errorf("DELETE /v1/pems/ns1/secret1 Content-Length: "+ t.Errorf("2nd DELETE /v1/pems/ns1/secret1 Content-Type: "+
"got %s want 0", "got %s want %s",
rr.Result().Header.Get("Content-Length")) rr.Header().Get("Content-Type"), problemContentType)
}
bodylen := len(rr.Body.String())
if rr.Header().Get("Content-Length") != strconv.Itoa(bodylen) {
t.Errorf("2nd DELETE /v1/pems/ns1/secret1 Content-Length: "+
"got %s want %d",
rr.Header().Get("Content-Length"), bodylen)
}
problem := &Problem{}
if err = json.Unmarshal(rr.Body.Bytes(), problem); err != nil {
t.Fatalf("2nd DELETE /v1/pems/ns1/secret1 body unmarshal: %v",
err)
}
if problem.Type != errPemDeleteNotFound.Type {
t.Errorf("2nd DELETE /v1/pems/ns1/secret1 problem type: "+
"got %s want %s",
problem.Type, errPemDeleteNotFound.Type)
}
if problem.Title != errPemDeleteNotFound.Title {
t.Errorf("2nd DELETE /v1/pems/ns1/secret1 problem title: "+
"got %s want %s",
problem.Title, errPemDeleteNotFound.Title)
}
if problem.Status != http.StatusNotFound {
t.Errorf("2nd DELETE /v1/pems/ns1/secret1 problem status: "+
"got %d want %d",
problem.Status, http.StatusNotFound)
}
if !errInstancePattern.Match([]byte(problem.Instance)) {
t.Errorf("2nd DELETE /v1/pems/ns1/secret1 problem instance: "+
"got %s want /log/errors/N", problem.Instance)
} }
req = httptest.NewRequest(http.MethodDelete, "/v1/pems/foo/bar", nil) req = httptest.NewRequest(http.MethodDelete, "/v1/pems/foo/bar", nil)
...@@ -817,8 +849,6 @@ func TestAllPem(t *testing.T) { ...@@ -817,8 +849,6 @@ func TestAllPem(t *testing.T) {
} }
} }
var errInstancePattern = regexp.MustCompile(`^/log/errors/\d+$`)
func TestErrors(t *testing.T) { func TestErrors(t *testing.T) {
client := fake.NewSimpleClientset() client := fake.NewSimpleClientset()
lister := setupSecretLister(client) lister := setupSecretLister(client)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment