Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xwa153 committed Jul 25, 2023
1 parent eb96780 commit 1c2efc2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
3 changes: 3 additions & 0 deletions internal/resource/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
43 changes: 41 additions & 2 deletions internal/resource/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand Down

0 comments on commit 1c2efc2

Please sign in to comment.