forked from kataras/iris
-
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.
Some minor but helpful additions, like
CookieOption
. Relative: http…
…s://github.com/kataras/iris/issues/1018. Simple cookies example added too. Cookie encoding (side by side with the already session's cookie id encoding) and version upgrade will come tomorrow with a new HISTORY.md entry as well, stay tuned!
- Loading branch information
Showing
8 changed files
with
277 additions
and
30 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,64 @@ | ||
package main | ||
|
||
import "github.com/kataras/iris" | ||
|
||
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) // <-- | ||
// Alternatively: ctx.SetCookie(&http.Cookie{...}) | ||
// | ||
// If you want to set custom the path: | ||
// ctx.SetCookieKV(name, value, iris.CookiePath("/custom/path/cookie/will/be/stored")) | ||
// | ||
// If you want to be visible only to current request path: | ||
// (note that client should be responsible for that if server sent an empty cookie's path, all browsers are compatible) | ||
// ctx.SetCookieKV(name, value, iris.CookieCleanPath /* or iris.CookiePath("") */) | ||
// More: | ||
// iris.CookieExpires(time.Duration) | ||
// iris.CookieHTTPOnly(false) | ||
|
||
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) // <-- | ||
// If you want more than the value then: | ||
// cookie, err := ctx.Request().Cookie(name) | ||
// if err != nil { | ||
// handle error. | ||
// } | ||
|
||
ctx.WriteString(value) | ||
}) | ||
|
||
// Delete A Cookie. | ||
app.Delete("/cookies/{name}", func(ctx iris.Context) { | ||
name := ctx.Params().Get("name") | ||
|
||
ctx.RemoveCookie(name) // <-- | ||
// If you want to set custom the path: | ||
// ctx.SetCookieKV(name, value, iris.CookiePath("/custom/path/cookie/will/be/stored")) | ||
|
||
ctx.Writef("cookie %s removed", name) | ||
}) | ||
|
||
return app | ||
} | ||
|
||
func main() { | ||
app := newApp() | ||
|
||
// GET: http://localhost:8080/cookies/my_name/my_value | ||
// GET: http://localhost:8080/cookies/my_name | ||
// DELETE: http://localhost:8080/cookies/my_name | ||
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,32 @@ | ||
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) | ||
t1.Cookie(cookieName).Value().Equal(cookieValue) // validate cookie's existence, it should be there now. | ||
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() | ||
} |
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
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