@@ -1441,6 +1441,58 @@ func TestContextRenderSSE(t *testing.T) {
14411441 assert .Equal (t , strings .ReplaceAll (w .Body .String (), " " , "" ), strings .ReplaceAll ("event:float\n data:1.5\n \n id:123\n data:text\n \n event:chat\n data:{\" bar\" :\" foo\" ,\" foo\" :\" bar\" }\n \n " , " " , "" ))
14421442}
14431443
1444+ func TestContextInitSSE (t * testing.T ) {
1445+ w := httptest .NewRecorder ()
1446+ c , _ := CreateTestContext (w )
1447+ c .Request , _ = http .NewRequest (http .MethodGet , "/" , nil )
1448+
1449+ c .InitSSE ()
1450+
1451+ assert .Equal (t , sse .ContentType , w .Header ().Get ("Content-Type" ))
1452+ assert .Equal (t , "no-cache" , w .Header ().Get ("Cache-Control" ))
1453+ assert .Equal (t , "keep-alive" , w .Header ().Get ("Connection" ))
1454+ assert .Equal (t , http .StatusOK , w .Code )
1455+ }
1456+
1457+ func TestContextSSEStreamNormalEnd (t * testing.T ) {
1458+ w := httptest .NewRecorder ()
1459+ c , _ := CreateTestContext (w )
1460+ c .Request , _ = http .NewRequest (http .MethodGet , "/" , nil )
1461+
1462+ count := 0
1463+ disconnected := c .SSEStream (func (c * Context ) bool {
1464+ count ++
1465+ c .SSEvent ("ping" , count )
1466+ return count < 3
1467+ })
1468+
1469+ assert .False (t , disconnected )
1470+ assert .Equal (t , 3 , count )
1471+ assert .Equal (t , sse .ContentType , w .Header ().Get ("Content-Type" ))
1472+ assert .Equal (t , "no-cache" , w .Header ().Get ("Cache-Control" ))
1473+ assert .Equal (t , "keep-alive" , w .Header ().Get ("Connection" ))
1474+ assert .Contains (t , w .Body .String (), "event:ping" )
1475+ }
1476+
1477+ func TestContextSSEStreamClientDisconnect (t * testing.T ) {
1478+ w := httptest .NewRecorder ()
1479+ c , _ := CreateTestContext (w )
1480+
1481+ ctx , cancel := context .WithCancel (context .Background ())
1482+ defer cancel ()
1483+ c .Request , _ = http .NewRequestWithContext (ctx , http .MethodGet , "/" , nil )
1484+
1485+ // step watches its own context so the result is deterministic:
1486+ // cancel() guarantees Done() is closed before the receive.
1487+ result := c .SSEStream (func (c * Context ) bool {
1488+ cancel () // trigger cancellation
1489+ <- c .Request .Context ().Done ()
1490+ return false // step returns false → SSEStream returns false
1491+ })
1492+
1493+ assert .False (t , result )
1494+ }
1495+
14441496func TestContextRenderFile (t * testing.T ) {
14451497 w := httptest .NewRecorder ()
14461498 c , _ := CreateTestContext (w )
@@ -3030,10 +3082,6 @@ func (r *TestResponseRecorder) CloseNotify() <-chan bool {
30303082 return r .closeChannel
30313083}
30323084
3033- func (r * TestResponseRecorder ) closeClient () {
3034- r .closeChannel <- true
3035- }
3036-
30373085func CreateTestResponseRecorder () * TestResponseRecorder {
30383086 return & TestResponseRecorder {
30393087 httptest .NewRecorder (),
@@ -3044,6 +3092,7 @@ func CreateTestResponseRecorder() *TestResponseRecorder {
30443092func TestContextStream (t * testing.T ) {
30453093 w := CreateTestResponseRecorder ()
30463094 c , _ := CreateTestContext (w )
3095+ c .Request , _ = http .NewRequest (http .MethodGet , "/" , nil )
30473096
30483097 stopStream := true
30493098 c .Stream (func (w io.Writer ) bool {
@@ -3064,17 +3113,21 @@ func TestContextStreamWithClientGone(t *testing.T) {
30643113 w := CreateTestResponseRecorder ()
30653114 c , _ := CreateTestContext (w )
30663115
3067- c .Stream (func (writer io.Writer ) bool {
3068- defer func () {
3069- w .closeClient ()
3070- }()
3116+ ctx , cancel := context .WithCancel (context .Background ())
3117+ defer cancel ()
3118+ c .Request , _ = http .NewRequestWithContext (ctx , http .MethodGet , "/" , nil )
30713119
3120+ // step detects ctx cancellation via a direct channel receive and returns false,
3121+ // so Stream terminates. This tests the context-based disconnect path.
3122+ result := c .Stream (func (writer io.Writer ) bool {
30723123 _ , err := writer .Write ([]byte ("test" ))
30733124 require .NoError (t , err )
3074-
3075- return true
3125+ cancel ()
3126+ <- ctx .Done ()
3127+ return false
30763128 })
30773129
3130+ assert .False (t , result )
30783131 assert .Equal (t , "test" , w .Body .String ())
30793132}
30803133
0 commit comments