@@ -765,6 +765,66 @@ func TestGetStats_NoStreamEndpoint(t *testing.T) {
765765 }
766766}
767767
768+ func TestGetStats_Connections (t * testing.T ) {
769+ t .Parallel ()
770+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
771+ switch {
772+ case r .RequestURI == "/" :
773+ _ , err := w .Write ([]byte (`[4, 5, 6, 7, 8, 9]` ))
774+ if err != nil {
775+ t .Fatalf ("unexpected error: %v" , err )
776+ }
777+ case r .RequestURI == "/8/" :
778+ _ , err := w .Write ([]byte (`["nginx","processes","connections","slabs","http","resolvers","ssl","workers"]` ))
779+ if err != nil {
780+ t .Fatalf ("unexpected error: %v" , err )
781+ }
782+ case strings .HasPrefix (r .RequestURI , "/8/connections" ):
783+ _ , err := w .Write ([]byte (`{
784+ "active": -1,
785+ "idle": 3,
786+ "accepted": 5,
787+ "dropped": 2
788+ }` ))
789+ if err != nil {
790+ t .Fatalf ("unexpected error: %v" , err )
791+ }
792+ default :
793+ _ , err := w .Write ([]byte (`{}` ))
794+ if err != nil {
795+ t .Fatalf ("unexpected error: %v" , err )
796+ }
797+ }
798+ }))
799+
800+ defer ts .Close ()
801+
802+ // Test creating a new client with a supported API version on the server
803+ client , err := NewNginxClient (ts .URL , WithAPIVersion (8 ), WithCheckAPI ())
804+ if err != nil {
805+ t .Fatalf ("unexpected error: %v" , err )
806+ }
807+ if client == nil {
808+ t .Fatalf ("client is nil" )
809+ }
810+
811+ stats , err := client .GetStats (context .Background ())
812+ if err != nil {
813+ t .Fatalf ("unexpected error: %v" , err )
814+ }
815+
816+ testStats := Connections {
817+ Accepted : 5 ,
818+ Dropped : 2 ,
819+ Active : - 1 ,
820+ Idle : 3 ,
821+ }
822+
823+ if ! reflect .DeepEqual (stats .Connections , testStats ) {
824+ t .Fatalf ("Connection stats: expected %v, actual %v" , testStats , stats .Connections )
825+ }
826+ }
827+
768828func TestGetStats_SSL (t * testing.T ) {
769829 t .Parallel ()
770830 ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
0 commit comments