@@ -46,6 +46,7 @@ func TestEventSourceSimpleFlow(t *testing.T) {
46
46
defer ts .Close ()
47
47
48
48
es .SetURL (ts .URL )
49
+ es .SetMethod (MethodPost )
49
50
err := es .Get ()
50
51
assertNil (t , err )
51
52
assertEqual (t , counter , messageCounter )
@@ -115,6 +116,7 @@ func TestEventSourceMultipleEventTypes(t *testing.T) {
115
116
defer ts .Close ()
116
117
117
118
es .SetURL (ts .URL ).
119
+ SetMethod (MethodPost ).
118
120
AddEventListener ("user_connect" , userConnectFunc , userEvent {}).
119
121
AddEventListener ("user_message" , userMessageFunc , userEvent {})
120
122
@@ -354,6 +356,7 @@ func TestEventSourceCoverage(t *testing.T) {
354
356
func createEventSource (t * testing.T , url string , fn EventMessageFunc , rt any ) * EventSource {
355
357
es := NewEventSource ().
356
358
SetURL (url ).
359
+ SetMethod (MethodGet ).
357
360
AddHeader ("X-Test-Header-1" , "test header 1" ).
358
361
SetHeader ("X-Test-Header-2" , "test header 2" ).
359
362
SetRetryCount (2 ).
@@ -406,3 +409,154 @@ func createSSETestServer(t *testing.T, ticker time.Duration, fn func(io.Writer)
406
409
}
407
410
})
408
411
}
412
+
413
+ func TestEventSourceWithDifferentMethods (t * testing.T ) {
414
+ testCases := []struct {
415
+ name string
416
+ method string
417
+ body []byte
418
+ }{
419
+ {
420
+ name : "GET Method" ,
421
+ method : MethodGet ,
422
+ body : nil ,
423
+ },
424
+ {
425
+ name : "POST Method" ,
426
+ method : MethodPost ,
427
+ body : []byte (`{"test":"post_data"}` ),
428
+ },
429
+ {
430
+ name : "PUT Method" ,
431
+ method : MethodPut ,
432
+ body : []byte (`{"test":"put_data"}` ),
433
+ },
434
+ {
435
+ name : "DELETE Method" ,
436
+ method : MethodDelete ,
437
+ body : nil ,
438
+ },
439
+ {
440
+ name : "PATCH Method" ,
441
+ method : MethodPatch ,
442
+ body : []byte (`{"test":"patch_data"}` ),
443
+ },
444
+ }
445
+
446
+ for _ , tc := range testCases {
447
+ t .Run (tc .name , func (t * testing.T ) {
448
+ messageCounter := 0
449
+ messageFunc := func (e any ) {
450
+ event := e .(* Event )
451
+ assertEqual (t , strconv .Itoa (messageCounter ), event .ID )
452
+ assertEqual (t , true , strings .HasPrefix (event .Data , fmt .Sprintf ("%s method test:" , tc .method )))
453
+ messageCounter ++
454
+ }
455
+
456
+ counter := 0
457
+ methodVerified := false
458
+ bodyVerified := false
459
+
460
+ es := createEventSource (t , "" , messageFunc , nil )
461
+ ts := createMethodVerifyingSSETestServer (
462
+ t ,
463
+ 10 * time .Millisecond ,
464
+ tc .method ,
465
+ tc .body ,
466
+ & methodVerified ,
467
+ & bodyVerified ,
468
+ func (w io.Writer ) error {
469
+ if counter == 20 {
470
+ es .Close ()
471
+ return fmt .Errorf ("stop sending events" )
472
+ }
473
+ _ , err := fmt .Fprintf (w , "id: %v\n data: %s method test: %s\n \n " , counter , tc .method , time .Now ().Format (time .RFC3339 ))
474
+ counter ++
475
+ return err
476
+ },
477
+ )
478
+ defer ts .Close ()
479
+
480
+ es .SetURL (ts .URL )
481
+ es .SetMethod (tc .method )
482
+
483
+ // set body
484
+ if tc .body != nil {
485
+ es .SetBody (bytes .NewBuffer (tc .body ))
486
+ }
487
+
488
+ err := es .Get ()
489
+ assertNil (t , err )
490
+
491
+ // check the message count
492
+ assertEqual (t , counter , messageCounter )
493
+
494
+ // check if server receive correct method and body
495
+ assertEqual (t , true , methodVerified )
496
+ if tc .body != nil {
497
+ assertEqual (t , true , bodyVerified )
498
+ }
499
+ })
500
+ }
501
+ }
502
+
503
+ // almost like create server before but add verifying method and body
504
+ func createMethodVerifyingSSETestServer (
505
+ t * testing.T ,
506
+ ticker time.Duration ,
507
+ expectedMethod string ,
508
+ expectedBody []byte ,
509
+ methodVerified * bool ,
510
+ bodyVerified * bool ,
511
+ fn func (io.Writer ) error ,
512
+ ) * httptest.Server {
513
+ return httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
514
+ // validate method
515
+ if r .Method == expectedMethod {
516
+ * methodVerified = true
517
+ } else {
518
+ t .Errorf ("Expected method %s, got %s" , expectedMethod , r .Method )
519
+ }
520
+
521
+ // validate body
522
+ if expectedBody != nil {
523
+ body , err := io .ReadAll (r .Body )
524
+ if err != nil {
525
+ t .Errorf ("Failed to read request body: %v" , err )
526
+ } else if string (body ) == string (expectedBody ) {
527
+ * bodyVerified = true
528
+ } else {
529
+ t .Errorf ("Expected body %s, got %s" , string (expectedBody ), string (body ))
530
+ }
531
+ }
532
+
533
+ // same as createSSETestServer
534
+ w .Header ().Set ("Content-Type" , "text/event-stream" )
535
+ w .Header ().Set ("Cache-Control" , "no-cache" )
536
+ w .Header ().Set ("Connection" , "keep-alive" )
537
+ w .Header ().Set ("Access-Control-Allow-Origin" , "*" )
538
+
539
+ clientGone := r .Context ().Done ()
540
+
541
+ rc := http .NewResponseController (w )
542
+ tick := time .NewTicker (ticker )
543
+ defer tick .Stop ()
544
+
545
+ for {
546
+ select {
547
+ case <- clientGone :
548
+ t .Log ("Client disconnected" )
549
+ return
550
+ case <- tick .C :
551
+ if err := fn (w ); err != nil {
552
+ t .Log (err )
553
+ return
554
+ }
555
+ if err := rc .Flush (); err != nil {
556
+ t .Log (err )
557
+ return
558
+ }
559
+ }
560
+ }
561
+ }))
562
+ }
0 commit comments