mirror of
https://github.com/labstack/echo.git
synced 2025-01-12 01:22:21 +02:00
Better tests for context.go
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
9b6c392829
commit
b526a0df5b
12
context.go
12
context.go
@ -116,6 +116,11 @@ func (c *Context) NoContent(code int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Redirect redirects the request using http.Redirect with status code.
|
||||
func (c *Context) Redirect(code int, url string) {
|
||||
http.Redirect(c.response, c.request, url, code)
|
||||
}
|
||||
|
||||
// Error invokes the registered HTTP error handler. Usually used by middleware.
|
||||
func (c *Context) Error(err error) {
|
||||
c.echo.httpErrorHandler(err, c)
|
||||
@ -131,12 +136,7 @@ func (c *Context) Set(key string, val interface{}) {
|
||||
c.store[key] = val
|
||||
}
|
||||
|
||||
// Redirect redirects the request using http.Redirect with status code.
|
||||
func (c *Context) Redirect(code int, url string) {
|
||||
http.Redirect(c.response, c.request, url, code)
|
||||
}
|
||||
|
||||
func (c *Context) reset(w http.ResponseWriter, r *http.Request, e *Echo) {
|
||||
func (c *Context) reset(r *http.Request, w http.ResponseWriter, e *Echo) {
|
||||
c.request = r
|
||||
c.response.reset(w)
|
||||
c.echo = e
|
||||
|
@ -3,11 +3,14 @@ package echo
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"text/template"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type (
|
||||
@ -25,6 +28,15 @@ func TestContext(t *testing.T) {
|
||||
r, _ := http.NewRequest(POST, "/users/1", bytes.NewReader(b))
|
||||
c := NewContext(r, NewResponse(httptest.NewRecorder()), New())
|
||||
|
||||
// Request
|
||||
assert.NotEmpty(t, c.Request())
|
||||
|
||||
// Response
|
||||
assert.NotEmpty(t, c.Response())
|
||||
|
||||
// Socket
|
||||
assert.Nil(t, c.Socket())
|
||||
|
||||
//------
|
||||
// Bind
|
||||
//------
|
||||
@ -110,7 +122,19 @@ func TestContext(t *testing.T) {
|
||||
t.Errorf("html %v", he.Error)
|
||||
}
|
||||
|
||||
// NoContent
|
||||
c.NoContent(http.StatusOK)
|
||||
assert.Equal(t, http.StatusOK, c.response.status)
|
||||
|
||||
// Redirect
|
||||
c.response.committed = false
|
||||
c.Redirect(http.StatusMovedPermanently, "http://labstack.github.io/echo")
|
||||
|
||||
// Error
|
||||
c.response.committed = false
|
||||
c.Error(errors.New("error"))
|
||||
assert.Equal(t, http.StatusInternalServerError, c.response.status)
|
||||
|
||||
// reset
|
||||
c.reset(r, NewResponse(httptest.NewRecorder()), New())
|
||||
}
|
||||
|
2
echo.go
2
echo.go
@ -392,7 +392,7 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if echo != nil {
|
||||
e = echo
|
||||
}
|
||||
c.reset(w, r, e)
|
||||
c.reset(r, w, e)
|
||||
if h == nil {
|
||||
h = e.notFoundHandler
|
||||
}
|
||||
|
@ -308,7 +308,7 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, echo *E
|
||||
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
c := r.echo.pool.Get().(*Context)
|
||||
h, _ := r.Find(req.Method, req.URL.Path, c)
|
||||
c.reset(w, req, r.echo)
|
||||
c.reset(req, w, r.echo)
|
||||
if h == nil {
|
||||
h = r.echo.notFoundHandler
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user