From 1c2efc21aef7ad279eaefc10ceca45262f7da535 Mon Sep 17 00:00:00 2001 From: Xinyi Wang Date: Tue, 25 Jul 2023 12:16:37 -0700 Subject: [PATCH] add more tests --- internal/resource/http/http.go | 3 ++ internal/resource/http/http_test.go | 43 +++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/internal/resource/http/http.go b/internal/resource/http/http.go index 7a988eb7961b..f2fb3de5ae48 100644 --- a/internal/resource/http/http.go +++ b/internal/resource/http/http.go @@ -134,6 +134,9 @@ func checkURL(r *http.Request) (tenancy *pbresource.Tenancy, resourceName string } } resourceName = path.Base(r.URL.Path) + if resourceName == "." || resourceName == "/" { + resourceName = "" + } return } diff --git a/internal/resource/http/http_test.go b/internal/resource/http/http_test.go index 6bc9be9a2cf0..168c1d5d2a0e 100644 --- a/internal/resource/http/http_test.go +++ b/internal/resource/http/http_test.go @@ -31,9 +31,47 @@ func TestResourceHandler(t *testing.T) { hclog.NewNullLogger(), } - t.Run("Write", func(t *testing.T) { + t.Run("should return bad request due to missing resource name", func(t *testing.T) { rsp := httptest.NewRecorder() - req := httptest.NewRequest("PUT", "/api/demo/v2/artist/keith-urban?partition=default&peer_name=local&namespace=default", strings.NewReader(` + req := httptest.NewRequest("PUT", "/?partition=default&peer_name=local&namespace=default", strings.NewReader(` + { + "metadata": { + "foo": "bar" + }, + "data": { + "name": "Keith Urban", + "genre": "GENRE_COUNTRY" + } + } + `)) + + resourceHandler.ServeHTTP(rsp, req) + + require.Equal(t, http.StatusBadRequest, rsp.Result().StatusCode) + }) + + t.Run("should return bad request due to wrong schema", func(t *testing.T) { + rsp := httptest.NewRecorder() + req := httptest.NewRequest("PUT", "/?partition=default&peer_name=local&namespace=default", strings.NewReader(` + { + "metadata": { + "foo": "bar" + }, + "tada": { + "name": "Keith Urban", + "genre": "GENRE_COUNTRY" + } + } + `)) + + resourceHandler.ServeHTTP(rsp, req) + + require.Equal(t, http.StatusBadRequest, rsp.Result().StatusCode) + }) + + t.Run("should write to the resource backend", func(t *testing.T) { + rsp := httptest.NewRecorder() + req := httptest.NewRequest("PUT", "/keith-urban?partition=default&peer_name=local&namespace=default", strings.NewReader(` { "metadata": { "foo": "bar" @@ -52,6 +90,7 @@ func TestResourceHandler(t *testing.T) { var result map[string]any require.NoError(t, json.NewDecoder(rsp.Body).Decode(&result)) require.Equal(t, "Keith Urban", result["data"].(map[string]any)["name"]) + require.Equal(t, "keith-urban", result["id"].(map[string]any)["name"]) readRsp, err := client.Read(testutil.TestContext(t), &pbresource.ReadRequest{ Id: &pbresource.ID{