Skip to content

Commit cc66f4b

Browse files
Copilotgaby
andauthored
test(domain): add OpenAPI helper coverage for domainRouter
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/29ec80db-3357-467f-a834-93f162ec1e3a Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
1 parent ff3e3ea commit cc66f4b

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

domain_test.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,137 @@ func Test_Domain_WithMiddleware(t *testing.T) {
243243
require.Empty(t, resp.Header.Get("X-Domain"))
244244
}
245245

246+
func Test_Domain_OpenAPI_Helpers(t *testing.T) {
247+
t.Parallel()
248+
249+
t.Run("Summary", func(t *testing.T) {
250+
t.Parallel()
251+
app := New()
252+
domain := app.Domain("api.example.com")
253+
domain.Get("/users", testEmptyHandler).Summary("sum")
254+
route := app.stack[app.methodInt(MethodGet)][0]
255+
require.Equal(t, "sum", route.Summary)
256+
})
257+
258+
t.Run("Description", func(t *testing.T) {
259+
t.Parallel()
260+
app := New()
261+
domain := app.Domain("api.example.com")
262+
domain.Get("/users", testEmptyHandler).Description("desc")
263+
route := app.stack[app.methodInt(MethodGet)][0]
264+
require.Equal(t, "desc", route.Description)
265+
})
266+
267+
t.Run("Consumes", func(t *testing.T) {
268+
t.Parallel()
269+
app := New()
270+
domain := app.Domain("api.example.com")
271+
domain.Get("/users", testEmptyHandler).Consumes(MIMEApplicationJSON)
272+
route := app.stack[app.methodInt(MethodGet)][0]
273+
//nolint:testifylint // MIMEApplicationJSON is a plain string, JSONEq not required
274+
require.Equal(t, MIMEApplicationJSON, route.Consumes)
275+
})
276+
277+
t.Run("Produces", func(t *testing.T) {
278+
t.Parallel()
279+
app := New()
280+
domain := app.Domain("api.example.com")
281+
domain.Get("/users", testEmptyHandler).Produces(MIMEApplicationXML)
282+
route := app.stack[app.methodInt(MethodGet)][0]
283+
require.Equal(t, MIMEApplicationXML, route.Produces)
284+
})
285+
286+
t.Run("RequestBody", func(t *testing.T) {
287+
t.Parallel()
288+
app := New()
289+
domain := app.Domain("api.example.com")
290+
domain.Post("/users", testEmptyHandler).RequestBody("User", true, MIMEApplicationJSON)
291+
route := app.stack[app.methodInt(MethodPost)][0]
292+
require.NotNil(t, route.RequestBody)
293+
require.Equal(t, []string{MIMEApplicationJSON}, route.RequestBody.MediaTypes)
294+
})
295+
296+
t.Run("RequestBodyWithExample", func(t *testing.T) {
297+
t.Parallel()
298+
app := New()
299+
domain := app.Domain("api.example.com")
300+
domain.Post("/users", testEmptyHandler).
301+
RequestBodyWithExample("User", true, map[string]any{"type": "object"}, "#/components/schemas/User", map[string]any{"name": "doe"}, map[string]any{"sample": map[string]any{"name": "john"}}, MIMEApplicationJSON)
302+
route := app.stack[app.methodInt(MethodPost)][0]
303+
require.NotNil(t, route.RequestBody)
304+
require.Equal(t, "#/components/schemas/User", route.RequestBody.SchemaRef)
305+
require.Equal(t, map[string]any{"$ref": "#/components/schemas/User"}, route.RequestBody.Schema)
306+
require.Equal(t, map[string]any{"name": "doe"}, route.RequestBody.Example)
307+
})
308+
309+
t.Run("Parameter", func(t *testing.T) {
310+
t.Parallel()
311+
app := New()
312+
domain := app.Domain("api.example.com")
313+
domain.Get("/users/:id", testEmptyHandler).Parameter("id", "path", false, map[string]any{"type": "integer"}, "identifier")
314+
route := app.stack[app.methodInt(MethodGet)][0]
315+
require.Len(t, route.Parameters, 1)
316+
require.Equal(t, "id", route.Parameters[0].Name)
317+
require.True(t, route.Parameters[0].Required)
318+
require.Equal(t, "integer", route.Parameters[0].Schema["type"])
319+
})
320+
321+
t.Run("ParameterWithExample", func(t *testing.T) {
322+
t.Parallel()
323+
app := New()
324+
domain := app.Domain("api.example.com")
325+
domain.Get("/users/:id", testEmptyHandler).
326+
ParameterWithExample("id", "path", false, nil, "#/components/schemas/ID", "identifier", "123", map[string]any{"sample": "value"})
327+
route := app.stack[app.methodInt(MethodGet)][0]
328+
require.Len(t, route.Parameters, 1)
329+
require.Equal(t, "#/components/schemas/ID", route.Parameters[0].SchemaRef)
330+
require.Equal(t, "123", route.Parameters[0].Example)
331+
require.Equal(t, map[string]any{"sample": "value"}, route.Parameters[0].Examples)
332+
})
333+
334+
t.Run("Response", func(t *testing.T) {
335+
t.Parallel()
336+
app := New()
337+
domain := app.Domain("api.example.com")
338+
domain.Get("/users", testEmptyHandler).Response(StatusCreated, "Created", MIMEApplicationJSON)
339+
route := app.stack[app.methodInt(MethodGet)][0]
340+
require.Contains(t, route.Responses, "201")
341+
require.Equal(t, []string{MIMEApplicationJSON}, route.Responses["201"].MediaTypes)
342+
})
343+
344+
t.Run("ResponseWithExample", func(t *testing.T) {
345+
t.Parallel()
346+
app := New()
347+
domain := app.Domain("api.example.com")
348+
domain.Get("/users", testEmptyHandler).
349+
ResponseWithExample(StatusCreated, "Created", nil, "#/components/schemas/User", map[string]any{"id": 1}, map[string]any{"sample": map[string]any{"id": 2}}, MIMEApplicationJSON)
350+
route := app.stack[app.methodInt(MethodGet)][0]
351+
resp := route.Responses["201"]
352+
require.Equal(t, "#/components/schemas/User", resp.SchemaRef)
353+
require.Equal(t, map[string]any{"$ref": "#/components/schemas/User"}, resp.Schema)
354+
require.Equal(t, map[string]any{"id": 1}, resp.Example)
355+
require.Equal(t, map[string]any{"sample": map[string]any{"id": 2}}, resp.Examples)
356+
})
357+
358+
t.Run("Tags", func(t *testing.T) {
359+
t.Parallel()
360+
app := New()
361+
domain := app.Domain("api.example.com")
362+
domain.Get("/users", testEmptyHandler).Tags("foo", "bar")
363+
route := app.stack[app.methodInt(MethodGet)][0]
364+
require.Equal(t, []string{"foo", "bar"}, route.Tags)
365+
})
366+
367+
t.Run("Deprecated", func(t *testing.T) {
368+
t.Parallel()
369+
app := New()
370+
domain := app.Domain("api.example.com")
371+
domain.Get("/users", testEmptyHandler).Deprecated()
372+
route := app.stack[app.methodInt(MethodGet)][0]
373+
require.True(t, route.Deprecated)
374+
})
375+
}
376+
246377
func Test_Domain_HTTPMethods(t *testing.T) {
247378
t.Parallel()
248379

0 commit comments

Comments
 (0)