From 681c48fd12cd3bf2f61227ac3543890c7279936e Mon Sep 17 00:00:00 2001 From: Michael Baird Date: Thu, 29 Jun 2017 09:31:21 -0500 Subject: [PATCH] add more tests for index and input-definition not found --- handler.go | 2 +- handler_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/handler.go b/handler.go index 63ecffac0..88f1e3fa6 100644 --- a/handler.go +++ b/handler.go @@ -1595,7 +1595,7 @@ func (h *Handler) handleDeleteInputDefinition(w http.ResponseWriter, r *http.Req // Delete input definition from the index. if err := index.DeleteInputDefinition(inputDefName); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + http.Error(w, err.Error(), http.StatusNotFound) return } diff --git a/handler_test.go b/handler_test.go index a88ab1f64..a0654adb4 100644 --- a/handler_test.go +++ b/handler_test.go @@ -1121,6 +1121,15 @@ func TestHandler_CreateInputDefinition(t *testing.T) { t.Fatalf("unexpected body: %s", body) } + // Test index not found + w = httptest.NewRecorder() + h.ServeHTTP(w, MustNewHTTPRequest("POST", "/index/foo/input-definition/input2", bytes.NewBuffer(inputBody))) + if w.Code != http.StatusNotFound { + t.Fatalf("unexpected status code: %d", w.Code) + } else if body := w.Body.String(); body != pilosa.ErrIndexNotFound.Error()+"\n" { + t.Fatalf("unexpected body: %s", body) + } + } // Ensure throwing error if there's duplicated primaryKey field. @@ -1247,6 +1256,14 @@ func TestHandler_DeleteInputDefinition(t *testing.T) { if err != nil { t.Fatal(err) } + + // Test definition not found + w = httptest.NewRecorder() + h.ServeHTTP(w, MustNewHTTPRequest("DELETE", "/index/i0/input-definition/foo", strings.NewReader(""))) + if w.Code != http.StatusNotFound { + t.Fatalf("unexpected status code: %d", w.Code) + } + w = httptest.NewRecorder() h.ServeHTTP(w, MustNewHTTPRequest("DELETE", "/index/i0/input-definition/test", strings.NewReader(""))) if w.Code != http.StatusOK { @@ -1402,7 +1419,24 @@ func TestHandler_CreateInput(t *testing.T) { h := NewHandler() h.Holder = hldr.Holder h.Cluster = NewCluster(1) + + // Return error if index does not exist w := httptest.NewRecorder() + h.ServeHTTP(w, MustNewHTTPRequest("POST", "/index/foo/input/input1", bytes.NewBuffer(inputBody))) + if w.Code != http.StatusNotFound { + t.Fatalf("unexpected status code: %d", w.Code) + } else if body := w.Body.String(); body != pilosa.ErrIndexNotFound.Error()+"\n" { + t.Fatalf("unexpected body: %s, expect: %s", body, pilosa.ErrIndexNotFound) + } + + // Check non existant definition + w = httptest.NewRecorder() + h.ServeHTTP(w, MustNewHTTPRequest("POST", "/index/i0/input/input2", bytes.NewBuffer(inputBody))) + if w.Code != http.StatusNotFound { + t.Fatalf("unexpected status code: %d", w.Code) + } + + w = httptest.NewRecorder() h.ServeHTTP(w, MustNewHTTPRequest("POST", "/index/i0/input/input1", bytes.NewBuffer(inputBody))) if w.Code != http.StatusOK { t.Fatalf("unexpected status code: %d", w.Code)