summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorJulien Dessaux2024-10-05 09:54:35 +0200
committerJulien Dessaux2024-10-06 00:15:58 +0200
commitc4ce5e86234e9f91599b87cff032228adb10fd50 (patch)
treebc328d23ccf4a4af68a055a62fbb6675905ec681 /cmd
parentfeat(tfstated): begin implementing tests (diff)
downloadtfstated-c4ce5e86234e9f91599b87cff032228adb10fd50.tar.gz
tfstated-c4ce5e86234e9f91599b87cff032228adb10fd50.tar.bz2
tfstated-c4ce5e86234e9f91599b87cff032228adb10fd50.zip
fix(tfstated): add delete handler tests
Diffstat (limited to 'cmd')
-rw-r--r--cmd/tfstated/delete.go16
-rw-r--r--cmd/tfstated/delete_test.go41
2 files changed, 47 insertions, 10 deletions
diff --git a/cmd/tfstated/delete.go b/cmd/tfstated/delete.go
index 82811b0..3b708d5 100644
--- a/cmd/tfstated/delete.go
+++ b/cmd/tfstated/delete.go
@@ -1,8 +1,6 @@
package main
import (
- "database/sql"
- "errors"
"fmt"
"net/http"
@@ -17,15 +15,13 @@ func handleDelete(db *database.DB) http.Handler {
return
}
- if err := db.DeleteState(r.URL.Path); err != nil {
- if errors.Is(err, sql.ErrNoRows) {
- _ = errorResponse(w, http.StatusNotFound,
- fmt.Errorf("state path not found: %s", r.URL.Path))
- } else {
- _ = errorResponse(w, http.StatusInternalServerError, err)
- }
- } else {
+ if success, err := db.DeleteState(r.URL.Path); err != nil {
+ _ = errorResponse(w, http.StatusInternalServerError, err)
+ } else if success {
w.WriteHeader(http.StatusOK)
+ } else {
+ _ = errorResponse(w, http.StatusNotFound,
+ fmt.Errorf("state path not found: %s", r.URL.Path))
}
})
}
diff --git a/cmd/tfstated/delete_test.go b/cmd/tfstated/delete_test.go
new file mode 100644
index 0000000..748896d
--- /dev/null
+++ b/cmd/tfstated/delete_test.go
@@ -0,0 +1,41 @@
+package main
+
+import (
+ "io"
+ "net/http"
+ "net/url"
+ "strings"
+ "testing"
+)
+
+func TestDelete(t *testing.T) {
+ tests := []struct {
+ method string
+ uri *url.URL
+ body io.Reader
+ expect string
+ status int
+ msg string
+ }{
+ {"DELETE", &url.URL{Path: "/"}, nil, "", http.StatusBadRequest, "/"},
+ {"DELETE", &url.URL{Path: "/non_existent_delete"}, nil, "", http.StatusNotFound, "non existent"},
+ {"POST", &url.URL{Path: "/test_delete"}, strings.NewReader("the_test_delete"), "", http.StatusOK, "/test_delete"},
+ {"DELETE", &url.URL{Path: "/test_delete"}, nil, "", http.StatusOK, "/test_delete"},
+ {"DELETE", &url.URL{Path: "/test_delete"}, nil, "", http.StatusNotFound, "/test_delete"},
+ }
+ for _, tt := range tests {
+ runHTTPRequest(tt.method, tt.uri, tt.body, func(r *http.Response, err error) {
+ if err != nil {
+ t.Fatalf("failed %s with error: %+v", tt.method, err)
+ } else if r.StatusCode != tt.status {
+ t.Fatalf("%s %s should %s, got %s", tt.method, tt.msg, http.StatusText(tt.status), http.StatusText(r.StatusCode))
+ } else if tt.expect != "" {
+ if body, err := io.ReadAll(r.Body); err != nil {
+ t.Fatalf("failed to read body with error: %+v", err)
+ } else if strings.Compare(string(body), tt.expect) != 0 {
+ t.Fatalf("%s should have returned \"%s\", got %s", tt.method, tt.expect, string(body))
+ }
+ }
+ })
+ }
+}