Review fixes: record cancelled requests as 499; empty usage is an array
Implemented-By: OpenCode session (model recorded in docs/implementer-log.md)
This commit is contained in:
@@ -141,6 +141,9 @@ func (hx *handler) usageGet(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusInternalServerError, "usage: "+err.Error())
|
||||
return
|
||||
}
|
||||
if rows == nil {
|
||||
rows = []store.UsageRow{}
|
||||
}
|
||||
if r.Header.Get("Accept") == "text/plain" {
|
||||
writeUsageTable(w, rows)
|
||||
return
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package admin_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.wntrmute.dev/kyle/crossbar/internal/store"
|
||||
)
|
||||
|
||||
// An empty usage table is an empty JSON array, not null: clients iterate it.
|
||||
func TestUsageEmptyIsAnArray(t *testing.T) {
|
||||
r := newRig(t)
|
||||
for _, q := range []string{"/_crossbar/usage", "/_crossbar/usage?by=host", "/_crossbar/usage?by=model&since=1h"} {
|
||||
rec := r.do(t, "GET", q, "")
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("%s: %d", q, rec.Code)
|
||||
}
|
||||
if strings.TrimSpace(rec.Body.String()) != "[]" {
|
||||
t.Errorf("%s: body %q, want []", q, rec.Body.String())
|
||||
}
|
||||
var rows []store.UsageRow
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &rows); err != nil || rows == nil || len(rows) != 0 {
|
||||
t.Errorf("%s: decoded %v %v, want an empty non-nil slice", q, rows, err)
|
||||
}
|
||||
}
|
||||
rec := r.do(t, "GET", "/_crossbar/usage?by=route", "", "Accept", "text/plain")
|
||||
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "key") {
|
||||
t.Errorf("text form with no rows must still print the header: %d %q", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user