Module: OpenapiFirst::Test
- Defined in:
- lib/openapi_first/test.rb,
lib/openapi_first/test/app.rb,
lib/openapi_first/test/logger.rb,
lib/openapi_first/test/methods.rb,
lib/openapi_first/test/observe.rb,
lib/openapi_first/test/callable.rb,
lib/openapi_first/test/coverage.rb,
lib/openapi_first/test/configuration.rb,
lib/openapi_first/test/coverage/plan.rb,
lib/openapi_first/test/plain_helpers.rb,
lib/openapi_first/test/coverage/tracker.rb,
lib/openapi_first/test/minitest_helpers.rb,
lib/openapi_first/test/coverage/route_task.rb,
lib/openapi_first/test/observer_middleware.rb,
lib/openapi_first/test/coverage/request_task.rb,
lib/openapi_first/test/coverage/html_reporter.rb,
lib/openapi_first/test/coverage/response_task.rb,
lib/openapi_first/test/coverage/covered_request.rb,
lib/openapi_first/test/coverage/covered_response.rb,
lib/openapi_first/test/coverage/terminal_reporter.rb,
lib/openapi_first/test/coverage/html_reporter/context.rb
Overview
Defined Under Namespace
Modules: Callable, Coverage, Methods, MinitestHelpers, Observe, PlainHelpers
Classes: App, Configuration, CoverageError, Logger, ObserveError, ObserverMiddleware, UnknownQueryParameterError
Constant Summary
collapse
- REQUEST =
'openapi.test.request'
- RESPONSE =
'openapi.test.response'
Class Method Summary
collapse
Class Method Details
.app(app, spec: nil, api: :default, validate_request_before_handling: false) ⇒ Object
Returns the Rack app wrapped with silent request, response validation You can use this if you want to track coverage via Test::Coverage, but don’t want to use the middlewares or manual request, response validation.
108
109
110
111
|
# File 'lib/openapi_first/test.rb', line 108
def self.app(app, spec: nil, api: :default, validate_request_before_handling: false)
spec ||= self[api]
App.new(app, api: spec, validate_request_before_handling:)
end
|
.configuration ⇒ Object
38
39
40
|
# File 'lib/openapi_first/test.rb', line 38
def self.configuration
@configuration ||= Configuration.new
end
|
.definitions ⇒ Object
30
31
32
|
# File 'lib/openapi_first/test.rb', line 30
def self.definitions
super.empty? ? OpenapiFirst.definitions : super
end
|
.handle_exit ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/openapi_first/test.rb', line 76
def self.handle_exit
return unless configuration.report_coverage
report_coverage(
reporter: configuration.coverage_reporter,
**configuration.coverage_reporter_options
)
return unless configuration.report_coverage == true
coverage = Coverage.result.coverage
return if coverage >= configuration.minimum_coverage
puts "API Coverage fails with exit 2, because not all described requests and responses have been tested (#{coverage.round(4)}% covered)."
exit 2
end
|
.install ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/openapi_first/test.rb', line 113
def self.install
return if @installed
OpenapiFirst.configure do |config|
@after_request_validation = config.after_request_validation do |validated_request, oad|
next unless registered?(oad)
if configuration.raise_request_error?(validated_request)
raise validated_request.error.exception if validated_request.unknown?
check_unknown_query_parameters(validated_request)
end
Coverage.track_request(validated_request, oad)
end
@after_response_validation = config.after_response_validation do |validated_response, rack_request, oad|
next unless registered?(oad)
raise validated_response.error.exception if raise_response_error?(validated_response, rack_request)
Coverage.track_response(validated_response, rack_request, oad)
end
end
@installed = true
end
|
.logger ⇒ Object
34
35
36
|
# File 'lib/openapi_first/test.rb', line 34
def self.logger
configuration.logger
end
|
.minitest?(base) ⇒ Boolean
24
25
26
27
28
|
# File 'lib/openapi_first/test.rb', line 24
def self.minitest?(base)
base.include?(::Minitest::Assertions)
rescue NameError
false
end
|
.observe(app, api: :default) ⇒ Object
Inject request/response validation in a rack app class
20
21
22
|
# File 'lib/openapi_first/test.rb', line 20
def self.observe(app, api: :default)
Observe.observe(app, api:)
end
|
.registered?(oad) ⇒ Boolean
42
43
44
45
|
# File 'lib/openapi_first/test.rb', line 42
def self.registered?(oad)
key = oad.key
definitions.any? { |(_name, registered)| registered.key == key }
end
|
.report_coverage(reporter: Coverage::TerminalReporter, formatter: nil) ⇒ IO
Print the coverage report
97
98
99
100
101
102
103
|
# File 'lib/openapi_first/test.rb', line 97
def self.report_coverage(reporter: Coverage::TerminalReporter, formatter: nil, **)
if formatter
warn 'DEPRECATION WARNING: Test.report_coverage(formatter:) is deprecated, use reporter: instead.'
reporter = formatter
end
reporter.new(**).report(Coverage.result)
end
|
.setup {|OpenapiFirst::Test::Configuration| ... } ⇒ Object
Sets up OpenAPI test coverage and OAD registration.
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/openapi_first/test.rb', line 49
def self.setup
install
yield configuration if block_given?
Coverage.start(skip_response: configuration.skip_response_coverage, skip_route: configuration.skip_coverage)
if definitions.empty?
raise NotRegisteredError,
'No API descriptions have been registered. ' \
'Please register your API description via ' \
"`OpenapiFirst.register('myopenapi.yaml)` or " \
'in a block passed to `OpenapiFirst::Test.setup` like this: ' \
"`OpenapiFirst::Test.setup { |test| test.register('myopenapi.yaml') }` " \
end
@exit_handler = method(:handle_exit)
main_process = Process.pid
@setup ||= at_exit do
@exit_handler&.call if Process.pid == main_process
end
end
|
.uninstall ⇒ Object
139
140
141
142
143
144
145
146
147
|
# File 'lib/openapi_first/test.rb', line 139
def self.uninstall
configuration = OpenapiFirst.configuration
configuration.after_request_validation.delete(@after_request_validation)
configuration.after_response_validation.delete(@after_response_validation)
definitions.clear
@configuration = nil
@installed = nil
@exit_handler = nil
end
|