1
0
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:
Alex Edwards
2024-02-03 18:44:19 +01:00
parent cef4b05350
commit a38e822451
2 changed files with 79 additions and 0 deletions

18
session_go120.go Normal file
View 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
View 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)
}
}