| 169 | } |
| 170 | |
| 171 | func VerifySwaggerDefinitions(t *testing.T, router chi.Router, swaggerComments []SwaggerComment, opts ...SwaggerOption) { |
| 172 | cfg := swaggerOptions{} |
| 173 | for _, opt := range opts { |
| 174 | opt(&cfg) |
| 175 | } |
| 176 | |
| 177 | assertUniqueRoutes(t, swaggerComments) |
| 178 | assertSingleAnnotations(t, swaggerComments) |
| 179 | |
| 180 | err := chi.Walk(router, func(method, route string, _ http.Handler, _ ...func(http.Handler) http.Handler) error { |
| 181 | method = strings.ToLower(method) |
| 182 | if route != "/" && strings.HasSuffix(route, "/") { |
| 183 | route = route[:len(route)-1] |
| 184 | } |
| 185 | |
| 186 | // chi.Walk yields routes relative to the router that |
| 187 | // VerifySwaggerDefinitions was called with. Prepend the configured |
| 188 | // mount prefix so routes match the absolute paths used in @Router |
| 189 | // annotations. |
| 190 | if cfg.routePrefix != "" { |
| 191 | if route == "/" { |
| 192 | route = cfg.routePrefix + "/" |
| 193 | } else { |
| 194 | route = cfg.routePrefix + route |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | t.Run(method+" "+route, func(t *testing.T) { |
| 199 | t.Parallel() |
| 200 | |
| 201 | // Wildcard routes break the swaggo parser, so we do not document |
| 202 | // them. |
| 203 | if strings.HasSuffix(route, "/*") { |
| 204 | return |
| 205 | } |
| 206 | if isExperimentalEndpoint(route) { |
| 207 | return |
| 208 | } |
| 209 | |
| 210 | c := findSwaggerCommentByMethodAndRoute(swaggerComments, method, route) |
| 211 | assert.NotNil(t, c, "Missing @Router annotation") |
| 212 | if c == nil { |
| 213 | return // do not fail next assertion for this route |
| 214 | } |
| 215 | |
| 216 | assertConsistencyBetweenRouteIDAndSummary(t, *c) |
| 217 | assertSuccessOrFailureDefined(t, *c) |
| 218 | assertRequiredAnnotations(t, *c) |
| 219 | assertGoCommentFirst(t, *c) |
| 220 | assertPathParametersDefined(t, *c) |
| 221 | assertSecurityDefined(t, *c) |
| 222 | assertAccept(t, *c) |
| 223 | assertProduce(t, *c) |
| 224 | }) |
| 225 | return nil |
| 226 | }) |
| 227 | require.NoError(t, err, "chi.Walk should not fail") |
| 228 | } |