Added Redirect function
This commit is contained in:
parent
f0cb179b8b
commit
818e0054c1
@ -9,6 +9,7 @@ type Context interface {
|
|||||||
Bytes([]byte) error
|
Bytes([]byte) error
|
||||||
Error(...any) error
|
Error(...any) error
|
||||||
Next() error
|
Next() error
|
||||||
|
Redirect(int, string) error
|
||||||
Request() Request
|
Request() Request
|
||||||
Response() Response
|
Response() Response
|
||||||
Status(int) Context
|
Status(int) Context
|
||||||
@ -51,6 +52,14 @@ func (ctx *context) Next() error {
|
|||||||
return ctx.server.handlers[ctx.handlerCount](ctx)
|
return ctx.server.handlers[ctx.handlerCount](ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Redirect redirects the client to a different location
|
||||||
|
// with the specified status code.
|
||||||
|
func (ctx *context) Redirect(status int, location string) error {
|
||||||
|
ctx.response.SetStatus(status)
|
||||||
|
ctx.response.SetHeader("Location", location)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Request returns the HTTP request.
|
// Request returns the HTTP request.
|
||||||
func (ctx *context) Request() Request {
|
func (ctx *context) Request() Request {
|
||||||
return &ctx.request
|
return &ctx.request
|
||||||
|
@ -55,3 +55,15 @@ func TestErrorMultiple(t *testing.T) {
|
|||||||
assert.Equal(t, response.Status(), 401)
|
assert.Equal(t, response.Status(), 401)
|
||||||
assert.Equal(t, string(response.Body()), "")
|
assert.Equal(t, string(response.Body()), "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRedirect(t *testing.T) {
|
||||||
|
s := web.NewServer()
|
||||||
|
|
||||||
|
s.Get("/", func(ctx web.Context) error {
|
||||||
|
return ctx.Redirect(301, "/target")
|
||||||
|
})
|
||||||
|
|
||||||
|
response := s.Request("GET", "/", nil, nil)
|
||||||
|
assert.Equal(t, response.Status(), 301)
|
||||||
|
assert.Equal(t, response.Header("Location"), "/target")
|
||||||
|
}
|
||||||
|
@ -44,6 +44,7 @@ PASS: TestBytes
|
|||||||
PASS: TestString
|
PASS: TestString
|
||||||
PASS: TestError
|
PASS: TestError
|
||||||
PASS: TestErrorMultiple
|
PASS: TestErrorMultiple
|
||||||
|
PASS: TestRedirect
|
||||||
PASS: TestRequest
|
PASS: TestRequest
|
||||||
PASS: TestRequestHeader
|
PASS: TestRequestHeader
|
||||||
PASS: TestRequestParam
|
PASS: TestRequestParam
|
||||||
|
Loading…
Reference in New Issue
Block a user