Skip to content

Add WithMaxAPIVersion Option to NginxClient #380

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

Merged
merged 11 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
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
56 changes: 56 additions & 0 deletions client/nginx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,62 @@ func TestClientWithHTTPClient(t *testing.T) {
}
}

func TestClientWithMaxAPI(t *testing.T) {
t.Parallel()

tests := []struct {
apiVersions string
expected int
}{
{
apiVersions: `[4, 5, 6, 7, 8, 9, 25]`,
expected: APIVersion,
},
{
apiVersions: ``,
expected: APIVersion,
},
{
apiVersions: `[4, 5, 6, 7]`,
expected: 7,
},
{
apiVersions: `[""]`,
expected: APIVersion,
},
}

for _, test := range tests {
// 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(test.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 != test.expected {
t.Fatalf("expected client.apiVersion to be %v, but got %v", test.expected, client.apiVersion)
}
}
}

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