Skip to content

Commit 4d582dd

Browse files
committed
Added WithMacAPIVersion option to NewNginxClient
1 parent 8084b43 commit 4d582dd

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

client/nginx.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,17 @@ func WithTimeout(duration time.Duration) Option {
597597
}
598598
}
599599

600+
// WithMaxAPIVersion sets the flag to use the max API version.
601+
func WithMaxAPIVersion() Option {
602+
return func(o *NginxClient) {
603+
version, err := o.GetMaxAPIVersion()
604+
if err != nil {
605+
return
606+
}
607+
o.apiVersion = version
608+
}
609+
}
610+
600611
// NewNginxClient creates a new NginxClient.
601612
func NewNginxClient(apiEndpoint string, opts ...Option) (*NginxClient, error) {
602613
c := &NginxClient{

client/nginx_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,63 @@ func TestClientWithHTTPClient(t *testing.T) {
623623
}
624624
}
625625

626+
func TestClientWithMaxAPI(t *testing.T) {
627+
t.Parallel()
628+
629+
tests := []struct {
630+
apiVersions string
631+
expected int
632+
}{
633+
{
634+
apiVersions: `[4, 5, 6, 7, 8, 9, 25]`,
635+
expected: APIVersion,
636+
},
637+
{
638+
apiVersions: ``,
639+
expected: APIVersion,
640+
},
641+
{
642+
apiVersions: `[4, 5, 6, 7]`,
643+
expected: 7,
644+
},
645+
{
646+
apiVersions: `[""]`,
647+
expected: APIVersion,
648+
},
649+
}
650+
651+
for _, test := range tests {
652+
// Test creating a new client with max API version
653+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
654+
switch {
655+
case r.RequestURI == "/":
656+
_, err := w.Write([]byte(test.apiVersions))
657+
if err != nil {
658+
t.Fatalf("unexpected error: %v", err)
659+
}
660+
default:
661+
_, err := w.Write([]byte(`{}`))
662+
if err != nil {
663+
t.Fatalf("unexpected error: %v", err)
664+
}
665+
}
666+
}))
667+
defer ts.Close()
668+
669+
client, err := NewNginxClient(ts.URL, WithMaxAPIVersion())
670+
if err != nil {
671+
t.Fatalf("unexpected error: %v", err)
672+
}
673+
if client == nil {
674+
t.Fatalf("client is nil")
675+
}
676+
if client.apiVersion != test.expected {
677+
t.Fatalf("expected client.apiVersion to be %v, but got %v", test.expected, client.apiVersion)
678+
}
679+
}
680+
681+
}
682+
626683
func TestGetStats_NoStreamEndpoint(t *testing.T) {
627684
tests := []struct {
628685
ctx context.Context

0 commit comments

Comments
 (0)