-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cookies: Ability to set custom cookie encoders to encode the cookie's…
… value before sent by `ctx.SetCookie` and `ctx.SetCookieKV` and cookie decoders to decode the cookie's value when retrieving from `ctx.GetCookie`. That was the second and final part relative to a community's question at: #1018
- Loading branch information
Showing
7 changed files
with
496 additions
and
68 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
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,59 @@ | ||
package main | ||
|
||
// developers can use any library to add a custom cookie encoder/decoder. | ||
// At this example we use the gorilla's securecookie package: | ||
// $ go get github.com/gorilla/securecookie | ||
// $ go run main.go | ||
|
||
import ( | ||
"github.com/kataras/iris" | ||
|
||
"github.com/gorilla/securecookie" | ||
) | ||
|
||
var ( | ||
// AES only supports key sizes of 16, 24 or 32 bytes. | ||
// You either need to provide exactly that amount or you derive the key from what you type in. | ||
hashKey = []byte("the-big-and-secret-fash-key-here") | ||
blockKey = []byte("lot-secret-of-characters-big-too") | ||
sc = securecookie.New(hashKey, blockKey) | ||
) | ||
|
||
func newApp() *iris.Application { | ||
app := iris.New() | ||
|
||
// Set A Cookie. | ||
app.Get("/cookies/{name}/{value}", func(ctx iris.Context) { | ||
name := ctx.Params().Get("name") | ||
value := ctx.Params().Get("value") | ||
|
||
ctx.SetCookieKV(name, value, iris.CookieEncode(sc.Encode)) // <-- | ||
|
||
ctx.Writef("cookie added: %s = %s", name, value) | ||
}) | ||
|
||
// Retrieve A Cookie. | ||
app.Get("/cookies/{name}", func(ctx iris.Context) { | ||
name := ctx.Params().Get("name") | ||
|
||
value := ctx.GetCookie(name, iris.CookieDecode(sc.Decode)) // <-- | ||
|
||
ctx.WriteString(value) | ||
}) | ||
|
||
// Delete A Cookie. | ||
app.Delete("/cookies/{name}", func(ctx iris.Context) { | ||
name := ctx.Params().Get("name") | ||
|
||
ctx.RemoveCookie(name) // <-- | ||
|
||
ctx.Writef("cookie %s removed", name) | ||
}) | ||
|
||
return app | ||
} | ||
|
||
func main() { | ||
app := newApp() | ||
app.Run(iris.Addr(":8080")) | ||
} |
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,34 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/kataras/iris/httptest" | ||
) | ||
|
||
func TestCookiesBasic(t *testing.T) { | ||
app := newApp() | ||
e := httptest.New(t, app, httptest.URL("http://example.com")) | ||
|
||
cookieName, cookieValue := "my_cookie_name", "my_cookie_value" | ||
|
||
// Test Set A Cookie. | ||
t1 := e.GET(fmt.Sprintf("/cookies/%s/%s", cookieName, cookieValue)).Expect().Status(httptest.StatusOK) | ||
// note that this will not work because it doesn't always returns the same value: | ||
// cookieValueEncoded, _ := sc.Encode(cookieName, cookieValue) | ||
t1.Cookie(cookieName).Value().NotEqual(cookieValue) // validate cookie's existence and value is not on its raw form. | ||
t1.Body().Contains(cookieValue) | ||
|
||
// Test Retrieve A Cookie. | ||
t2 := e.GET(fmt.Sprintf("/cookies/%s", cookieName)).Expect().Status(httptest.StatusOK) | ||
t2.Body().Equal(cookieValue) | ||
|
||
// Test Remove A Cookie. | ||
t3 := e.DELETE(fmt.Sprintf("/cookies/%s", cookieName)).Expect().Status(httptest.StatusOK) | ||
t3.Body().Contains(cookieName) | ||
|
||
t4 := e.GET(fmt.Sprintf("/cookies/%s", cookieName)).Expect().Status(httptest.StatusOK) | ||
t4.Cookies().Empty() | ||
t4.Body().Empty() | ||
} |
Oops, something went wrong.