Merge branch 'master' of https://github.com/se77en/gin into se77en-master
This commit is contained in:
commit
45b72951af
37
context.go
37
context.go
@ -9,6 +9,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -321,6 +322,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 {
|
||||||
|
cookie, err := c.Request.Cookie(name)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
val, _ := url.QueryUnescape(cookie.Value)
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
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 {
|
||||||
|
@ -247,6 +247,23 @@ 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, w, _ := createTestContext()
|
||||||
|
c.SetCookie("user", "gin", 1, "/", "localhost", true, true)
|
||||||
|
c.SetCookie("user", "gin", int32(1), "/", "localhost", 1)
|
||||||
|
c.SetCookie("user", "gin", int64(1))
|
||||||
|
|
||||||
|
c.Request, _ = http.NewRequest("GET", "/set", nil)
|
||||||
|
assert.Equal(t, c.GetCookie("Set-Cookie"), "user=gin; Path=/; Domain=localhost; Max-Age=1; HttpOnly; Secure")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContextGetCookie(t *testing.T) {
|
||||||
|
c, w, _ := createTestContext()
|
||||||
|
c.Request, _ = http.NewRequest("GET", "/get", nil)
|
||||||
|
c.Request.Header.Set("Cookie", "user=gin")
|
||||||
|
assert.Equal(t, c.GetCookie("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
Block a user