Skip to content

Commit 1062d6a

Browse files
authored
Add WithMaxAPIVersion Option to NginxClient (#380)
* Added WithMaxAPIVersion option to NewNginxClient so that the Max API version can be set by a function
1 parent 09790f5 commit 1062d6a

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@
55
# Visual Studio Code settings
66
.vscode
77

8+
# Goland settings
9+
.idea/
10+
811
dist

Diff for: client/nginx.go

+13
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,19 @@ func WithCheckAPI() Option {
588588
}
589589
}
590590

591+
// WithMaxAPIVersion sets the API version to the max API version.
592+
func WithMaxAPIVersion() Option {
593+
return func(o *NginxClient) {
594+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
595+
defer cancel()
596+
version, err := o.GetMaxAPIVersion(ctx)
597+
if err != nil {
598+
return
599+
}
600+
o.apiVersion = version
601+
}
602+
}
603+
591604
// NewNginxClient creates a new NginxClient.
592605
func NewNginxClient(apiEndpoint string, opts ...Option) (*NginxClient, error) {
593606
c := &NginxClient{

Diff for: client/nginx_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,69 @@ func TestClientWithHTTPClient(t *testing.T) {
601601
}
602602
}
603603

604+
func TestClientWithMaxAPI(t *testing.T) {
605+
t.Parallel()
606+
tests := []struct {
607+
name string
608+
apiVersions string
609+
expected int
610+
}{
611+
{
612+
name: "Test 1: API versions contains invalid version",
613+
apiVersions: `[4, 5, 6, 7, 8, 9, 25]`,
614+
expected: APIVersion,
615+
},
616+
{
617+
name: "Test 2: No API versions, default API Version is used",
618+
apiVersions: ``,
619+
expected: APIVersion,
620+
},
621+
{
622+
name: "Test 3: API version lower than default",
623+
apiVersions: `[4, 5, 6, 7]`,
624+
expected: 7,
625+
},
626+
{
627+
name: "Test 4: No API versions, default API version is used",
628+
apiVersions: `[""]`,
629+
expected: APIVersion,
630+
},
631+
}
632+
633+
for _, tt := range tests {
634+
t.Run(tt.name, func(t *testing.T) {
635+
t.Parallel()
636+
// Test creating a new client with max API version
637+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
638+
switch {
639+
case r.RequestURI == "/":
640+
_, err := w.Write([]byte(tt.apiVersions))
641+
if err != nil {
642+
t.Fatalf("unexpected error: %v", err)
643+
}
644+
default:
645+
_, err := w.Write([]byte(`{}`))
646+
if err != nil {
647+
t.Fatalf("unexpected error: %v", err)
648+
}
649+
}
650+
}))
651+
defer ts.Close()
652+
653+
client, err := NewNginxClient(ts.URL, WithMaxAPIVersion())
654+
if err != nil {
655+
t.Fatalf("unexpected error: %v", err)
656+
}
657+
if client == nil {
658+
t.Fatalf("client is nil")
659+
}
660+
if client.apiVersion != tt.expected {
661+
t.Fatalf("expected client.apiVersion to be %v, but got %v", tt.expected, client.apiVersion)
662+
}
663+
})
664+
}
665+
}
666+
604667
func TestGetStats_NoStreamEndpoint(t *testing.T) {
605668
var writeLock sync.Mutex
606669

0 commit comments

Comments
 (0)