forked from alexedwards/scs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Hijacker and Flusher interfaces for Go 1.20+
- Loading branch information
1 parent
cef4b05
commit a38e822
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |