32 lines
1.0 KiB
Go
32 lines
1.0 KiB
Go
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())
|
|
}
|
|
}
|