Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 13 additions & 0 deletions client/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,19 @@ func WithCheckAPI() Option {
}
}

// WithMaxAPIVersion sets the API version to the max API version.
func WithMaxAPIVersion() Option {
return func(o *NginxClient) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
version, err := o.GetMaxAPIVersion(ctx)
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 @@ -601,6 +601,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) {
var writeLock sync.Mutex

Expand Down
Loading