mirror of
https://github.com/labstack/echo.git
synced 2025-04-17 12:06:44 +02:00
Test coverage for cookie.go and context.go
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
acc91b28f3
commit
d0ed5830c4
16
context.go
16
context.go
@ -180,7 +180,7 @@ type (
|
|||||||
}
|
}
|
||||||
|
|
||||||
echoContext struct {
|
echoContext struct {
|
||||||
context context.Context
|
context.Context
|
||||||
request engine.Request
|
request engine.Request
|
||||||
response engine.Response
|
response engine.Response
|
||||||
path string
|
path string
|
||||||
@ -199,27 +199,27 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (c *echoContext) StdContext() context.Context {
|
func (c *echoContext) StdContext() context.Context {
|
||||||
return c.context
|
return c.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *echoContext) SetStdContext(ctx context.Context) {
|
func (c *echoContext) SetStdContext(ctx context.Context) {
|
||||||
c.context = ctx
|
c.Context = ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *echoContext) Deadline() (deadline time.Time, ok bool) {
|
func (c *echoContext) Deadline() (deadline time.Time, ok bool) {
|
||||||
return c.context.Deadline()
|
return c.Context.Deadline()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *echoContext) Done() <-chan struct{} {
|
func (c *echoContext) Done() <-chan struct{} {
|
||||||
return c.context.Done()
|
return c.Context.Done()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *echoContext) Err() error {
|
func (c *echoContext) Err() error {
|
||||||
return c.context.Err()
|
return c.Context.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *echoContext) Value(key interface{}) interface{} {
|
func (c *echoContext) Value(key interface{}) interface{} {
|
||||||
return c.context.Value(key)
|
return c.Context.Value(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *echoContext) Request() engine.Request {
|
func (c *echoContext) Request() engine.Request {
|
||||||
@ -508,7 +508,7 @@ func ContentTypeByExtension(name string) (t string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *echoContext) Reset(req engine.Request, res engine.Response) {
|
func (c *echoContext) Reset(req engine.Request, res engine.Response) {
|
||||||
c.context = nil
|
c.Context = nil
|
||||||
c.request = req
|
c.request = req
|
||||||
c.response = res
|
c.response = res
|
||||||
c.store = nil
|
c.store = nil
|
||||||
|
163
context_test.go
163
context_test.go
@ -4,12 +4,15 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"net/url"
|
"net/url"
|
||||||
@ -36,27 +39,17 @@ func TestContext(t *testing.T) {
|
|||||||
rec := test.NewResponseRecorder()
|
rec := test.NewResponseRecorder()
|
||||||
c := e.NewContext(req, rec).(*echoContext)
|
c := e.NewContext(req, rec).(*echoContext)
|
||||||
|
|
||||||
|
// Echo
|
||||||
|
assert.Equal(t, e, c.Echo())
|
||||||
|
|
||||||
// Request
|
// Request
|
||||||
assert.NotNil(t, c.Request())
|
assert.Equal(t, req, c.Request())
|
||||||
|
|
||||||
// Response
|
// Response
|
||||||
assert.NotNil(t, c.Response())
|
assert.Equal(t, rec, c.Response())
|
||||||
|
|
||||||
// ParamNames
|
// Logger
|
||||||
c.pnames = []string{"uid", "fid"}
|
assert.Equal(t, e.logger, c.Logger())
|
||||||
assert.EqualValues(t, []string{"uid", "fid"}, c.ParamNames())
|
|
||||||
|
|
||||||
// Param by id
|
|
||||||
c.pnames = []string{"id"}
|
|
||||||
c.pvalues = []string{"1"}
|
|
||||||
assert.Equal(t, "1", c.P(0))
|
|
||||||
|
|
||||||
// Param by name
|
|
||||||
assert.Equal(t, "1", c.Param("id"))
|
|
||||||
|
|
||||||
// Store
|
|
||||||
c.Set("user", "Jon Snow")
|
|
||||||
assert.Equal(t, "Jon Snow", c.Get("user"))
|
|
||||||
|
|
||||||
//--------
|
//--------
|
||||||
// Render
|
// Render
|
||||||
@ -158,13 +151,6 @@ func TestContext(t *testing.T) {
|
|||||||
c.NoContent(http.StatusOK)
|
c.NoContent(http.StatusOK)
|
||||||
assert.Equal(t, http.StatusOK, rec.Status())
|
assert.Equal(t, http.StatusOK, rec.Status())
|
||||||
|
|
||||||
// Redirect
|
|
||||||
rec = test.NewResponseRecorder()
|
|
||||||
c = e.NewContext(req, rec).(*echoContext)
|
|
||||||
assert.Equal(t, nil, c.Redirect(http.StatusMovedPermanently, "http://labstack.github.io/echo"))
|
|
||||||
assert.Equal(t, http.StatusMovedPermanently, rec.Status())
|
|
||||||
assert.Equal(t, "http://labstack.github.io/echo", rec.Header().Get(HeaderLocation))
|
|
||||||
|
|
||||||
// Error
|
// Error
|
||||||
rec = test.NewResponseRecorder()
|
rec = test.NewResponseRecorder()
|
||||||
c = e.NewContext(req, rec).(*echoContext)
|
c = e.NewContext(req, rec).(*echoContext)
|
||||||
@ -203,7 +189,7 @@ func TestContextCookie(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Write
|
// Write
|
||||||
cookie = &test.Cookie{&http.Cookie{
|
cookie = &test.Cookie{Cookie: &http.Cookie{
|
||||||
Name: "SSID",
|
Name: "SSID",
|
||||||
Value: "Ap4PGTEq",
|
Value: "Ap4PGTEq",
|
||||||
Domain: "labstack.com",
|
Domain: "labstack.com",
|
||||||
@ -235,35 +221,130 @@ func TestContextPath(t *testing.T) {
|
|||||||
assert.Equal(t, "/users/:uid/files/:fid", c.Path())
|
assert.Equal(t, "/users/:uid/files/:fid", c.Path())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContextQueryParam(t *testing.T) {
|
func TestContextPathParam(t *testing.T) {
|
||||||
q := make(url.Values)
|
|
||||||
q.Set("name", "joe")
|
|
||||||
q.Set("email", "joe@labstack.com")
|
|
||||||
req := test.NewRequest(GET, "/?"+q.Encode(), nil)
|
|
||||||
e := New()
|
e := New()
|
||||||
|
req := test.NewRequest(GET, "/", nil)
|
||||||
c := e.NewContext(req, nil)
|
c := e.NewContext(req, nil)
|
||||||
assert.Equal(t, "joe", c.QueryParam("name"))
|
|
||||||
assert.Equal(t, "joe@labstack.com", c.QueryParam("email"))
|
// ParamNames
|
||||||
|
c.SetParamNames("uid", "fid")
|
||||||
|
assert.EqualValues(t, []string{"uid", "fid"}, c.ParamNames())
|
||||||
|
|
||||||
|
// ParamValues
|
||||||
|
c.SetParamValues("101", "501")
|
||||||
|
assert.EqualValues(t, []string{"101", "501"}, c.ParamValues())
|
||||||
|
|
||||||
|
// P
|
||||||
|
assert.Equal(t, "101", c.P(0))
|
||||||
|
|
||||||
|
// Param
|
||||||
|
assert.Equal(t, "501", c.Param("fid"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContextFormValue(t *testing.T) {
|
func TestContextFormValue(t *testing.T) {
|
||||||
f := make(url.Values)
|
f := make(url.Values)
|
||||||
f.Set("name", "joe")
|
f.Set("name", "Jon Snow")
|
||||||
f.Set("email", "joe@labstack.com")
|
f.Set("email", "jon@labstack.com")
|
||||||
|
|
||||||
e := New()
|
e := New()
|
||||||
req := test.NewRequest(POST, "/", strings.NewReader(f.Encode()))
|
req := test.NewRequest(POST, "/", strings.NewReader(f.Encode()))
|
||||||
req.Header().Add(HeaderContentType, MIMEApplicationForm)
|
req.Header().Add(HeaderContentType, MIMEApplicationForm)
|
||||||
|
|
||||||
c := e.NewContext(req, nil)
|
c := e.NewContext(req, nil)
|
||||||
assert.Equal(t, "joe", c.FormValue("name"))
|
|
||||||
assert.Equal(t, "joe@labstack.com", c.FormValue("email"))
|
// FormValue
|
||||||
|
assert.Equal(t, "Jon Snow", c.FormValue("name"))
|
||||||
|
assert.Equal(t, "jon@labstack.com", c.FormValue("email"))
|
||||||
|
|
||||||
|
// FormParams
|
||||||
|
assert.Equal(t, map[string][]string{
|
||||||
|
"name": []string{"Jon Snow"},
|
||||||
|
"email": []string{"jon@labstack.com"},
|
||||||
|
}, c.FormParams())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContextNetContext(t *testing.T) {
|
func TestContextQueryParam(t *testing.T) {
|
||||||
// c := new(context)
|
q := make(url.Values)
|
||||||
// c.Context = xcontext.WithValue(nil, "key", "val")
|
q.Set("name", "Jon Snow")
|
||||||
// assert.Equal(t, "val", c.Value("key"))
|
q.Set("email", "jon@labstack.com")
|
||||||
|
req := test.NewRequest(GET, "/?"+q.Encode(), nil)
|
||||||
|
e := New()
|
||||||
|
c := e.NewContext(req, nil)
|
||||||
|
|
||||||
|
// QueryParam
|
||||||
|
assert.Equal(t, "Jon Snow", c.QueryParam("name"))
|
||||||
|
assert.Equal(t, "jon@labstack.com", c.QueryParam("email"))
|
||||||
|
|
||||||
|
// QueryParams
|
||||||
|
assert.Equal(t, map[string][]string{
|
||||||
|
"name": []string{"Jon Snow"},
|
||||||
|
"email": []string{"jon@labstack.com"},
|
||||||
|
}, c.QueryParams())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContextFormFile(t *testing.T) {
|
||||||
|
e := New()
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
mr := multipart.NewWriter(buf)
|
||||||
|
w, err := mr.CreateFormFile("file", "test")
|
||||||
|
if assert.NoError(t, err) {
|
||||||
|
w.Write([]byte("test"))
|
||||||
|
}
|
||||||
|
mr.Close()
|
||||||
|
req := test.NewRequest(POST, "/", buf)
|
||||||
|
req.Header().Set(HeaderContentType, mr.FormDataContentType())
|
||||||
|
rec := test.NewResponseRecorder()
|
||||||
|
c := e.NewContext(req, rec)
|
||||||
|
f, err := c.FormFile("file")
|
||||||
|
if assert.NoError(t, err) {
|
||||||
|
assert.Equal(t, "test", f.Filename)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContextMultipartForm(t *testing.T) {
|
||||||
|
e := New()
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
mw := multipart.NewWriter(buf)
|
||||||
|
mw.WriteField("name", "Jon Snow")
|
||||||
|
mw.Close()
|
||||||
|
req := test.NewRequest(POST, "/", buf)
|
||||||
|
req.Header().Set(HeaderContentType, mw.FormDataContentType())
|
||||||
|
rec := test.NewResponseRecorder()
|
||||||
|
c := e.NewContext(req, rec)
|
||||||
|
f, err := c.MultipartForm()
|
||||||
|
if assert.NoError(t, err) {
|
||||||
|
assert.NotNil(t, f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContextRedirect(t *testing.T) {
|
||||||
|
e := New()
|
||||||
|
req := test.NewRequest(GET, "/", nil)
|
||||||
|
rec := test.NewResponseRecorder()
|
||||||
|
c := e.NewContext(req, rec)
|
||||||
|
assert.Equal(t, nil, c.Redirect(http.StatusMovedPermanently, "http://labstack.github.io/echo"))
|
||||||
|
assert.Equal(t, http.StatusMovedPermanently, rec.Status())
|
||||||
|
assert.Equal(t, "http://labstack.github.io/echo", rec.Header().Get(HeaderLocation))
|
||||||
|
assert.Error(t, c.Redirect(310, "http://labstack.github.io/echo"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContextStdContext(t *testing.T) {
|
||||||
|
c := new(echoContext)
|
||||||
|
c.SetStdContext(context.WithValue(c.StdContext(), "key", "val"))
|
||||||
|
assert.Equal(t, "val", c.Value("key"))
|
||||||
|
ctx, _ := context.WithDeadline(context.Background(), time.Now())
|
||||||
|
c.SetStdContext(ctx)
|
||||||
|
assert.Equal(t, context.DeadlineExceeded, c.Err())
|
||||||
|
assert.NotNil(t, c.Done())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContextStore(t *testing.T) {
|
||||||
|
c := new(echoContext)
|
||||||
|
c.store = nil
|
||||||
|
c.Set("name", "Jon Snow")
|
||||||
|
assert.Equal(t, "Jon Snow", c.Get("name"))
|
||||||
|
assert.True(t, c.Contains("name"))
|
||||||
|
c.Del("name")
|
||||||
|
assert.Empty(t, c.Get("name"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContextServeContent(t *testing.T) {
|
func TestContextServeContent(t *testing.T) {
|
||||||
|
41
cookie_test.go
Normal file
41
cookie_test.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package echo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCookie(t *testing.T) {
|
||||||
|
c := new(Cookie)
|
||||||
|
|
||||||
|
// Name
|
||||||
|
c.SetName("name")
|
||||||
|
assert.Equal(t, "name", c.Name())
|
||||||
|
|
||||||
|
// Value
|
||||||
|
c.SetValue("Jon Snow")
|
||||||
|
assert.Equal(t, "Jon Snow", c.Value())
|
||||||
|
|
||||||
|
// Path
|
||||||
|
c.SetPath("/")
|
||||||
|
assert.Equal(t, "/", c.Path())
|
||||||
|
|
||||||
|
// Domain
|
||||||
|
c.SetDomain("labstack.com")
|
||||||
|
assert.Equal(t, "labstack.com", c.Domain())
|
||||||
|
|
||||||
|
// Expires
|
||||||
|
now := time.Now()
|
||||||
|
c.SetExpires(now)
|
||||||
|
assert.Equal(t, now, c.Expires())
|
||||||
|
|
||||||
|
// Secure
|
||||||
|
c.SetSecure(true)
|
||||||
|
assert.Equal(t, true, c.Secure())
|
||||||
|
|
||||||
|
// HTTPOnly
|
||||||
|
c.SetHTTPOnly(true)
|
||||||
|
assert.Equal(t, true, c.HTTPOnly())
|
||||||
|
}
|
4
echo.go
4
echo.go
@ -48,7 +48,7 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
ncontext "golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
"github.com/labstack/echo/engine"
|
"github.com/labstack/echo/engine"
|
||||||
"github.com/labstack/echo/log"
|
"github.com/labstack/echo/log"
|
||||||
@ -238,7 +238,7 @@ func New() (e *Echo) {
|
|||||||
// NewContext returns a Context instance.
|
// NewContext returns a Context instance.
|
||||||
func (e *Echo) NewContext(req engine.Request, res engine.Response) Context {
|
func (e *Echo) NewContext(req engine.Request, res engine.Response) Context {
|
||||||
return &echoContext{
|
return &echoContext{
|
||||||
context: ncontext.Background(),
|
Context: context.Background(),
|
||||||
request: req,
|
request: req,
|
||||||
response: res,
|
response: res,
|
||||||
echo: e,
|
echo: e,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user