Added redirect method
This commit is contained in:
parent
625bad898b
commit
1803bf7108
@ -16,6 +16,7 @@ type Context interface {
|
|||||||
File(string) error
|
File(string) error
|
||||||
Get(string) string
|
Get(string) string
|
||||||
Next() error
|
Next() error
|
||||||
|
Redirect(int, string) error
|
||||||
Request() Request
|
Request() Request
|
||||||
Response() Response
|
Response() Response
|
||||||
Status(int) Context
|
Status(int) Context
|
||||||
@ -94,6 +95,13 @@ func (ctx *ctx) Response() Response {
|
|||||||
return &ctx.response
|
return &ctx.response
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Redirect sets the Location header and writes the headers with the given status code.
|
||||||
|
func (ctx *ctx) Redirect(code int, url string) error {
|
||||||
|
ctx.Response().SetHeader("Location", url)
|
||||||
|
ctx.Status(code)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Status sets the HTTP status of the response.
|
// Status sets the HTTP status of the response.
|
||||||
func (ctx *ctx) Status(status int) Context {
|
func (ctx *ctx) Status(status int) Context {
|
||||||
ctx.response.WriteHeader(status)
|
ctx.response.WriteHeader(status)
|
||||||
|
@ -60,6 +60,10 @@ func TestRouter(t *testing.T) {
|
|||||||
return ctx.Request().Context().Err()
|
return ctx.Request().Context().Err()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
s.Get("/redirect", func(ctx server.Context) error {
|
||||||
|
return ctx.Redirect(http.StatusTemporaryRedirect, "/")
|
||||||
|
})
|
||||||
|
|
||||||
s.Get("/request/data", func(ctx server.Context) error {
|
s.Get("/request/data", func(ctx server.Context) error {
|
||||||
request := ctx.Request()
|
request := ctx.Request()
|
||||||
method := request.Method()
|
method := request.Method()
|
||||||
@ -124,6 +128,7 @@ func TestRouter(t *testing.T) {
|
|||||||
{Method: "GET", URL: "/request/header", Body: "", Status: http.StatusOK, Response: ""},
|
{Method: "GET", URL: "/request/header", Body: "", Status: http.StatusOK, Response: ""},
|
||||||
{Method: "GET", URL: "/response/header", Body: "", Status: http.StatusOK, Response: "text/plain"},
|
{Method: "GET", URL: "/response/header", Body: "", Status: http.StatusOK, Response: "text/plain"},
|
||||||
{Method: "GET", URL: "/reader", Body: "", Status: http.StatusOK, Response: "Hello"},
|
{Method: "GET", URL: "/reader", Body: "", Status: http.StatusOK, Response: "Hello"},
|
||||||
|
{Method: "GET", URL: "/redirect", Body: "", Status: http.StatusTemporaryRedirect, Response: ""},
|
||||||
{Method: "GET", URL: "/string", Body: "", Status: http.StatusOK, Response: "Hello"},
|
{Method: "GET", URL: "/string", Body: "", Status: http.StatusOK, Response: "Hello"},
|
||||||
{Method: "GET", URL: "/scheme", Body: "", Status: http.StatusOK, Response: "http"},
|
{Method: "GET", URL: "/scheme", Body: "", Status: http.StatusOK, Response: "http"},
|
||||||
{Method: "GET", URL: "/write", Body: "", Status: http.StatusOK, Response: "Hello"},
|
{Method: "GET", URL: "/write", Body: "", Status: http.StatusOK, Response: "Hello"},
|
||||||
|
Loading…
Reference in New Issue
Block a user