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
import (
"encoding/json"
"net/http"
"os"
"regexp"
"strconv"
"strings"
......@@ -83,6 +84,11 @@ var (
Detail: "/v1/pems/ URL path does not match " +
"/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
......@@ -238,8 +244,10 @@ func (h *pemsHndlr) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
ns, name := matches[1], matches[2]
if req.Method == http.MethodDelete {
if exist, err := h.files.Delete(ns, name); !exist {
status = http.StatusNotFound
if exist, err := h.files.Delete(ns, name); !exist ||
(err != nil && os.IsNotExist(err)) {
h.errorResponse(resp, req, now, http.StatusNotFound,
errPemDeleteNotFound, err)
} else if err != nil {
// XXX problem description in body
status = http.StatusInternalServerError
......
......@@ -53,6 +53,8 @@ import (
"k8s.io/client-go/tools/cache"
)
var errInstancePattern = regexp.MustCompile(`^/log/errors/\d+$`)
func TestHealthz(t *testing.T) {
version := "47.11"
hndlr := &healthzHndlr{
......@@ -656,10 +658,40 @@ func TestDeletePem(t *testing.T) {
t.Errorf("2nd DELETE /v1/pems/ns1/secret1 status: "+
"got %d want %d", rr.Code, http.StatusNotFound)
}
if rr.Result().Header.Get("Content-Length") != "0" {
t.Errorf("DELETE /v1/pems/ns1/secret1 Content-Length: "+
"got %s want 0",
rr.Result().Header.Get("Content-Length"))
if rr.Header().Get("Content-Type") != problemContentType {
t.Errorf("2nd DELETE /v1/pems/ns1/secret1 Content-Type: "+
"got %s want %s",
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)
......@@ -817,8 +849,6 @@ func TestAllPem(t *testing.T) {
}
}
var errInstancePattern = regexp.MustCompile(`^/log/errors/\d+$`)
func TestErrors(t *testing.T) {
client := fake.NewSimpleClientset()
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