123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- package tango
- import (
- "testing"
- "bytes"
- "strings"
- "net/http"
- "net/http/httptest"
- )
- func TestCookie1(t *testing.T) {
- buff := bytes.NewBufferString("")
- recorder := httptest.NewRecorder()
- recorder.Body = buff
- o := Classic()
- o.Get("/", func(ctx *Context) string {
- ck := ctx.Cookies().Get("name")
- if ck != nil {
- return ck.Value
- }
- return ""
- })
- req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
- if err != nil {
- t.Error(err)
- }
- req.AddCookie(NewCookie("name", "test"))
- o.ServeHTTP(recorder, req)
- expect(t, recorder.Code, http.StatusOK)
- refute(t, len(buff.String()), 0)
- expect(t, buff.String(), "test")
- }
- func TestCookie2(t *testing.T) {
- buff := bytes.NewBufferString("")
- recorder := httptest.NewRecorder()
- recorder.Body = buff
- o := Classic()
- o.Get("/", func(ctx *Context) string {
- ctx.Cookies().Set(NewCookie("name", "test"))
- return "test"
- })
- req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
- if err != nil {
- t.Error(err)
- }
- o.ServeHTTP(recorder, req)
- expect(t, recorder.Code, http.StatusOK)
- refute(t, len(buff.String()), 0)
- expect(t, buff.String(), "test")
- expect(t, strings.Split(recorder.Header().Get("Set-Cookie"), ";")[0], "name=test")
- }
- func TestCookie3(t *testing.T) {
- buff := bytes.NewBufferString("")
- recorder := httptest.NewRecorder()
- recorder.Body = buff
- o := Classic()
- o.Get("/", func(ctx *Context) string {
- ck := ctx.SecureCookies("sssss").Get("name")
- if ck != nil {
- return ck.Value
- }
- return ""
- })
- req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
- if err != nil {
- t.Error(err)
- }
- req.AddCookie(NewSecureCookie("sssss", "name", "test"))
- o.ServeHTTP(recorder, req)
- expect(t, recorder.Code, http.StatusOK)
- refute(t, len(buff.String()), 0)
- expect(t, buff.String(), "test")
- }
- func TestCookie4(t *testing.T) {
- buff := bytes.NewBufferString("")
- recorder := httptest.NewRecorder()
- recorder.Body = buff
- o := Classic()
- o.Get("/", func(ctx *Context) string {
- ctx.SecureCookies("ttttt").Set(NewSecureCookie("ttttt", "name", "test"))
- return "test"
- })
- req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
- if err != nil {
- t.Error(err)
- }
- o.ServeHTTP(recorder, req)
- expect(t, recorder.Code, http.StatusOK)
- refute(t, len(buff.String()), 0)
- expect(t, buff.String(), "test")
- r := strings.Split(recorder.Header().Get("Set-Cookie"), ";")[0]
- s := strings.SplitN(r, "=", 2)
- name, value := s[0], s[1]
- expect(t, name, "name")
- expect(t, parseSecureCookie("ttttt", value), "test")
- }
|