diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-02-07 18:15:53 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-02-07 18:15:53 +0300 |
commit | 468bcfb9c6d04b0244c349eda62cd7ed1496cca3 (patch) | |
tree | f8d645cbf62ccc9fcd94174390324e1ce934101b /spec/support | |
parent | 072dbf4b87096eb0a4f82f9e0583527ed5d8f4d0 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/support')
-rw-r--r-- | spec/support/matchers/schema_matcher.rb | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/spec/support/matchers/schema_matcher.rb b/spec/support/matchers/schema_matcher.rb index 5e08e96f4e1..d2f32b60464 100644 --- a/spec/support/matchers/schema_matcher.rb +++ b/spec/support/matchers/schema_matcher.rb @@ -36,12 +36,38 @@ end RSpec::Matchers.define :match_response_schema do |schema, dir: nil, **options| match do |response| - schema_path = Pathname.new(SchemaPath.expand(schema, dir)) - validator = SchemaPath.validator(schema_path) + @schema_path = Pathname.new(SchemaPath.expand(schema, dir)) + validator = SchemaPath.validator(@schema_path) - data = Gitlab::Json.parse(response.body) + @data = Gitlab::Json.parse(response.body) - validator.valid?(data) + @schema_errors = validator.validate(@data) + @schema_errors.none? + end + + failure_message do |actual| + message = [] + + message << <<~MESSAGE + expected JSON response to match schema #{@schema_path.inspect}. + + JSON input: #{Gitlab::Json.pretty_generate(@data).indent(2)} + + Schema errors: + MESSAGE + + @schema_errors.each do |error| + property_name, actual_value = error.values_at('data_pointer', 'data') + property_name = 'root' if property_name.empty? + + message << <<~MESSAGE + Property: #{property_name} + Actual value: #{Gitlab::Json.pretty_generate(actual_value).indent(2)} + Error: #{JSONSchemer::Errors.pretty(error)} + MESSAGE + end + + message.join("\n") end end |