Skip to content

Commit

Permalink
api: Ensure the internal/ui/service endpoint responds with an array (#…
Browse files Browse the repository at this point in the history
…9397)

In some circumstances this endpoint will have no results in it (dues to
ACLs, Namespaces or filtering).

This ensures that the response is at least an empty array (`[]`) rather
than `null`
  • Loading branch information
johncowen committed Dec 15, 2020
1 parent 7830eb8 commit a9913b5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion agent/ui_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ RPC:
summaries, hasProxy := summarizeServices(out.Nodes.ToServiceDump(), nil, "")
sorted := prepSummaryOutput(summaries, false)

var result []*ServiceListingSummary
// Ensure at least a zero length slice
result := make([]*ServiceListingSummary, 0)
for _, svc := range sorted {
sum := ServiceListingSummary{ServiceSummary: *svc}

Expand Down
15 changes: 15 additions & 0 deletions agent/ui_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,21 @@ func TestUiServices(t *testing.T) {
}
require.ElementsMatch(t, expected, summary)
})
t.Run("Filtered without results", func(t *testing.T) {
filterQuery := url.QueryEscape("Service.Service == absent")
req, _ := http.NewRequest("GET", "/v1/internal/ui/services?filter="+filterQuery, nil)
resp := httptest.NewRecorder()
obj, err := a.srv.UIServices(resp, req)
require.NoError(t, err)
assertIndex(t, resp)

// Ensure the ServiceSummary doesn't output a `null` response when there
// are no matching summaries
require.NotNil(t, obj)

summary := obj.([]*ServiceListingSummary)
require.Len(t, summary, 0)
})
}

func TestUIGatewayServiceNodes_Terminating(t *testing.T) {
Expand Down

0 comments on commit a9913b5

Please sign in to comment.