Browse Source

Added the pass through to the next handler.

master
Jeff Baskin 4 years ago
parent
commit
fa36de71ed
  1. 33
      sessionmanager.go
  2. 26
      sessionmanager_test.go

33
sessionmanager.go

@ -19,6 +19,10 @@ const (
sessionList
)
type pageGenerator interface {
ServeHTTP(http.ResponseWriter, *http.Request)
}
type sessionMessage struct {
action sessionAction
sessionID string
@ -33,24 +37,26 @@ type sessionInfo struct {
}
type sessionManager struct {
msgIn chan sessionMessage
msgOut chan sessionMessage
store map[string]sessionInfo
maxAge time.Duration
check time.Duration
timer *time.Timer
msgIn chan sessionMessage
msgOut chan sessionMessage
handler pageGenerator
store map[string]sessionInfo
maxAge time.Duration
check time.Duration
timer *time.Timer
}
func newSessionManager() *sessionManager {
func newSessionManager(handler pageGenerator) *sessionManager {
maxAge := 24 * time.Hour
check := time.Hour
sm := &sessionManager{
msgIn: make(chan sessionMessage),
msgOut: make(chan sessionMessage),
store: make(map[string]sessionInfo),
maxAge: maxAge,
check: check,
timer: time.NewTimer(check),
msgIn: make(chan sessionMessage),
msgOut: make(chan sessionMessage),
handler: handler,
store: make(map[string]sessionInfo),
maxAge: maxAge,
check: check,
timer: time.NewTimer(check),
}
go func() {
for msg := range sm.msgIn {
@ -199,4 +205,5 @@ func (sm *sessionManager) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.SetCookie(w, sm.createCookie(sname, sessid))
}
http.SetCookie(w, sm.createCookie(rname, reqid))
sm.handler.ServeHTTP(w, r)
}

26
sessionmanager_test.go

@ -5,6 +5,8 @@ package proxy
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/http/httptest"
@ -25,10 +27,17 @@ type webRig struct {
}
const smTestLoopCount = 1000
const bodyString = "Testing!"
type smTestHandler struct{}
func (th smTestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, bodyString)
}
func newTestServer() *testServer {
ts := &testServer{
sm: newSessionManager(),
sm: newSessionManager(smTestHandler{}),
msgStore: make(chan sessionMessage, 10),
}
go func() {
@ -42,7 +51,7 @@ func newTestServer() *testServer {
}
func newBenchmarkServer() *sessionManager {
sm := newSessionManager()
sm := newSessionManager(smTestHandler{})
go func() {
for msg := range sm.msgOut {
sm.msgIn <- msg
@ -574,3 +583,16 @@ func TestBadRequestCookie(t *testing.T) {
t.Error("The bad session did not get deleted.")
}
}
func TestPagePassThrough(t *testing.T) {
wr := newWebRig(t)
res := wr.get(t, wr.ws.URL)
data, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
holder := string(data)
if holder != bodyString {
t.Errorf("Expected the body to be '%s', but got '%s'.", bodyString, holder)
}
}
Loading…
Cancel
Save