Class: OpenapiFirst::Test::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_first/test/configuration.rb

Overview

Helper class to setup tests

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/openapi_first/test/configuration.rb', line 9

def initialize
  @minimum_coverage = 100
  @coverage_reporter = Coverage::HtmlReporter
  @coverage_reporter_options = {}
  @skip_response_coverage = nil
  @skip_coverage = nil
  @response_raise_error = true
  @ignored_unknown_status = Set.new([401, 404, 500])
  @ignore_unknown_response_status = false
  @report_coverage = true
  @ignore_unknown_requests = false
  @ignore_request_error = nil
  @ignore_response_error = nil
  @logger = Logger.new($stdout)
end

Instance Attribute Details

#coverage_reporterObject

Returns the value of attribute coverage_reporter.



37
38
39
# File 'lib/openapi_first/test/configuration.rb', line 37

def coverage_reporter
  @coverage_reporter
end

#coverage_reporter_optionsObject

Returns the value of attribute coverage_reporter_options.



37
38
39
# File 'lib/openapi_first/test/configuration.rb', line 37

def coverage_reporter_options
  @coverage_reporter_options
end

#ignore_unknown_requestsObject Also known as: ignore_unknown_requests?

Returns the value of attribute ignore_unknown_requests.



37
38
39
# File 'lib/openapi_first/test/configuration.rb', line 37

def ignore_unknown_requests
  @ignore_unknown_requests
end

#ignore_unknown_response_statusObject Also known as: ignore_unknown_response_status?

Returns the value of attribute ignore_unknown_response_status.



37
38
39
# File 'lib/openapi_first/test/configuration.rb', line 37

def ignore_unknown_response_status
  @ignore_unknown_response_status
end

#ignored_unknown_statusObject

Returns the value of attribute ignored_unknown_status.



39
40
41
# File 'lib/openapi_first/test/configuration.rb', line 39

def ignored_unknown_status
  @ignored_unknown_status
end

#loggerObject

Returns the value of attribute logger.



37
38
39
# File 'lib/openapi_first/test/configuration.rb', line 37

def logger
  @logger
end

#minimum_coverageObject

Returns the value of attribute minimum_coverage.



37
38
39
# File 'lib/openapi_first/test/configuration.rb', line 37

def minimum_coverage
  @minimum_coverage
end

#report_coverageObject

Returns the value of attribute report_coverage.



39
40
41
# File 'lib/openapi_first/test/configuration.rb', line 39

def report_coverage
  @report_coverage
end

#response_raise_errorObject

Returns the value of attribute response_raise_error.



37
38
39
# File 'lib/openapi_first/test/configuration.rb', line 37

def response_raise_error
  @response_raise_error
end

Instance Method Details

#coverage_formatterObject

Deprecated.

Use #coverage_reporter instead.



42
43
44
45
# File 'lib/openapi_first/test/configuration.rb', line 42

def coverage_formatter
  warn_coverage_formatter_deprecation
  coverage_reporter
end

#coverage_formatter=(value) ⇒ Object

Deprecated.

Use #coverage_reporter= instead.



48
49
50
51
# File 'lib/openapi_first/test/configuration.rb', line 48

def coverage_formatter=(value)
  warn_coverage_formatter_deprecation
  self.coverage_reporter = value
end

#coverage_formatter_optionsObject

Deprecated.


54
55
56
57
# File 'lib/openapi_first/test/configuration.rb', line 54

def coverage_formatter_options
  warn_coverage_formatter_deprecation
  coverage_reporter_options
end

#coverage_formatter_options=(value) ⇒ Object

Deprecated.


60
61
62
63
# File 'lib/openapi_first/test/configuration.rb', line 60

def coverage_formatter_options=(value)
  warn_coverage_formatter_deprecation
  self.coverage_reporter_options = value
end

#ignore_request_error(&block) ⇒ Object

Ignore certain errors for certain requests

Parameters:

  • block

    A Proc that will be called with [OpenapiFirst::ValidatedRequest]

Raises:

  • (ArgumentError)


84
85
86
87
88
89
90
# File 'lib/openapi_first/test/configuration.rb', line 84

def ignore_request_error(&block)
  # :nocov:
  raise ArgumentError, 'You have to pass a block' unless block_given?
  # :nocov:

  @ignore_request_error = block
end

#ignore_response_error(&block) ⇒ Object

Ignore certain errors for certain responses

Parameters:

  • block

    A Proc that will be called with [OpenapiFirst::ValidatedResponse, Rack::Request]

Raises:

  • (ArgumentError)


94
95
96
97
98
99
100
# File 'lib/openapi_first/test/configuration.rb', line 94

def ignore_response_error(&block)
  # :nocov:
  raise ArgumentError, 'You have to pass a block' unless block_given?
  # :nocov:

  @ignore_response_error = block
end

#observe(app, api: :default) ⇒ Object

Observe a rack app



33
34
35
# File 'lib/openapi_first/test/configuration.rb', line 33

def observe(app, api: :default)
  Observe.observe(app, api:)
end

#raise_request_error?(validated_request) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
121
122
123
# File 'lib/openapi_first/test/configuration.rb', line 118

def raise_request_error?(validated_request)
  return false if @ignore_request_error&.call(validated_request)
  return false if ignore_unknown_requests? && validated_request.unknown?

  true
end

#raise_response_error?(validated_response, rack_request) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
129
130
131
132
# File 'lib/openapi_first/test/configuration.rb', line 125

def raise_response_error?(validated_response, rack_request)
  return false if @ignore_response_error&.call(validated_response, rack_request)
  return false if response_raise_error == false
  return false if ignored_unknown_status.include?(validated_response.status)
  return false if ignore_unknown_response_status? && validated_response.error.type == :response_status_not_found

  true
end

#register(oad, as: :default) ⇒ Object

Register OADs, but don’t load them just yet

Parameters:

  • oad (OpenapiFirst::OAD)

    The OAD to register

  • as (Symbol) (defaults to: :default)

    The name to register the OAD under



28
29
30
# File 'lib/openapi_first/test/configuration.rb', line 28

def register(oad, as: :default)
  Test.register(oad, as:)
end

#skip_coverage(&block) ⇒ Object



109
110
111
112
113
# File 'lib/openapi_first/test/configuration.rb', line 109

def skip_coverage(&block)
  return @skip_coverage unless block_given?

  @skip_coverage = block
end

#skip_response_coverage(&block) ⇒ Object

Parameters:

  • block

    A Proc that will be called with [OpenapiFirst::ValidatedResponse, Rack::Request]



103
104
105
106
107
# File 'lib/openapi_first/test/configuration.rb', line 103

def skip_response_coverage(&block)
  return @skip_response_coverage unless block_given?

  @skip_response_coverage = block
end