Browse Source

Ability to destroy session file

Vova Tkach 6 years ago
parent
commit
16b3bf6f83
2 changed files with 94 additions and 0 deletions
  1. 7 0
      session/session.go
  2. 87 0
      session/session_test.go

+ 7 - 0
session/session.go

@@ -95,3 +95,10 @@ func (this *Session) Close() bool {
 
 	return false
 }
+
+func (this *Session) Destroy() error {
+	if this.d == "" || this.i == "" {
+		return nil
+	}
+	return os.Remove(this.d + string(os.PathSeparator) + this.i)
+}

+ 87 - 0
session/session_test.go

@@ -245,3 +245,90 @@ func TestSessionDoNotCreateSessionFileForDefValues(t *testing.T) {
 		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.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
+	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.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")
+	}
+
+	// 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" {
+			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")
+	}
+}