Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Donal Hurley committed Oct 6, 2023
1 parent 010207b commit 6a14655
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions client/nginx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
)

Expand Down Expand Up @@ -589,3 +590,54 @@ func TestClientWithHTTPClient(t *testing.T) {
t.Fatalf("expected client to be nil, but got %v", client)
}
}

func TestGetStats_NoStreamEndpoint(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/" {
_, err := w.Write([]byte(`[4, 5, 6, 7]`))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
} else if r.RequestURI == "/7/" {
_, err := w.Write([]byte(`["nginx","processes","connections","slabs","http","resolvers","ssl"]`))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
} else if strings.HasPrefix(r.RequestURI, "/7/stream") {
t.Fatal("Stream endpoint should not be called since it does not exist.")
} else {
_, err := w.Write([]byte(`{}`))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
}))
defer ts.Close()

// Test creating a new client with a supported API version on the server
client, err := NewNginxClient(ts.URL, WithAPIVersion(7), WithCheckAPI())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if client == nil {
t.Fatalf("client is nil")
}

stats, err := client.GetStats()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

if !reflect.DeepEqual(stats.StreamServerZones, StreamServerZones{}) {
t.Fatalf("StreamServerZones: expected %v, actual %v", StreamServerZones{}, stats.StreamServerZones)
}
if !reflect.DeepEqual(stats.StreamLimitConnections, StreamLimitConnections{}) {
t.Fatalf("StreamLimitConnections: expected %v, actual %v", StreamLimitConnections{}, stats.StreamLimitConnections)
}
if !reflect.DeepEqual(stats.StreamUpstreams, StreamUpstreams{}) {
t.Fatalf("StreamUpstreams: expected %v, actual %v", StreamUpstreams{}, stats.StreamUpstreams)
}
if !reflect.DeepEqual(stats.StreamZoneSync, &StreamZoneSync{}) {
t.Fatalf("StreamZoneSync: expected %v, actual %v", &StreamZoneSync{}, stats.StreamZoneSync)
}
}

0 comments on commit 6a14655

Please sign in to comment.