diff --git a/match/json.go b/match/json.go index a149dcf11..446fbeba7 100644 --- a/match/json.go +++ b/match/json.go @@ -78,6 +78,18 @@ func JSONKeyMissing(forbiddenKey string) JSON { } } +// JSONKeyMissing returns a matcher which will check that `forbiddenKey` is not present in the JSON object, or is null. +// `forbiddenKey` can be nested, see https://godoc.org/github.com/tidwall/gjson#Get for details. +func JSONKeyMissingOrNull(forbiddenKey string) JSON { + return func(body gjson.Result) error { + res := body.Get(forbiddenKey) + if res.Exists() && res.Type != gjson.Null { + return fmt.Errorf("key '%s' present and not null", forbiddenKey) + } + return nil + } +} + // JSONKeyTypeEqual returns a matcher which will check that `wantKey` is present and its value is of the type `wantType`. // `wantKey` can be nested, see https://godoc.org/github.com/tidwall/gjson#Get for details. func JSONKeyTypeEqual(wantKey string, wantType gjson.Type) JSON { diff --git a/tests/csapi/apidoc_room_state_test.go b/tests/csapi/apidoc_room_state_test.go index ff085efc3..b0424a001 100644 --- a/tests/csapi/apidoc_room_state_test.go +++ b/tests/csapi/apidoc_room_state_test.go @@ -98,7 +98,7 @@ func TestRoomState(t *testing.T) { match.JSONKeyPresent("joined"), match.JSONKeyPresent("joined." + authedClient.UserID), match.JSONKeyPresent("joined." + authedClient.UserID + ".display_name"), - match.JSONKeyPresent("joined." + authedClient.UserID + ".avatar_url"), + match.JSONKeyMissingOrNull("joined." + authedClient.UserID + ".avatar_url"), }, }) })