Skip to content
This repository has been archived by the owner on May 23, 2022. It is now read-only.

Commit

Permalink
route:list Add HEAD method to GET route
Browse files Browse the repository at this point in the history
  • Loading branch information
reindert-vetter committed Feb 14, 2022
1 parent 3d2c209 commit 5830480
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
13 changes: 12 additions & 1 deletion console/route_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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()

Expand Down
50 changes: 46 additions & 4 deletions test/console/route_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")))

Expand All @@ -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")
}

0 comments on commit 5830480

Please # to comment.