1
0
mirror of https://github.com/alexedwards/scs.git synced 2025-07-17 01:12:21 +02:00

[refactor] Use explicit package names for engines

This commit is contained in:
Alex Edwards
2016-08-18 18:07:37 +02:00
parent ffbc5bcf36
commit c7477e8965
6 changed files with 65 additions and 65 deletions

View File

@ -1,4 +1,4 @@
package engine package memstore
import ( import (
"errors" "errors"
@ -9,19 +9,19 @@ import (
var ErrTypeAssertionFailed = errors.New("type assertion failed: could not convert interface{} to []byte") var ErrTypeAssertionFailed = errors.New("type assertion failed: could not convert interface{} to []byte")
func New() *engine { func New() *memstore {
return &engine{ return &memstore{
// Clear up expired items once every minute // Clear up expired items once every minute
cache.New(cache.DefaultExpiration, time.Minute), cache.New(cache.DefaultExpiration, time.Minute),
} }
} }
type engine struct { type memstore struct {
*cache.Cache *cache.Cache
} }
func (e *engine) Find(token string) ([]byte, bool, error) { func (m *memstore) Find(token string) ([]byte, bool, error) {
v, exists := e.Cache.Get(token) v, exists := m.Cache.Get(token)
if exists == false { if exists == false {
return nil, exists, nil return nil, exists, nil
} }
@ -34,12 +34,12 @@ func (e *engine) Find(token string) ([]byte, bool, error) {
return b, exists, nil return b, exists, nil
} }
func (e *engine) Save(token string, b []byte, expiry time.Time) error { func (m *memstore) Save(token string, b []byte, expiry time.Time) error {
e.Cache.Set(token, b, expiry.Sub(time.Now())) m.Cache.Set(token, b, expiry.Sub(time.Now()))
return nil return nil
} }
func (e *engine) Delete(token string) error { func (m *memstore) Delete(token string) error {
e.Cache.Delete(token) m.Cache.Delete(token)
return nil return nil
} }

View File

@ -1,4 +1,4 @@
package engine package memstore
import ( import (
"bytes" "bytes"
@ -9,22 +9,22 @@ import (
) )
func TestNew(t *testing.T) { func TestNew(t *testing.T) {
e := New() m := New()
_, ok := interface{}(e).(session.Engine) _, ok := interface{}(m).(session.Engine)
if ok == false { if ok == false {
t.Fatalf("got %v: expected %v", ok, true) t.Fatalf("got %v: expected %v", ok, true)
} }
if len(e.Cache.Items()) > 0 { if len(m.Cache.Items()) > 0 {
t.Fatalf("got %d: expected %d", len(e.Cache.Items()), 0) t.Fatalf("got %d: expected %d", len(m.Cache.Items()), 0)
} }
} }
func TestFind(t *testing.T) { func TestFind(t *testing.T) {
e := New() m := New()
e.Cache.Set("test_session_token", []byte("encoded_data"), 0) m.Cache.Set("test_session_token", []byte("encoded_data"), 0)
b, found, err := e.Find("test_session_token") b, found, err := m.Find("test_session_token")
if err != nil { if err != nil {
t.Fatalf("got %v: expected %v", err, nil) t.Fatalf("got %v: expected %v", err, nil)
} }
@ -35,7 +35,7 @@ func TestFind(t *testing.T) {
t.Fatalf("got %v: expected %v", b, []byte("encoded_data")) t.Fatalf("got %v: expected %v", b, []byte("encoded_data"))
} }
b, found, err = e.Find("missing_session_token") b, found, err = m.Find("missing_session_token")
if err != nil { if err != nil {
t.Fatalf("got %v: expected %v", err, nil) t.Fatalf("got %v: expected %v", err, nil)
} }
@ -48,54 +48,54 @@ func TestFind(t *testing.T) {
} }
func TestFindBadData(t *testing.T) { func TestFindBadData(t *testing.T) {
e := New() m := New()
e.Cache.Set("test_session_token", "not_a_byte_slice", 0) m.Cache.Set("test_session_token", "not_a_byte_slice", 0)
_, _, err := e.Find("test_session_token") _, _, err := m.Find("test_session_token")
if err != ErrTypeAssertionFailed { if err != ErrTypeAssertionFailed {
t.Fatalf("got %v: expected %v", err, ErrTypeAssertionFailed) t.Fatalf("got %v: expected %v", err, ErrTypeAssertionFailed)
} }
} }
func TestExpiry(t *testing.T) { func TestExpiry(t *testing.T) {
e := New() m := New()
err := e.Save("test_session_token", []byte("encoded_data"), time.Now().Add(100*time.Millisecond)) err := m.Save("test_session_token", []byte("encoded_data"), time.Now().Add(100*time.Millisecond))
if err != nil { if err != nil {
t.Fatalf("got %v: expected %v", err, nil) t.Fatalf("got %v: expected %v", err, nil)
} }
_, found, _ := e.Find("test_session_token") _, found, _ := m.Find("test_session_token")
if found != true { if found != true {
t.Fatalf("got %v: expected %v", found, true) t.Fatalf("got %v: expected %v", found, true)
} }
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
_, found, _ = e.Find("test_session_token") _, found, _ = m.Find("test_session_token")
if found != false { if found != false {
t.Fatalf("got %v: expected %v", found, false) t.Fatalf("got %v: expected %v", found, false)
} }
} }
func TestSave(t *testing.T) { func TestSave(t *testing.T) {
e := New() m := New()
err := e.Save("test_session_token", []byte("encoded_data"), time.Now().Add(time.Minute)) err := m.Save("test_session_token", []byte("encoded_data"), time.Now().Add(time.Minute))
if err != nil { if err != nil {
t.Fatalf("got %v: expected %v", err, nil) t.Fatalf("got %v: expected %v", err, nil)
} }
if len(e.Cache.Items()) != 1 { if len(m.Cache.Items()) != 1 {
t.Fatalf("got %d: expected %d", len(e.Cache.Items()), 1) t.Fatalf("got %d: expected %d", len(m.Cache.Items()), 1)
} }
} }
func TestDelete(t *testing.T) { func TestDelete(t *testing.T) {
e := New() m := New()
e.Cache.Set("test_session_token", []byte("encoded_data"), 0) m.Cache.Set("test_session_token", []byte("encoded_data"), 0)
err := e.Delete("test_session_token") err := m.Delete("test_session_token")
if err != nil { if err != nil {
t.Fatalf("got %v: expected %v", err, nil) t.Fatalf("got %v: expected %v", err, nil)
} }
if len(e.Cache.Items()) != 0 { if len(m.Cache.Items()) != 0 {
t.Fatalf("got %d: expected %d", len(e.Cache.Items()), 0) t.Fatalf("got %d: expected %d", len(m.Cache.Items()), 0)
} }
} }

View File

@ -6,11 +6,11 @@ import (
"testing" "testing"
"time" "time"
"github.com/alexedwards/scs/mem/engine" "github.com/alexedwards/scs/engine/memstore"
) )
func TestString(t *testing.T) { func TestString(t *testing.T) {
m := Manage(engine.New()) m := Manage(memstore.New())
h := m(testServeMux) h := m(testServeMux)
_, body, cookie := testRequest(t, h, "/PutString", "") _, body, cookie := testRequest(t, h, "/PutString", "")
@ -35,7 +35,7 @@ func TestString(t *testing.T) {
} }
func TestBool(t *testing.T) { func TestBool(t *testing.T) {
m := Manage(engine.New()) m := Manage(memstore.New())
h := m(testServeMux) h := m(testServeMux)
_, body, cookie := testRequest(t, h, "/PutBool", "") _, body, cookie := testRequest(t, h, "/PutBool", "")
@ -60,7 +60,7 @@ func TestBool(t *testing.T) {
} }
func TestInt(t *testing.T) { func TestInt(t *testing.T) {
m := Manage(engine.New()) m := Manage(memstore.New())
h := m(testServeMux) h := m(testServeMux)
_, body, cookie := testRequest(t, h, "/PutInt", "") _, body, cookie := testRequest(t, h, "/PutInt", "")
@ -93,7 +93,7 @@ func TestInt(t *testing.T) {
} }
func TestInt64(t *testing.T) { func TestInt64(t *testing.T) {
m := Manage(engine.New()) m := Manage(memstore.New())
h := m(testServeMux) h := m(testServeMux)
_, body, cookie := testRequest(t, h, "/PutInt64", "") _, body, cookie := testRequest(t, h, "/PutInt64", "")
@ -126,7 +126,7 @@ func TestInt64(t *testing.T) {
} }
func TestFloat(t *testing.T) { func TestFloat(t *testing.T) {
m := Manage(engine.New()) m := Manage(memstore.New())
h := m(testServeMux) h := m(testServeMux)
_, body, cookie := testRequest(t, h, "/PutFloat", "") _, body, cookie := testRequest(t, h, "/PutFloat", "")
@ -159,7 +159,7 @@ func TestFloat(t *testing.T) {
} }
func TestTime(t *testing.T) { func TestTime(t *testing.T) {
m := Manage(engine.New()) m := Manage(memstore.New())
h := m(testServeMux) h := m(testServeMux)
_, body, cookie := testRequest(t, h, "/PutTime", "") _, body, cookie := testRequest(t, h, "/PutTime", "")
@ -193,7 +193,7 @@ func TestTime(t *testing.T) {
} }
func TestBytes(t *testing.T) { func TestBytes(t *testing.T) {
m := Manage(engine.New()) m := Manage(memstore.New())
h := m(testServeMux) h := m(testServeMux)
_, body, cookie := testRequest(t, h, "/PutBytes", "") _, body, cookie := testRequest(t, h, "/PutBytes", "")
@ -231,7 +231,7 @@ func TestBytes(t *testing.T) {
} }
func TestObject(t *testing.T) { func TestObject(t *testing.T) {
m := Manage(engine.New()) m := Manage(memstore.New())
h := m(testServeMux) h := m(testServeMux)
_, body, cookie := testRequest(t, h, "/PutObject", "") _, body, cookie := testRequest(t, h, "/PutObject", "")
@ -270,7 +270,7 @@ func TestObject(t *testing.T) {
} }
func TestRemove(t *testing.T) { func TestRemove(t *testing.T) {
m := Manage(engine.New()) m := Manage(memstore.New())
h := m(testServeMux) h := m(testServeMux)
_, _, cookie := testRequest(t, h, "/PutString", "") _, _, cookie := testRequest(t, h, "/PutString", "")
@ -293,7 +293,7 @@ func TestRemove(t *testing.T) {
} }
func TestClear(t *testing.T) { func TestClear(t *testing.T) {
m := Manage(engine.New()) m := Manage(memstore.New())
h := m(testServeMux) h := m(testServeMux)
_, _, cookie := testRequest(t, h, "/PutString", "") _, _, cookie := testRequest(t, h, "/PutString", "")

View File

@ -4,11 +4,11 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/alexedwards/scs/mem/engine" "github.com/alexedwards/scs/engine/memstore"
) )
func TestWriteResponse(t *testing.T) { func TestWriteResponse(t *testing.T) {
m := Manage(engine.New()) m := Manage(memstore.New())
h := m(testServeMux) h := m(testServeMux)
code, _, _ := testRequest(t, h, "/WriteHeader", "") code, _, _ := testRequest(t, h, "/WriteHeader", "")
@ -18,9 +18,9 @@ func TestWriteResponse(t *testing.T) {
} }
func TestManagerOptionsLeak(t *testing.T) { func TestManagerOptionsLeak(t *testing.T) {
_ = Manage(engine.New(), Domain("example.org")) _ = Manage(memstore.New(), Domain("example.org"))
m := Manage(engine.New()) m := Manage(memstore.New())
h := m(testServeMux) h := m(testServeMux)
_, _, cookie := testRequest(t, h, "/PutString", "") _, _, cookie := testRequest(t, h, "/PutString", "")
if strings.Contains(cookie, "example.org") == true { if strings.Contains(cookie, "example.org") == true {

View File

@ -9,11 +9,11 @@ import (
"testing" "testing"
"time" "time"
"github.com/alexedwards/scs/mem/engine" "github.com/alexedwards/scs/engine/memstore"
) )
func TestCookieOptions(t *testing.T) { func TestCookieOptions(t *testing.T) {
m := Manage(engine.New()) m := Manage(memstore.New())
h := m(testServeMux) h := m(testServeMux)
_, _, cookie := testRequest(t, h, "/PutString", "") _, _, cookie := testRequest(t, h, "/PutString", "")
@ -30,7 +30,7 @@ func TestCookieOptions(t *testing.T) {
t.Fatalf("got %q: expected to contain %q", cookie, "HttpOnly") t.Fatalf("got %q: expected to contain %q", cookie, "HttpOnly")
} }
m = Manage(engine.New(), Path("/foo"), Domain("example.org"), Secure(true), HttpOnly(false), Lifetime(time.Hour), Persist(true)) m = Manage(memstore.New(), Path("/foo"), Domain("example.org"), Secure(true), HttpOnly(false), Lifetime(time.Hour), Persist(true))
h = m(testServeMux) h = m(testServeMux)
_, _, cookie = testRequest(t, h, "/PutString", "") _, _, cookie = testRequest(t, h, "/PutString", "")
@ -53,7 +53,7 @@ func TestCookieOptions(t *testing.T) {
t.Fatalf("got %q: expected to contain %q:", cookie, "Expires") t.Fatalf("got %q: expected to contain %q:", cookie, "Expires")
} }
m = Manage(engine.New(), Lifetime(time.Hour)) m = Manage(memstore.New(), Lifetime(time.Hour))
h = m(testServeMux) h = m(testServeMux)
_, _, cookie = testRequest(t, h, "/PutString", "") _, _, cookie = testRequest(t, h, "/PutString", "")
@ -66,7 +66,7 @@ func TestCookieOptions(t *testing.T) {
} }
func TestLifetime(t *testing.T) { func TestLifetime(t *testing.T) {
e := engine.New() e := memstore.New()
m := Manage(e, Lifetime(200*time.Millisecond)) m := Manage(e, Lifetime(200*time.Millisecond))
h := m(testServeMux) h := m(testServeMux)
@ -89,7 +89,7 @@ func TestLifetime(t *testing.T) {
} }
func TestIdleTimeout(t *testing.T) { func TestIdleTimeout(t *testing.T) {
e := engine.New() e := memstore.New()
m := Manage(e, IdleTimeout(100*time.Millisecond), Lifetime(500*time.Millisecond)) m := Manage(e, IdleTimeout(100*time.Millisecond), Lifetime(500*time.Millisecond))
h := m(testServeMux) h := m(testServeMux)
@ -126,7 +126,7 @@ func TestIdleTimeout(t *testing.T) {
} }
func TestErrorFunc(t *testing.T) { func TestErrorFunc(t *testing.T) {
m := Manage(engine.New()) m := Manage(memstore.New())
man, ok := m(nil).(*manager) man, ok := m(nil).(*manager)
if ok == false { if ok == false {
t.Fatal("type assertion failed") t.Fatal("type assertion failed")
@ -141,7 +141,7 @@ func TestErrorFunc(t *testing.T) {
t.Fatalf("got %q: expected %q", string(rr.Body.Bytes()), "test error\n") t.Fatalf("got %q: expected %q", string(rr.Body.Bytes()), "test error\n")
} }
m = Manage(engine.New(), ErrorFunc(func(w http.ResponseWriter, r *http.Request, err error) { m = Manage(memstore.New(), ErrorFunc(func(w http.ResponseWriter, r *http.Request, err error) {
w.WriteHeader(418) w.WriteHeader(418)
io.WriteString(w, http.StatusText(418)) io.WriteString(w, http.StatusText(418))
})) }))
@ -161,7 +161,7 @@ func TestErrorFunc(t *testing.T) {
} }
func TestPersist(t *testing.T) { func TestPersist(t *testing.T) {
e := engine.New() e := memstore.New()
m := Manage(e, IdleTimeout(5*time.Minute), Persist(true)) m := Manage(e, IdleTimeout(5*time.Minute), Persist(true))
h := m(testServeMux) h := m(testServeMux)
@ -175,7 +175,7 @@ func TestCookieName(t *testing.T) {
oldCookieName := CookieName oldCookieName := CookieName
CookieName = "custom_cookie_name" CookieName = "custom_cookie_name"
m := Manage(engine.New()) m := Manage(memstore.New())
h := m(testServeMux) h := m(testServeMux)
_, _, cookie := testRequest(t, h, "/PutString", "") _, _, cookie := testRequest(t, h, "/PutString", "")
@ -195,7 +195,7 @@ func TestContextDataName(t *testing.T) {
oldContextName := ContextName oldContextName := ContextName
ContextName = "custom_context_name" ContextName = "custom_context_name"
m := Manage(engine.New()) m := Manage(memstore.New())
h := m(testServeMux) h := m(testServeMux)
_, _, cookie := testRequest(t, h, "/PutString", "") _, _, cookie := testRequest(t, h, "/PutString", "")

View File

@ -11,7 +11,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/alexedwards/scs/mem/engine" "github.com/alexedwards/scs/engine/memstore"
) )
var testServeMux *http.ServeMux var testServeMux *http.ServeMux
@ -346,7 +346,7 @@ func TestGenerateToken(t *testing.T) {
} }
func TestDestroy(t *testing.T) { func TestDestroy(t *testing.T) {
e := engine.New() e := memstore.New()
m := Manage(e) m := Manage(e)
h := m(testServeMux) h := m(testServeMux)
@ -384,7 +384,7 @@ func TestDestroy(t *testing.T) {
} }
func TestRegenerateToken(t *testing.T) { func TestRegenerateToken(t *testing.T) {
e := engine.New() e := memstore.New()
m := Manage(e) m := Manage(e)
h := m(testServeMux) h := m(testServeMux)
@ -411,7 +411,7 @@ func TestRegenerateToken(t *testing.T) {
} }
func TestRenew(t *testing.T) { func TestRenew(t *testing.T) {
e := engine.New() e := memstore.New()
m := Manage(e) m := Manage(e)
h := m(testServeMux) h := m(testServeMux)