Skip to content

Commit 9cf5670

Browse files
committed
404 when key doesn't exist during deletion, 204 still when does exist
1 parent a494776 commit 9cf5670

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

cmd/short-it/main.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ func handleListURLs(w http.ResponseWriter, r *http.Request) {
401401
cursor := r.Header.Get("Cursor")
402402
limitStr := r.Header.Get("Limit")
403403
limit := maxPageSize
404-
404+
405405
if limitStr != "" {
406406
if l, err := strconv.Atoi(limitStr); err == nil {
407407
if l == 0 {
@@ -481,6 +481,11 @@ func handlePutCustomURL(w http.ResponseWriter, r *http.Request, path string) {
481481
}
482482

483483
func handleDeleteURL(w http.ResponseWriter, r *http.Request, path string) {
484+
if _, err := getURL(path); err != nil {
485+
writeJSONError(w, http.StatusNotFound, fmt.Sprintf("Not Found: The short link key '%s' does not exist.", path))
486+
return
487+
}
488+
484489
if err := deleteURL(path); err != nil {
485490
writeJSONError(w, http.StatusInternalServerError, fmt.Sprintf("Internal Server Error: Failed to delete URL '%s' from the database (%v).", path, err))
486491
return
@@ -687,7 +692,7 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
687692

688693
url, err := getURL(rootRedirectKey)
689694
if err == nil {
690-
r.URL.Path = "/"
695+
r.URL.Path = "/"
691696
handlePageView(r)
692697
http.Redirect(w, r, url, http.StatusFound)
693698
return
@@ -722,4 +727,4 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
722727
default:
723728
writeJSONError(w, http.StatusMethodNotAllowed, fmt.Sprintf("Method not allowed: Unsupported HTTP method '%s' for custom path endpoint. Use GET, PUT, or DELETE.", r.Method))
724729
}
725-
}
730+
}

cmd/short-it/main_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,14 @@ func TestDeleteURL(t *testing.T) {
301301
if err == nil {
302302
t.Error("URL should not exist after deletion")
303303
}
304+
305+
// Test DELETE non-existent key
306+
req2 := httptest.NewRequest("DELETE", "/nonexistent", nil)
307+
w2 := httptest.NewRecorder()
308+
handleDeleteURL(w2, req2, "nonexistent")
309+
if w2.Code != http.StatusNotFound {
310+
t.Errorf("Expected 404 for deleting non-existent key, got %d", w2.Code)
311+
}
304312
}
305313

306314
func TestHandleWebUICreateUsesAPIURL(t *testing.T) {

0 commit comments

Comments
 (0)