package protocol import ( "fmt" "reflect" "testing" ) func TestUnknownFieldsReportsOnlyUnmodeledSortedKeys(t *testing.T) { if got := UnknownFields([]byte(`{"type":"pong"}`)); got != nil { t.Fatalf("fully modeled frame reported %v", got) } got := UnknownFields([]byte(`{"type":"pong","zzz":1,"aaa":2}`)) if !reflect.DeepEqual(got, []string{"aaa", "zzz"}) { t.Fatalf("unknown fields = %v, want sorted [aaa zzz]", got) } } func TestUnknownFieldsIgnoresMalformedAndNonObjectFrames(t *testing.T) { for _, data := range []string{ `not json`, `[1,2,3]`, `"a string"`, `42`, `null`, } { if got := UnknownFields([]byte(data)); got != nil { t.Fatalf("frame %q reported unknown fields %v", data, got) } } } func TestKnownFieldsCachesTheModeledKeySet(t *testing.T) { first := KnownFields() second := KnownFields() if len(first) == 0 { t.Fatal("known field set is empty") } if _, ok := first["type"]; !ok { t.Fatal("known field set is missing the modeled type key") } if _, ok := first["build"]; !ok { t.Fatal("known field set is missing the modeled build key") } if _, ok := first["protocol"]; !ok { t.Fatal("known field set is missing the modeled protocol key") } // The sync.Once must return the same cached map on every call. if fmt.Sprintf("%p", first) != fmt.Sprintf("%p", second) { t.Fatal("KnownFields re-derived the set instead of using the cache") } }