mirror of
https://github.com/labstack/echo.git
synced 2025-02-03 13:11:39 +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
|
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.
|
// Error invokes the registered HTTP error handler. Usually used by middleware.
|
||||||
func (c *Context) Error(err error) {
|
func (c *Context) Error(err error) {
|
||||||
c.echo.httpErrorHandler(err, c)
|
c.echo.httpErrorHandler(err, c)
|
||||||
@ -131,12 +136,7 @@ func (c *Context) Set(key string, val interface{}) {
|
|||||||
c.store[key] = val
|
c.store[key] = val
|
||||||
}
|
}
|
||||||
|
|
||||||
// Redirect redirects the request using http.Redirect with status code.
|
func (c *Context) reset(r *http.Request, w http.ResponseWriter, e *Echo) {
|
||||||
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) {
|
|
||||||
c.request = r
|
c.request = r
|
||||||
c.response.reset(w)
|
c.response.reset(w)
|
||||||
c.echo = e
|
c.echo = e
|
||||||
|
@ -3,11 +3,14 @@ package echo
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@ -25,6 +28,15 @@ func TestContext(t *testing.T) {
|
|||||||
r, _ := http.NewRequest(POST, "/users/1", bytes.NewReader(b))
|
r, _ := http.NewRequest(POST, "/users/1", bytes.NewReader(b))
|
||||||
c := NewContext(r, NewResponse(httptest.NewRecorder()), New())
|
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
|
// Bind
|
||||||
//------
|
//------
|
||||||
@ -110,7 +122,19 @@ func TestContext(t *testing.T) {
|
|||||||
t.Errorf("html %v", he.Error)
|
t.Errorf("html %v", he.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NoContent
|
||||||
|
c.NoContent(http.StatusOK)
|
||||||
|
assert.Equal(t, http.StatusOK, c.response.status)
|
||||||
|
|
||||||
// Redirect
|
// Redirect
|
||||||
c.response.committed = false
|
c.response.committed = false
|
||||||
c.Redirect(http.StatusMovedPermanently, "http://labstack.github.io/echo")
|
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 {
|
if echo != nil {
|
||||||
e = echo
|
e = echo
|
||||||
}
|
}
|
||||||
c.reset(w, r, e)
|
c.reset(r, w, e)
|
||||||
if h == nil {
|
if h == nil {
|
||||||
h = e.notFoundHandler
|
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) {
|
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
c := r.echo.pool.Get().(*Context)
|
c := r.echo.pool.Get().(*Context)
|
||||||
h, _ := r.Find(req.Method, req.URL.Path, c)
|
h, _ := r.Find(req.Method, req.URL.Path, c)
|
||||||
c.reset(w, req, r.echo)
|
c.reset(req, w, r.echo)
|
||||||
if h == nil {
|
if h == nil {
|
||||||
h = r.echo.notFoundHandler
|
h = r.echo.notFoundHandler
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user