forked from labstack/echo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test coverage for cookie.go and context.go
Signed-off-by: Vishal Rana <[email protected]>
- Loading branch information
Showing
4 changed files
with
172 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,15 @@ import ( | |
"bytes" | ||
"errors" | ||
"io" | ||
"mime/multipart" | ||
"net/http" | ||
"os" | ||
"testing" | ||
"text/template" | ||
"time" | ||
|
||
"golang.org/x/net/context" | ||
|
||
"strings" | ||
|
||
"net/url" | ||
|
@@ -36,27 +39,17 @@ func TestContext(t *testing.T) { | |
rec := test.NewResponseRecorder() | ||
c := e.NewContext(req, rec).(*echoContext) | ||
|
||
// Echo | ||
assert.Equal(t, e, c.Echo()) | ||
|
||
// Request | ||
assert.NotNil(t, c.Request()) | ||
assert.Equal(t, req, c.Request()) | ||
|
||
// Response | ||
assert.NotNil(t, c.Response()) | ||
|
||
// ParamNames | ||
c.pnames = []string{"uid", "fid"} | ||
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)) | ||
assert.Equal(t, rec, c.Response()) | ||
|
||
// Param by name | ||
assert.Equal(t, "1", c.Param("id")) | ||
|
||
// Store | ||
c.Set("user", "Jon Snow") | ||
assert.Equal(t, "Jon Snow", c.Get("user")) | ||
// Logger | ||
assert.Equal(t, e.logger, c.Logger()) | ||
|
||
//-------- | ||
// Render | ||
|
@@ -158,13 +151,6 @@ func TestContext(t *testing.T) { | |
c.NoContent(http.StatusOK) | ||
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 | ||
rec = test.NewResponseRecorder() | ||
c = e.NewContext(req, rec).(*echoContext) | ||
|
@@ -203,7 +189,7 @@ func TestContextCookie(t *testing.T) { | |
} | ||
|
||
// Write | ||
cookie = &test.Cookie{&http.Cookie{ | ||
cookie = &test.Cookie{Cookie: &http.Cookie{ | ||
Name: "SSID", | ||
Value: "Ap4PGTEq", | ||
Domain: "labstack.com", | ||
|
@@ -235,35 +221,130 @@ func TestContextPath(t *testing.T) { | |
assert.Equal(t, "/users/:uid/files/:fid", c.Path()) | ||
} | ||
|
||
func TestContextQueryParam(t *testing.T) { | ||
q := make(url.Values) | ||
q.Set("name", "joe") | ||
q.Set("email", "[email protected]") | ||
req := test.NewRequest(GET, "/?"+q.Encode(), nil) | ||
func TestContextPathParam(t *testing.T) { | ||
e := New() | ||
req := test.NewRequest(GET, "/", nil) | ||
c := e.NewContext(req, nil) | ||
assert.Equal(t, "joe", c.QueryParam("name")) | ||
assert.Equal(t, "[email protected]", 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) { | ||
f := make(url.Values) | ||
f.Set("name", "joe") | ||
f.Set("email", "joe@labstack.com") | ||
f.Set("name", "Jon Snow") | ||
f.Set("email", "jon@labstack.com") | ||
|
||
e := New() | ||
req := test.NewRequest(POST, "/", strings.NewReader(f.Encode())) | ||
req.Header().Add(HeaderContentType, MIMEApplicationForm) | ||
c := e.NewContext(req, nil) | ||
|
||
// FormValue | ||
assert.Equal(t, "Jon Snow", c.FormValue("name")) | ||
assert.Equal(t, "[email protected]", c.FormValue("email")) | ||
|
||
// FormParams | ||
assert.Equal(t, map[string][]string{ | ||
"name": []string{"Jon Snow"}, | ||
"email": []string{"[email protected]"}, | ||
}, c.FormParams()) | ||
} | ||
|
||
func TestContextQueryParam(t *testing.T) { | ||
q := make(url.Values) | ||
q.Set("name", "Jon Snow") | ||
q.Set("email", "[email protected]") | ||
req := test.NewRequest(GET, "/?"+q.Encode(), nil) | ||
e := New() | ||
c := e.NewContext(req, nil) | ||
assert.Equal(t, "joe", c.FormValue("name")) | ||
assert.Equal(t, "[email protected]", c.FormValue("email")) | ||
|
||
// QueryParam | ||
assert.Equal(t, "Jon Snow", c.QueryParam("name")) | ||
assert.Equal(t, "[email protected]", c.QueryParam("email")) | ||
|
||
// QueryParams | ||
assert.Equal(t, map[string][]string{ | ||
"name": []string{"Jon Snow"}, | ||
"email": []string{"[email protected]"}, | ||
}, 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 TestContextNetContext(t *testing.T) { | ||
// c := new(context) | ||
// c.Context = xcontext.WithValue(nil, "key", "val") | ||
// assert.Equal(t, "val", c.Value("key")) | ||
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) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters