88 "strings"
99 "sync"
1010 "testing"
11- "time"
1211)
1312
1413func TestDetermineUpdates (t * testing.T ) {
@@ -581,27 +580,6 @@ func TestClientWithAPIVersion(t *testing.T) {
581580 }
582581}
583582
584- func TestClientWithTimeout (t * testing.T ) {
585- t .Parallel ()
586- // Test creating a new client with a supported API version on the client
587- client , err := NewNginxClient ("http://api-url" , WithTimeout (1 * time .Second ))
588- if err != nil {
589- t .Fatalf ("unexpected error: %v" , err )
590- }
591- if client == nil {
592- t .Fatalf ("client is nil" )
593- }
594-
595- // Test creating a new client with an invalid duration
596- client , err = NewNginxClient ("http://api-url" , WithTimeout (- 1 * time .Second ))
597- if err == nil {
598- t .Fatalf ("expected error, but got nil" )
599- }
600- if client != nil {
601- t .Fatalf ("expected client to be nil, but got %v" , client )
602- }
603- }
604-
605583func TestClientWithHTTPClient (t * testing.T ) {
606584 t .Parallel ()
607585 // Test creating a new client passing a custom HTTP client
@@ -687,21 +665,6 @@ func TestClientWithMaxAPI(t *testing.T) {
687665}
688666
689667func TestGetStats_NoStreamEndpoint (t * testing.T ) {
690- tests := []struct {
691- ctx context.Context
692- name string
693- }{
694- {
695- ctx : nil ,
696- name : "no context test" ,
697- },
698- {
699- ctx : context .Background (),
700- name : "with context test" ,
701- },
702- }
703- var err error
704- var client * NginxClient
705668 var writeLock sync.Mutex
706669
707670 t .Parallel ()
@@ -712,19 +675,19 @@ func TestGetStats_NoStreamEndpoint(t *testing.T) {
712675 switch {
713676 case r .RequestURI == "/" :
714677
715- _ , err = w .Write ([]byte (`[4, 5, 6, 7, 8, 9]` ))
678+ _ , err : = w .Write ([]byte (`[4, 5, 6, 7, 8, 9]` ))
716679 if err != nil {
717680 t .Fatalf ("unexpected error: %v" , err )
718681 }
719682 case r .RequestURI == "/7/" :
720- _ , err = w .Write ([]byte (`["nginx","processes","connections","slabs","http","resolvers","ssl"]` ))
683+ _ , err : = w .Write ([]byte (`["nginx","processes","connections","slabs","http","resolvers","ssl"]` ))
721684 if err != nil {
722685 t .Fatalf ("unexpected error: %v" , err )
723686 }
724687 case strings .HasPrefix (r .RequestURI , "/7/stream" ):
725688 t .Fatal ("Stream endpoint should not be called since it does not exist." )
726689 default :
727- _ , err = w .Write ([]byte (`{}` ))
690+ _ , err : = w .Write ([]byte (`{}` ))
728691 if err != nil {
729692 t .Fatalf ("unexpected error: %v" , err )
730693 }
@@ -733,27 +696,17 @@ func TestGetStats_NoStreamEndpoint(t *testing.T) {
733696 defer ts .Close ()
734697
735698 // Test creating a new client with a supported API version on the server
736- client , err = NewNginxClient (ts .URL , WithAPIVersion (7 ), WithCheckAPI ())
699+ client , err : = NewNginxClient (ts .URL , WithAPIVersion (7 ), WithCheckAPI ())
737700 if err != nil {
738701 t .Fatalf ("unexpected error: %v" , err )
739702 }
740703 if client == nil {
741704 t .Fatalf ("client is nil" )
742705 }
743706
744- var stats * Stats
745- for _ , test := range tests {
746- if test .ctx == nil {
747- stats , err = client .GetStats ()
748- if err != nil {
749- t .Fatalf ("unexpected error: %v" , err )
750- }
751- } else {
752- stats , err = client .GetStatsWithContext (test .ctx )
753- if err != nil {
754- t .Fatalf ("unexpected error: %v" , err )
755- }
756- }
707+ stats , err := client .GetStats (context .Background ())
708+ if err != nil {
709+ t .Fatalf ("unexpected error: %v" , err )
757710 }
758711
759712 if ! reflect .DeepEqual (stats .StreamServerZones , StreamServerZones {}) {
@@ -771,20 +724,6 @@ func TestGetStats_NoStreamEndpoint(t *testing.T) {
771724}
772725
773726func TestGetStats_SSL (t * testing.T ) {
774- tests := []struct {
775- ctx context.Context
776- name string
777- }{
778- {
779- ctx : nil ,
780- name : "no context test" ,
781- },
782- {
783- ctx : context .Background (),
784- name : "with context test" ,
785- },
786- }
787-
788727 t .Parallel ()
789728 ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
790729 switch {
@@ -841,41 +780,30 @@ func TestGetStats_SSL(t *testing.T) {
841780 t .Fatalf ("client is nil" )
842781 }
843782
844- var stats * Stats
845-
846- for _ , test := range tests {
847- if test .ctx == nil {
848- stats , err = client .GetStats ()
849- if err != nil {
850- t .Fatalf ("unexpected error: %v" , err )
851- }
852- } else {
853- stats , err = client .GetStatsWithContext (test .ctx )
854- if err != nil {
855- t .Fatalf ("unexpected error: %v" , err )
856- }
857- }
783+ stats , err := client .GetStats (context .Background ())
784+ if err != nil {
785+ t .Fatalf ("unexpected error: %v" , err )
786+ }
858787
859- testStats := SSL {
860- Handshakes : 79572 ,
861- HandshakesFailed : 21025 ,
862- SessionReuses : 15762 ,
863- NoCommonProtocol : 4 ,
864- NoCommonCipher : 2 ,
865- HandshakeTimeout : 0 ,
866- PeerRejectedCert : 0 ,
867- VerifyFailures : VerifyFailures {
868- NoCert : 0 ,
869- ExpiredCert : 2 ,
870- RevokedCert : 1 ,
871- HostnameMismatch : 2 ,
872- Other : 1 ,
873- },
874- }
788+ testStats := SSL {
789+ Handshakes : 79572 ,
790+ HandshakesFailed : 21025 ,
791+ SessionReuses : 15762 ,
792+ NoCommonProtocol : 4 ,
793+ NoCommonCipher : 2 ,
794+ HandshakeTimeout : 0 ,
795+ PeerRejectedCert : 0 ,
796+ VerifyFailures : VerifyFailures {
797+ NoCert : 0 ,
798+ ExpiredCert : 2 ,
799+ RevokedCert : 1 ,
800+ HostnameMismatch : 2 ,
801+ Other : 1 ,
802+ },
803+ }
875804
876- if ! reflect .DeepEqual (stats .SSL , testStats ) {
877- t .Fatalf ("SSL stats: expected %v, actual %v" , testStats , stats .SSL )
878- }
805+ if ! reflect .DeepEqual (stats .SSL , testStats ) {
806+ t .Fatalf ("SSL stats: expected %v, actual %v" , testStats , stats .SSL )
879807 }
880808}
881809
@@ -902,7 +830,7 @@ func TestGetMaxAPIVersionServer(t *testing.T) {
902830 t .Fatalf ("unexpected error: %v" , err )
903831 }
904832
905- maxVer , err := c .GetMaxAPIVersion ()
833+ maxVer , err := c .GetMaxAPIVersion (context . Background () )
906834 if err != nil {
907835 t .Fatalf ("unexpected error: %v" , err )
908836 }
@@ -934,7 +862,7 @@ func TestGetMaxAPIVersionClient(t *testing.T) {
934862 t .Fatalf ("unexpected error: %v" , err )
935863 }
936864
937- maxVer , err := c .GetMaxAPIVersion ()
865+ maxVer , err := c .GetMaxAPIVersion (context . Background () )
938866 if err != nil {
939867 t .Fatalf ("unexpected error: %v" , err )
940868 }
0 commit comments