Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WithMaxAPIVersion Option to NginxClient #380

Merged
merged 11 commits into from
Sep 24, 2024
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
# Visual Studio Code settings
.vscode

# Goland settings
.idea/

dist
11 changes: 11 additions & 0 deletions client/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,17 @@ func WithTimeout(duration time.Duration) Option {
}
}

// WithMaxAPIVersion sets the API version to the max API version.
func WithMaxAPIVersion() Option {
return func(o *NginxClient) {
version, err := o.GetMaxAPIVersion()
if err != nil {
return
}
o.apiVersion = version
}
}

// NewNginxClient creates a new NginxClient.
func NewNginxClient(apiEndpoint string, opts ...Option) (*NginxClient, error) {
c := &NginxClient{
Expand Down
63 changes: 63 additions & 0 deletions client/nginx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,69 @@ func TestClientWithHTTPClient(t *testing.T) {
}
}

func TestClientWithMaxAPI(t *testing.T) {
t.Parallel()
tests := []struct {
name string
apiVersions string
expected int
}{
{
name: "Test 1: API versions contains invalid version",
apiVersions: `[4, 5, 6, 7, 8, 9, 25]`,
expected: APIVersion,
},
{
name: "Test 2: No API versions, default API Version is used",
apiVersions: ``,
expected: APIVersion,
},
{
name: "Test 3: API version lower than default",
apiVersions: `[4, 5, 6, 7]`,
expected: 7,
},
{
name: "Test 4: No API versions, default API version is used",
apiVersions: `[""]`,
expected: APIVersion,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
// Test creating a new client with max API version
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.RequestURI == "/":
_, err := w.Write([]byte(tt.apiVersions))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
default:
_, err := w.Write([]byte(`{}`))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
}))
defer ts.Close()

client, err := NewNginxClient(ts.URL, WithMaxAPIVersion())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if client == nil {
t.Fatalf("client is nil")
}
if client.apiVersion != tt.expected {
t.Fatalf("expected client.apiVersion to be %v, but got %v", tt.expected, client.apiVersion)
}
})
}
}

func TestGetStats_NoStreamEndpoint(t *testing.T) {
tests := []struct {
ctx context.Context
Expand Down
Loading