Skip to content

Commit

Permalink
Support Hijacker and Flusher interfaces for Go 1.20+
Browse files Browse the repository at this point in the history
  • Loading branch information
alexedwards committed Feb 3, 2024
1 parent cef4b05 commit a38e822
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
18 changes: 18 additions & 0 deletions session_go120.go
Original file line number Diff line number Diff line change
@@ -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 changes: 61 additions & 0 deletions session_go120_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit a38e822

Please sign in to comment.