123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- package session
- import (
- "fmt"
- "io/ioutil"
- "net/http"
- "net/http/httptest"
- "os"
- "testing"
- )
- var SessionId string = ""
- func TestSessionBool(t *testing.T) {
- // Set value
- request, err := http.NewRequest("GET", "/set", nil)
- if err != nil {
- t.Fatal(err)
- }
- recorder := httptest.NewRecorder()
- http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- sess := New(w, r, "./../tmp")
- defer sess.Close()
- if r.URL.Path == "/set" {
- sess.SetBool("some_bool", true)
- w.Write([]byte(`ok`))
- } else {
- w.WriteHeader(http.StatusNotFound)
- w.Write([]byte(`404`))
- }
- }).ServeHTTP(recorder, request)
- if recorder.Body.String() != "ok" {
- t.Fatalf("bad body response, not match")
- }
- // Remember session id
- if SessionId == "" && len(recorder.Result().Cookies()) > 0 {
- SessionId = recorder.Result().Cookies()[0].Value
- }
- // Get value
- request, err = http.NewRequest("GET", "/get", nil)
- if err != nil {
- t.Fatal(err)
- }
- request.Header.Set("Cookie", "session="+SessionId)
- recorder = httptest.NewRecorder()
- http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- sess := New(w, r, "./../tmp")
- defer sess.Close()
- if r.URL.Path == "/get" {
- w.Write([]byte(fmt.Sprintf("%v", sess.GetBool("some_bool", false))))
- } else {
- w.WriteHeader(http.StatusNotFound)
- w.Write([]byte(`404`))
- }
- }).ServeHTTP(recorder, request)
- if recorder.Body.String() != "true" {
- t.Fatalf("bad body response, not match")
- }
- }
- func TestSessionInt(t *testing.T) {
- // Set value
- request, err := http.NewRequest("GET", "/set", nil)
- if err != nil {
- t.Fatal(err)
- }
- request.Header.Set("Cookie", "session="+SessionId)
- recorder := httptest.NewRecorder()
- http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- sess := New(w, r, "./../tmp")
- defer sess.Close()
- if r.URL.Path == "/set" {
- sess.SetInt("some_int", 5)
- w.Write([]byte(`ok`))
- } else {
- w.WriteHeader(http.StatusNotFound)
- w.Write([]byte(`404`))
- }
- }).ServeHTTP(recorder, request)
- if recorder.Body.String() != "ok" {
- t.Fatalf("bad body response, not match")
- }
- // Remember session id
- if SessionId == "" && len(recorder.Result().Cookies()) > 0 {
- SessionId = recorder.Result().Cookies()[0].Value
- }
- // Get value
- request, err = http.NewRequest("GET", "/get", nil)
- if err != nil {
- t.Fatal(err)
- }
- request.Header.Set("Cookie", "session="+SessionId)
- recorder = httptest.NewRecorder()
- http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- sess := New(w, r, "./../tmp")
- defer sess.Close()
- if r.URL.Path == "/get" {
- w.Write([]byte(fmt.Sprintf("%d", sess.GetInt("some_int", 0))))
- } else {
- w.WriteHeader(http.StatusNotFound)
- w.Write([]byte(`404`))
- }
- }).ServeHTTP(recorder, request)
- if recorder.Body.String() != "5" {
- t.Fatalf("bad body response, not match")
- }
- }
- func TestSessionString(t *testing.T) {
- // Set value
- request, err := http.NewRequest("GET", "/set", nil)
- if err != nil {
- t.Fatal(err)
- }
- request.Header.Set("Cookie", "session="+SessionId)
- recorder := httptest.NewRecorder()
- http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- sess := New(w, r, "./../tmp")
- defer sess.Close()
- if r.URL.Path == "/set" {
- sess.SetString("some_str", "test")
- w.Write([]byte(`ok`))
- } else {
- w.WriteHeader(http.StatusNotFound)
- w.Write([]byte(`404`))
- }
- }).ServeHTTP(recorder, request)
- if recorder.Body.String() != "ok" {
- t.Fatalf("bad body response, not match")
- }
- // Remember session id
- if SessionId == "" && len(recorder.Result().Cookies()) > 0 {
- SessionId = recorder.Result().Cookies()[0].Value
- }
- // Get value
- request, err = http.NewRequest("GET", "/get", nil)
- if err != nil {
- t.Fatal(err)
- }
- request.Header.Set("Cookie", "session="+SessionId)
- recorder = httptest.NewRecorder()
- http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- sess := New(w, r, "./../tmp")
- defer sess.Close()
- if r.URL.Path == "/get" {
- w.Write([]byte(fmt.Sprintf("%s", sess.GetString("some_str", ""))))
- } else {
- w.WriteHeader(http.StatusNotFound)
- w.Write([]byte(`404`))
- }
- }).ServeHTTP(recorder, request)
- if recorder.Body.String() != "test" {
- t.Fatalf("bad body response, not match")
- }
- }
- func TestSessionActualFile(t *testing.T) {
- if SessionId == "" {
- t.Fatal("SessionId is empty")
- }
- fname := "./../tmp" + string(os.PathSeparator) + SessionId
- bytes, err := ioutil.ReadFile(fname)
- if err != nil {
- t.Fatal(err)
- }
- if string(bytes) != `{"Bool":{"some_bool":true},"Int":{"some_int":5},"String":{"some_str":"test"}}` {
- t.Fatal("actual file content, not match")
- }
- err = os.Remove(fname)
- if err != nil {
- t.Fatal(err)
- }
- }
- func TestSessionDoNotCreateSessionFileForDefValues(t *testing.T) {
- // Set default values
- request, err := http.NewRequest("GET", "/set", nil)
- if err != nil {
- t.Fatal(err)
- }
- recorder := httptest.NewRecorder()
- http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- sess := New(w, r, "./../tmp")
- defer sess.Close()
- if r.URL.Path == "/set" {
- sess.SetBool("some_bool", false)
- sess.SetInt("some_int", 0)
- sess.SetString("some_str", "")
- w.Write([]byte(`ok`))
- } else {
- w.WriteHeader(http.StatusNotFound)
- w.Write([]byte(`404`))
- }
- }).ServeHTTP(recorder, request)
- if recorder.Body.String() != "ok" {
- t.Fatalf("bad body response, not match")
- }
- // Remember session id
- var sessId string
- if len(recorder.Result().Cookies()) > 0 {
- sessId = recorder.Result().Cookies()[0].Value
- }
- if sessId == "" {
- t.Fatalf("session identifier is not defined")
- }
- // Get value
- request, err = http.NewRequest("GET", "/get", nil)
- if err != nil {
- t.Fatal(err)
- }
- request.Header.Set("Cookie", "session="+sessId)
- recorder = httptest.NewRecorder()
- http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- sess := New(w, r, "./../tmp")
- defer sess.Close()
- if r.URL.Path == "/get" {
- w.Write([]byte(fmt.Sprintf(
- "(%v)(%v)(%v)",
- sess.GetBool("some_bool", false),
- sess.GetInt("some_int", 0),
- sess.GetString("some_str", ""),
- )))
- } else {
- w.WriteHeader(http.StatusNotFound)
- w.Write([]byte(`404`))
- }
- }).ServeHTTP(recorder, request)
- if recorder.Body.String() != "(false)(0)()" {
- t.Fatalf("bad body response, not match")
- }
- // Check session file
- fname := "./../tmp" + string(os.PathSeparator) + sessId
- _, err = ioutil.ReadFile(fname)
- if err == nil {
- _ = os.Remove(fname)
- t.Fatalf("session file in tmp folder do not must exists")
- }
- }
- func TestSessionDestroy(t *testing.T) {
- // Set value
- request, err := http.NewRequest("GET", "/set", nil)
- if err != nil {
- t.Fatal(err)
- }
- recorder := httptest.NewRecorder()
- http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- sess := New(w, r, "./../tmp")
- defer sess.Close()
- if r.URL.Path == "/set" {
- sess.SetInt("some_var", 1)
- w.Write([]byte(`ok`))
- } else {
- w.WriteHeader(http.StatusNotFound)
- w.Write([]byte(`404`))
- }
- }).ServeHTTP(recorder, request)
- if recorder.Body.String() != "ok" {
- t.Fatalf("bad body response, not match")
- }
- // Remember session id
- var sessId string
- if len(recorder.Result().Cookies()) > 0 {
- sessId = recorder.Result().Cookies()[0].Value
- }
- if sessId == "" {
- t.Fatalf("session identifier is not defined")
- }
- // Get value
- request, err = http.NewRequest("GET", "/get", nil)
- if err != nil {
- t.Fatal(err)
- }
- request.Header.Set("Cookie", "session="+sessId)
- recorder = httptest.NewRecorder()
- http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- sess := New(w, r, "./../tmp")
- defer sess.Close()
- if r.URL.Path == "/get" {
- w.Write([]byte(fmt.Sprintf("%v", sess.GetInt("some_var", 0))))
- } else {
- w.WriteHeader(http.StatusNotFound)
- w.Write([]byte(`404`))
- }
- }).ServeHTTP(recorder, request)
- if recorder.Body.String() != "1" {
- t.Fatalf("bad body response, not match")
- }
- // Check destroy
- request, err = http.NewRequest("GET", "/get", nil)
- if err != nil {
- t.Fatal(err)
- }
- request.Header.Set("Cookie", "session="+sessId)
- recorder = httptest.NewRecorder()
- http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- sess := New(w, r, "./../tmp")
- defer sess.Close()
- if r.URL.Path == "/get" {
- sess.SetInt("some_var", 2)
- err := sess.Destroy()
- if err == nil {
- w.Write([]byte(`OK`))
- } else {
- w.Write([]byte(`ERROR`))
- }
- } else {
- w.WriteHeader(http.StatusNotFound)
- w.Write([]byte(`404`))
- }
- }).ServeHTTP(recorder, request)
- if recorder.Body.String() != "OK" {
- t.Fatalf("bad body response, not match")
- }
- // Check session file
- fname := "./../tmp" + string(os.PathSeparator) + sessId
- _, err = ioutil.ReadFile(fname)
- if err == nil {
- _ = os.Remove(fname)
- t.Fatalf("session file in tmp folder do not must exists")
- }
- }
|