MCPcopy
hub / github.com/labstack/echo / TestEcho_OnAddRoute

Function TestEcho_OnAddRoute

echo_test.go:1141–1206  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1139}
1140
1141func TestEcho_OnAddRoute(t *testing.T) {
1142 exampleRoute := Route{
1143 Method: http.MethodGet,
1144 Path: "/api/files/:id",
1145 Handler: notFoundHandler,
1146 Middlewares: nil,
1147 Name: "x",
1148 }
1149
1150 var testCases = []struct {
1151 whenRoute Route
1152 whenError error
1153 name string
1154 expectError string
1155 expectAdded []string
1156 expectLen int
1157 }{
1158 {
1159 name: "ok",
1160 whenRoute: exampleRoute,
1161 whenError: nil,
1162 expectAdded: []string{"/static", "/api/files/:id"},
1163 expectError: "",
1164 expectLen: 2,
1165 },
1166 {
1167 name: "nok, error is returned",
1168 whenRoute: exampleRoute,
1169 whenError: errors.New("nope"),
1170 expectAdded: []string{"/static"},
1171 expectError: "nope",
1172 expectLen: 1,
1173 },
1174 }
1175 for _, tc := range testCases {
1176 t.Run(tc.name, func(t *testing.T) {
1177
1178 e := New()
1179
1180 added := make([]string, 0)
1181 cnt := 0
1182 e.OnAddRoute = func(route Route) error {
1183 if cnt > 0 && tc.whenError != nil { // we want to GET /static to succeed for nok tests
1184 return tc.whenError
1185 }
1186 cnt++
1187 added = append(added, route.Path)
1188 return nil
1189 }
1190
1191 e.GET("/static", notFoundHandler)
1192
1193 var err error
1194 _, err = e.AddRoute(tc.whenRoute)
1195
1196 if tc.expectError != "" {
1197 assert.EqualError(t, err, tc.expectError)
1198 } else {

Callers

nothing calls this directly

Calls 5

NewFunction · 0.85
RouterMethod · 0.80
RoutesMethod · 0.65
GETMethod · 0.45
AddRouteMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…