From 583048024aedd576cf750b7af62e60221117e761 Mon Sep 17 00:00:00 2001 From: Reindert Vetter Date: Mon, 14 Feb 2022 22:48:14 +0100 Subject: [PATCH 1/2] route:list Add HEAD method to GET route --- console/route_list.go | 13 ++++++++- test/console/route_list_test.go | 50 ++++++++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/console/route_list.go b/console/route_list.go index c5e8e13..e354650 100644 --- a/console/route_list.go +++ b/console/route_list.go @@ -43,12 +43,13 @@ func (e RouteList) Handle(c inter.Cli) inter.ExitCode { controller := route.Controller() controllerName := runtime.FuncForPC(reflect.ValueOf(controller).Pointer()).Name() + // Skip the route with HEAD method. Because it is already combined with the GET route. if route.Method() == http.MethodHead { continue } t.AppendRow(table.Row{ - route.Method(), + getRouteMethods(route), getCleanRouteUri(route), controllerName, route.Name(), @@ -60,6 +61,16 @@ func (e RouteList) Handle(c inter.Cli) inter.ExitCode { return inter.Success } +func getRouteMethods(route inter.Route) string { + method := route.Method() + + if method == http.MethodGet { + method = method + "|" + http.MethodHead + } + + return method +} + func getCleanRouteUri(route inter.Route) string { uri := route.Uri() diff --git a/test/console/route_list_test.go b/test/console/route_list_test.go index 0188ba3..ab99aca 100644 --- a/test/console/route_list_test.go +++ b/test/console/route_list_test.go @@ -29,13 +29,12 @@ func Test_route_list_without_routes_errors(t *testing.T) { func storeUsers(_ inter.Request) inter.Response { return nil } -func Test_route_list_shows_routes(t *testing.T) { +func Test_route_list_show_one_route(t *testing.T) { writer, app := setUp() var writerErr bytes.Buffer app.Bind("config.App.OsArgs", []interface{}{"/main", "route:list"}) app.Bind("routes", routing.NewRouteCollection(routing.Group( - routing.Get("/", func(request inter.Request) inter.Response { return nil }), routing.Post("/users", storeUsers).Name("users.store"), ).Prefix("/api"))) @@ -49,7 +48,50 @@ func Test_route_list_shows_routes(t *testing.T) { require.Equal(t, inter.Success, code) result := TrimDoubleSpaces(writer.String()) require.Contains(t, result, "\x1b[33mMETHOD\x1b[0m \x1b[33mURI\x1b[0m \x1b[33mCONTROLLER\x1b[0m \x1b[33mNAME\x1b[0m") - require.Contains(t, result, "GET /api github.com/confetti-framework/foundation/test/console.Test_route_list_shows_routes.func1") - require.Contains(t, result, "HEAD /api github.com/confetti-framework/foundation/test/console.Test_route_list_shows_routes.func1") require.Contains(t, result, "POST /api/users github.com/confetti-framework/foundation/test/console.storeUsers users.store") } + +func Test_route_list_shows_multiple_routes(t *testing.T) { + writer, app := setUp() + var writerErr bytes.Buffer + + app.Bind("config.App.OsArgs", []interface{}{"/main", "route:list"}) + app.Bind("routes", routing.NewRouteCollection(routing.Group( + routing.Patch("/", func(request inter.Request) inter.Response { return nil }), + routing.Post("/users", storeUsers).Name("users.store"), + ).Prefix("/api"))) + + code := console.Kernel{ + App: app, + Writer: &writer, + WriterErr: &writerErr, + Commands: []inter.Command{console.RouteList{}}, + }.Handle() + + require.Equal(t, inter.Success, code) + result := TrimDoubleSpaces(writer.String()) + require.Contains(t, result, "\x1b[33mMETHOD\x1b[0m \x1b[33mURI\x1b[0m \x1b[33mCONTROLLER\x1b[0m \x1b[33mNAME\x1b[0m") + require.Contains(t, result, "PATCH /api github.com/confetti-framework/foundation/test/console.Test_route_list_shows_multiple_routes.func1") + require.Contains(t, result, "POST /api/users github.com/confetti-framework/foundation/test/console.storeUsers users.store") +} + +func Test_route_list_with_head(t *testing.T) { + writer, app := setUp() + var writerErr bytes.Buffer + + app.Bind("config.App.OsArgs", []interface{}{"/main", "route:list"}) + app.Bind("routes", routing.NewRouteCollection(routing.Group( + routing.Get("/users", storeUsers).Name("users.store"), + ).Prefix("/api"))) + + code := console.Kernel{ + App: app, + Writer: &writer, + WriterErr: &writerErr, + Commands: []inter.Command{console.RouteList{}}, + }.Handle() + + require.Equal(t, inter.Success, code) + result := TrimDoubleSpaces(writer.String()) + require.Contains(t, result, "GET|HEAD") +} From bf281f2fb6dfdd37c0bbfd0fc327bc37f4fefa26 Mon Sep 17 00:00:00 2001 From: Reindert Vetter Date: Mon, 14 Feb 2022 22:50:32 +0100 Subject: [PATCH 2/2] route:list Add HEAD method to GET route --- console/route_list.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/console/route_list.go b/console/route_list.go index e354650..b023df9 100644 --- a/console/route_list.go +++ b/console/route_list.go @@ -65,7 +65,7 @@ func getRouteMethods(route inter.Route) string { method := route.Method() if method == http.MethodGet { - method = method + "|" + http.MethodHead + method += "|" + http.MethodHead } return method