Module: Committee::Test::Methods

Defined in:
lib/committee/test/methods.rb

Instance Method Summary collapse

Instance Method Details

#assert_request_schema_confirmObject



11
12
13
14
15
16
17
18
# File 'lib/committee/test/methods.rb', line 11

def assert_request_schema_confirm
  unless schema_validator.link_exist?
    request = "`#{request_object.request_method} #{request_object.path_info}` undefined in schema (prefix: #{committee_options[:prefix].inspect})."
    raise Committee::InvalidRequest.new(request)
  end

  schema_validator.request_validate(request_object)
end

#assert_response_schema_confirm(expected_status = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/committee/test/methods.rb', line 20

def assert_response_schema_confirm(expected_status = nil)
  unless schema_validator.link_exist?
    response = "`#{request_object.request_method} #{request_object.path_info}` undefined in schema (prefix: #{committee_options[:prefix].inspect})."
    raise Committee::InvalidResponse.new(response)
  end

  status, headers, body = response_data

  if expected_status.nil?
    Committee.warn_deprecated('Pass expected response status code to check it against the corresponding schema explicitly.')
  elsif expected_status != status
    response = "Expected `#{expected_status}` status code, but it was `#{status}`."
    raise Committee::InvalidResponse.new(response)
  end

  if schema_coverage
    operation_object = router.operation_object(request_object)
    schema_coverage&.update_response_coverage!(operation_object.original_path, operation_object.http_method, status)
  end

  schema_validator.response_validate(status, headers, [body], true) if validate_response?(status)
end

#assert_schema_conform(expected_status = nil) ⇒ Object



6
7
8
9
# File 'lib/committee/test/methods.rb', line 6

def assert_schema_conform(expected_status = nil)
  assert_request_schema_confirm unless old_behavior
  assert_response_schema_confirm(expected_status)
end

#committee_optionsObject



43
44
45
# File 'lib/committee/test/methods.rb', line 43

def committee_options
  raise "please set options"
end

#old_behaviorObject



79
80
81
82
83
84
85
86
# File 'lib/committee/test/methods.rb', line 79

def old_behavior
  old_assert_behavior = committee_options.fetch(:old_assert_behavior, nil)
  if old_assert_behavior.nil?
    Committee.warn_deprecated('Now assert_schema_conform check response schema only. but we will change check request and response in future major version. so if you want to conform response only, please use assert_response_schema_confirm, or you can suppress this message and keep old behavior by setting old_assert_behavior=true.')
    old_assert_behavior = true
  end
  old_assert_behavior
end

#request_objectObject



47
48
49
# File 'lib/committee/test/methods.rb', line 47

def request_object
  raise "please set object like 'last_request'"
end

#response_dataObject



51
52
53
# File 'lib/committee/test/methods.rb', line 51

def response_data
  raise "please set response data like 'last_response.status, last_response.headers, last_response.body'"
end

#routerObject



63
64
65
# File 'lib/committee/test/methods.rb', line 63

def router
  @router ||= schema.build_router(committee_options)
end

#schemaObject



59
60
61
# File 'lib/committee/test/methods.rb', line 59

def schema
  @schema ||= Committee::Middleware::Base.get_schema(committee_options)
end

#schema_coverageObject



71
72
73
74
75
76
77
# File 'lib/committee/test/methods.rb', line 71

def schema_coverage
  return nil unless schema.is_a?(Committee::Drivers::OpenAPI3::Schema)

  coverage = committee_options.fetch(:schema_coverage, nil)

  coverage.is_a?(SchemaCoverage) ? coverage : nil
end

#schema_validatorObject



67
68
69
# File 'lib/committee/test/methods.rb', line 67

def schema_validator
  @schema_validator ||= router.build_schema_validator(request_object)
end

#validate_response?(status) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/committee/test/methods.rb', line 55

def validate_response?(status)
  Committee::Middleware::ResponseValidation.validate?(status, committee_options.fetch(:validate_success_only, false))
end