Module: OpenapiRuby::Testing::Assertions

Defined in:
lib/openapi_ruby/testing/assertions.rb

Instance Method Summary collapse

Instance Method Details

#assert_request_schema_conform(spec_path: nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/openapi_ruby/testing/assertions.rb', line 32

def assert_request_schema_conform(spec_path: nil)
  doc = load_openapi_document(spec_path)
  path_template = find_matching_path(request.path, doc)
  method = request.request_method.downcase

  operation = doc.dig("paths", path_template, method)
  assert_or_expect(operation, "Operation not found: #{method.upcase} #{path_template}")

  errors = validate_request_params(operation)
  assert_message = "Request schema validation failed:\n#{errors.join("\n")}"

  if defined?(RSpec)
    expect(errors).to be_empty, assert_message
  else
    assert errors.empty?, assert_message
  end
end

#assert_response_schema_conform(expected_status = nil, spec_path: nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/openapi_ruby/testing/assertions.rb', line 6

def assert_response_schema_conform(expected_status = nil, spec_path: nil)
  doc = load_openapi_document(spec_path)
  validator = ResponseValidator.new(doc)

  status = expected_status || response.status
  path_template = find_matching_path(request.path, doc)
  method = request.request_method.downcase

  body = parse_json_response

  errors = validator.validate_against_document(
    response_body: body,
    status_code: status,
    path: path_template,
    method: method
  )

  assert_message = "Response schema validation failed:\n#{errors.join("\n")}"

  if defined?(RSpec)
    expect(errors).to be_empty, assert_message
  else
    assert errors.empty?, assert_message
  end
end