mirror of
https://github.com/alexedwards/scs.git
synced 2025-07-15 01:04:36 +02:00
Support Hijacker and Flusher interfaces for Go 1.20+
This commit is contained in:
18
session_go120.go
Normal file
18
session_go120.go
Normal file
@ -0,0 +1,18 @@
|
||||
//go:build go1.20
|
||||
// +build go1.20
|
||||
|
||||
package scs
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"net"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (sw *sessionResponseWriter) Flush() {
|
||||
http.NewResponseController(sw.ResponseWriter).Flush()
|
||||
}
|
||||
|
||||
func (sw *sessionResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
return http.NewResponseController(sw.ResponseWriter).Hijack()
|
||||
}
|
61
session_go120_test.go
Normal file
61
session_go120_test.go
Normal file
@ -0,0 +1,61 @@
|
||||
//go:build go1.20
|
||||
// +build go1.20
|
||||
|
||||
package scs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestFlusher(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
sessionManager := New()
|
||||
sessionManager.Lifetime = 500 * time.Millisecond
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("/get", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, ok := w.(http.Flusher)
|
||||
|
||||
fmt.Fprint(w, ok)
|
||||
}))
|
||||
|
||||
ts := newTestServer(t, sessionManager.LoadAndSave(mux))
|
||||
defer ts.Close()
|
||||
|
||||
ts.execute(t, "/put")
|
||||
|
||||
_, body := ts.execute(t, "/get")
|
||||
if body != "true" {
|
||||
t.Errorf("want %q; got %q", "true", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHijacker(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
sessionManager := New()
|
||||
sessionManager.Lifetime = 500 * time.Millisecond
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("/get", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, ok := w.(http.Hijacker)
|
||||
|
||||
fmt.Fprint(w, ok)
|
||||
}))
|
||||
|
||||
ts := newTestServer(t, sessionManager.LoadAndSave(mux))
|
||||
defer ts.Close()
|
||||
|
||||
ts.execute(t, "/put")
|
||||
|
||||
_, body := ts.execute(t, "/get")
|
||||
if body != "true" {
|
||||
t.Errorf("want %q; got %q", "true", body)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user