Skip to content

Commit 440fce7

Browse files
docs: add example ctx
1 parent 46293fc commit 440fce7

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

ctx_example_test.go

+65
Original file line numberDiff line numberDiff line change
@@ -960,3 +960,68 @@ func ExampleCtx_QueryParam() {
960960
// Output:
961961
// quick
962962
}
963+
964+
// This function is named ExampleCtx_Redirect()
965+
// it with the Examples type.
966+
func ExampleCtx_Redirect() {
967+
q := New()
968+
969+
q.Get("/old-path", func(c *Ctx) error {
970+
return c.Redirect("/new-path")
971+
})
972+
973+
// Simulate a GET request
974+
res, _ := q.Qtest(QuickTestOptions{
975+
Method: MethodGet,
976+
URI: "/old-path",
977+
})
978+
979+
if err := res.AssertStatus(StatusFound); err != nil {
980+
fmt.Println("Status error:", err)
981+
}
982+
983+
fmt.Println("Location header:", res.Response().Header.Get("Location"))
984+
fmt.Println("Status:", res.StatusCode())
985+
fmt.Println("Body:", res.BodyStr())
986+
987+
// Output:
988+
// Location header: /new-path
989+
// Status: 302
990+
// Body: Redirecting to /new-path
991+
}
992+
993+
// This function is named ExampleCtx_Next()
994+
// it with the Examples type.
995+
func ExampleCtx_Next() {
996+
q := New()
997+
998+
// Middleware that uses Next
999+
q.Use(func(c *Ctx) error {
1000+
err := c.Next()
1001+
return err
1002+
})
1003+
1004+
// Final route handler
1005+
q.Get("/", func(c *Ctx) error {
1006+
fmt.Println("Quick")
1007+
return c.String("done")
1008+
})
1009+
1010+
// Simulate request
1011+
res, _ := q.Qtest(QuickTestOptions{
1012+
Method: MethodGet,
1013+
URI: "/",
1014+
})
1015+
1016+
// Ensure response is what we expect
1017+
if err := res.AssertString("done"); err != nil {
1018+
fmt.Println("Body error:", err)
1019+
}
1020+
1021+
if err := res.AssertStatus(200); err != nil {
1022+
fmt.Println("Status error:", err)
1023+
}
1024+
1025+
// Output:
1026+
// Quick
1027+
}

0 commit comments

Comments
 (0)