mirror of https://github.com/gin-gonic/gin.git
Merge branch 'se77en-master' into develop
This commit is contained in:
commit
4892650ef8
37
context.go
37
context.go
|
@ -10,6 +10,7 @@ import (
|
||||||
"math"
|
"math"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -325,6 +326,42 @@ func (c *Context) Header(key, value string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Context) SetCookie(
|
||||||
|
name string,
|
||||||
|
value string,
|
||||||
|
maxAge int,
|
||||||
|
path string,
|
||||||
|
domain string,
|
||||||
|
secure bool,
|
||||||
|
httpOnly bool,
|
||||||
|
) {
|
||||||
|
cookie := http.Cookie{}
|
||||||
|
cookie.Name = name
|
||||||
|
cookie.Value = url.QueryEscape(value)
|
||||||
|
|
||||||
|
cookie.MaxAge = maxAge
|
||||||
|
|
||||||
|
cookie.Path = "/"
|
||||||
|
if path != "" {
|
||||||
|
cookie.Path = path
|
||||||
|
}
|
||||||
|
|
||||||
|
cookie.Domain = domain
|
||||||
|
cookie.Secure = secure
|
||||||
|
cookie.HttpOnly = httpOnly
|
||||||
|
|
||||||
|
c.Writer.Header().Add("Set-Cookie", cookie.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Context) GetCookie(name string) (string, error) {
|
||||||
|
cookie, err := c.Request.Cookie(name)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
val, _ := url.QueryUnescape(cookie.Value)
|
||||||
|
return val, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Context) Render(code int, r render.Render) {
|
func (c *Context) Render(code int, r render.Render) {
|
||||||
c.writermem.WriteHeader(code)
|
c.writermem.WriteHeader(code)
|
||||||
if err := r.Render(c.Writer); err != nil {
|
if err := r.Render(c.Writer); err != nil {
|
||||||
|
|
|
@ -238,6 +238,21 @@ func TestContextPostFormMultipart(t *testing.T) {
|
||||||
assert.Equal(t, c.PostForm("bar"), "foo")
|
assert.Equal(t, c.PostForm("bar"), "foo")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContextSetCookie(t *testing.T) {
|
||||||
|
c, _, _ := createTestContext()
|
||||||
|
c.SetCookie("user", "gin", 1, "/", "localhost", true, true)
|
||||||
|
c.Request, _ = http.NewRequest("GET", "/set", nil)
|
||||||
|
assert.Equal(t, c.Writer.Header().Get("Set-Cookie"), "user=gin; Path=/; Domain=localhost; Max-Age=1; HttpOnly; Secure")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContextGetCookie(t *testing.T) {
|
||||||
|
c, _, _ := createTestContext()
|
||||||
|
c.Request, _ = http.NewRequest("GET", "/get", nil)
|
||||||
|
c.Request.Header.Set("Cookie", "user=gin")
|
||||||
|
cookie, _ := c.GetCookie("user")
|
||||||
|
assert.Equal(t, cookie, "gin")
|
||||||
|
}
|
||||||
|
|
||||||
// Tests that the response is serialized as JSON
|
// Tests that the response is serialized as JSON
|
||||||
// and Content-Type is set to application/json
|
// and Content-Type is set to application/json
|
||||||
func TestContextRenderJSON(t *testing.T) {
|
func TestContextRenderJSON(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue