Skip to content

Commit

Permalink
Add test for GetRequestedResourceAndDo
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmclean committed Nov 29, 2023
1 parent 584a568 commit 4d1bf53
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
13 changes: 7 additions & 6 deletions babyapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestBabyAPI(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
api := babyapi.NewAPI[*Album]("Albums", "/albums", func() *Album { return &Album{} })
api.AddCustomRoute(chi.Route{
Pattern: "/action",
Pattern: "/teapot",
Handlers: map[string]http.Handler{
http.MethodGet: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusTeapot)
Expand All @@ -61,10 +61,11 @@ func TestBabyAPI(t *testing.T) {
})

api.AddCustomIDRoute(chi.Route{
Pattern: "/action",
Pattern: "/teapot",
Handlers: map[string]http.Handler{
http.MethodGet: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusTeapot)
http.MethodGet: api.GetRequestedResourceAndDo(func(r *http.Request, album *Album) (render.Renderer, *babyapi.ErrResponse) {
render.Status(r, http.StatusTeapot)
return album, nil
}),
},
})
Expand Down Expand Up @@ -93,15 +94,15 @@ func TestBabyAPI(t *testing.T) {
})

t.Run("ActionRoute", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, client.URL("")+"/action", http.NoBody)
req, err := http.NewRequest(http.MethodGet, client.URL("")+"/teapot", http.NoBody)
require.NoError(t, err)
_, err = client.MakeRequest(req, http.StatusTeapot)
require.NoError(t, err)
})

t.Run("ActionIDRoute", func(t *testing.T) {
t.Run("Successful", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, client.URL(album1.GetID())+"/action", http.NoBody)
req, err := http.NewRequest(http.MethodGet, client.URL(album1.GetID())+"/teapot", http.NoBody)
require.NoError(t, err)
_, err = client.MakeRequest(req, http.StatusTeapot)
require.NoError(t, err)
Expand Down
3 changes: 3 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ coverage:
project:
default:
threshold: 1%

ignore:
- examples

0 comments on commit 4d1bf53

Please sign in to comment.