Skip to content

Commit

Permalink
remove is dep
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdata committed Oct 20, 2023
1 parent 20b6b07 commit 5da0815
Showing 1 changed file with 88 additions and 53 deletions.
141 changes: 88 additions & 53 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"net"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"time"

"github.com/linkdata/jaws/what"
"github.com/matryer/is"
"nhooyr.io/websocket"
)

Expand Down Expand Up @@ -353,116 +353,151 @@ func TestSession_Delete(t *testing.T) {
}

func TestSession_Cleanup(t *testing.T) {
is := is.New(t)
jw := New()
defer jw.Close()

rr := httptest.NewRecorder()
hr := httptest.NewRequest("GET", "/", nil)

sess := jw.NewSession(rr, hr)
is.Equal(jw.GetSession(hr), sess)
is.True(sess != nil)
is.Equal(len(rr.Result().Cookies()), 1)
if x := sess; x == nil || x != jw.GetSession(hr) {
t.Fatal(x)
}
if x := len(rr.Result().Cookies()); x != 1 {
t.Fatal(x)
}

r1 := jw.NewRequest(hr)
is.True(r1 != nil)
is.Equal(r1.Session(), sess)
is.Equal(len(sess.requests), 1)
is.Equal(sess.requests[0], r1)
if x := sess; x != r1.Session() {
t.Error(x)
}
if x := len(sess.requests); x != 1 {
t.Error(x)
}
if x := sess.requests[0]; x != r1 {
t.Error(x)
}

r1.recycle()
r1 = nil
sess.deadline = time.Now()
is.Equal(jw.SessionCount(), 1)
if x := jw.SessionCount(); x != 1 {
t.Error(x)
}

go jw.ServeWithTimeout(time.Millisecond)
waited := 0
for waited < 100 && jw.SessionCount() > 0 {
waited++
time.Sleep(time.Millisecond)
}
is.Equal(jw.SessionCount(), 0)
if x := jw.SessionCount(); x != 0 {
t.Error(x)
}
}

func TestSession_ReplacesOld(t *testing.T) {
is := is.New(t)
jw := New()
defer jw.Close()
go jw.ServeWithTimeout(time.Second)

is.Equal(jw.SessionCount(), 0)
isEqual := func(a, b any) {
t.Helper()
if a != b {
if !reflect.DeepEqual(a, b) {
t.Errorf("%#v != %#v", a, b)
}
}
}

isTrue := func(a bool) {
t.Helper()
if !a {
t.Errorf("not true")
}
}

isEqual(jw.SessionCount(), 0)

w1 := httptest.NewRecorder()
h1 := httptest.NewRequest("GET", "/", nil)
s1 := jw.NewSession(w1, h1)
is.True(s1 != nil)
is.Equal(jw.GetSession(h1), s1)
is.Equal(len(w1.Result().Cookies()), 1)
isEqual(jw.GetSession(h1), s1)
isEqual(len(w1.Result().Cookies()), 1)
r1 := jw.NewRequest(h1)
is.Equal(r1.Session(), s1)
isEqual(r1.Session(), s1)
c1 := w1.Result().Cookies()[0]
is.Equal(c1.MaxAge, 0)
is.Equal(c1.Name, s1.cookie.Name)
is.Equal(c1.Value, s1.CookieValue())
is.True(!s1.isDead())
isEqual(c1.MaxAge, 0)
isEqual(c1.Name, s1.cookie.Name)
isEqual(c1.Value, s1.CookieValue())
if s1.isDead() {
t.Fatal("dead")
}
s1.Set("foo", "bar")
is.Equal(s1.Get("foo"), "bar")
isEqual(s1.Get("foo"), "bar")
c1copy := *c1

is.Equal(jw.SessionCount(), 1)
isEqual(jw.SessionCount(), 1)

w2 := httptest.NewRecorder()
h2 := httptest.NewRequest("GET", "/", nil)
s2 := jw.NewSession(w2, h2)
is.True(s2 != nil)
is.Equal(jw.GetSession(h2), s2)
is.Equal(len(w2.Result().Cookies()), 1)
isEqual(jw.GetSession(h2), s2)
isEqual(len(w2.Result().Cookies()), 1)
r2 := jw.NewRequest(h2)
is.Equal(r2.Session(), s2)
isEqual(r2.Session(), s2)
c2 := w2.Result().Cookies()[0]
is.Equal(c2.MaxAge, 0)
is.Equal(c2.Name, s2.cookie.Name)
is.Equal(c2.Value, s2.CookieValue())
is.True(!s2.isDead())
isEqual(c2.MaxAge, 0)
isEqual(c2.Name, s2.cookie.Name)
isEqual(c2.Value, s2.CookieValue())
if s2.isDead() {
t.Fatal("dead")
}

is.Equal(jw.SessionCount(), 2)
is.True(s1 != s2)
is.True(c1.Value != c2.Value)
isEqual(jw.SessionCount(), 2)
if s1 == s2 {
t.Fatal("identical")
}
if c1.Value == c2.Value {
t.Fatal("same value")
}

w4 := httptest.NewRecorder()
h4 := httptest.NewRequest("GET", "/", nil)
h4.AddCookie(&c1copy)
r4 := jw.NewRequest(h4)
is.Equal(r4.Session(), s1)
is.Equal(jw.GetSession(h4), s1)
is.Equal(len(w4.Result().Cookies()), 0)
isEqual(r4.Session(), s1)
isEqual(jw.GetSession(h4), s1)
isEqual(len(w4.Result().Cookies()), 0)

r4.recycle()

w3 := httptest.NewRecorder()
h3 := httptest.NewRequest("GET", "/", nil)
h3.AddCookie(&c1copy)
s3 := jw.NewSession(w3, h3)
is.True(s3 != nil)
is.Equal(jw.GetSession(h3), s3)
is.Equal(len(w3.Result().Cookies()), 1)
isTrue(s3 != nil)
isEqual(jw.GetSession(h3), s3)
isEqual(len(w3.Result().Cookies()), 1)
c3 := w3.Result().Cookies()[0]
is.Equal(c3.MaxAge, 0)
is.Equal(c3.Name, s3.cookie.Name)
is.Equal(c3.Value, s3.cookie.Value)
is.True(!s3.isDead())
isEqual(c3.MaxAge, 0)
isEqual(c3.Name, s3.cookie.Name)
isEqual(c3.Value, s3.cookie.Value)
isTrue(!s3.isDead())

is.Equal(jw.SessionCount(), 2)
is.True(s1 != s3)
is.True(c1.Value != c3.Value)
isEqual(jw.SessionCount(), 2)
isTrue(s1 != s3)
isTrue(c1.Value != c3.Value)

is.True(s1.isDead())
is.True(!s2.isDead())
is.True(!s3.isDead())
is.Equal(s1.Get("foo"), nil)
is.Equal(s1.Cookie().MaxAge, -1)
isTrue(s1.isDead())
isTrue(!s2.isDead())
isTrue(!s3.isDead())
isEqual(s1.Get("foo"), nil)
isEqual(s1.Cookie().MaxAge, -1)

h5 := httptest.NewRequest("GET", "/", nil)
h5.AddCookie(&c1copy)
is.Equal(jw.GetSession(h5), nil)
if x := jw.GetSession(h5); x != nil {
t.Error(x)
}
}

0 comments on commit 5da0815

Please sign in to comment.