11# frozen_string_literal: true
22
33require_relative 'test/configuration'
4+ require_relative 'test/registry'
45
56module OpenapiFirst
67 # Test integration
78 module Test
89 autoload :Coverage , 'openapi_first/test/coverage'
910 autoload :Methods , 'openapi_first/test/methods'
11+ extend Registry
1012
1113 class CoverageError < Error ; end
1214
@@ -59,8 +61,7 @@ def self.handle_exit
5961 )
6062 return unless configuration . report_coverage == true
6163
62- coverage = Coverage . result . coverage
63- return if coverage >= configuration . minimum_coverage
64+ return if Coverage . result . coverage >= configuration . minimum_coverage
6465
6566 raise OpenapiFirst ::Test ::CoverageError , 'Not all described requests and responses have been tested.'
6667 end
@@ -69,8 +70,7 @@ def self.handle_exit
6970 # @param formatter A formatter to define the report.
7071 # @output [IO] An output where to puts the report.
7172 def self . report_coverage ( formatter : Coverage ::TerminalFormatter , **)
72- coverage_result = Coverage . result
73- puts formatter . new ( **) . format ( coverage_result )
73+ puts formatter . new ( **) . format ( Coverage . result )
7474 end
7575
7676 # Returns the Rack app wrapped with silent request, response validation
@@ -90,10 +90,12 @@ def self.install
9090
9191 OpenapiFirst . configure do |config |
9292 @after_request_validation = config . after_request_validation do |validated_request , oad |
93- after_request_validation ( validated_request , oad )
93+ Coverage . track_request ( validated_request , oad )
9494 end
9595 @after_response_validation = config . after_response_validation do |validated_response , rack_request , oad |
96- after_response_validation ( validated_response , rack_request , oad )
96+ raise validated_response . error . exception if configuration . response_raise_error && validated_response . invalid?
97+
98+ Coverage . track_response ( validated_response , rack_request , oad )
9799 end
98100 end
99101 @installed = true
@@ -108,55 +110,5 @@ def self.uninstall
108110 @installed = nil
109111 @exit_handler = nil
110112 end
111-
112- class NotRegisteredError < StandardError ; end
113- class AlreadyRegisteredError < StandardError ; end
114-
115- @definitions = { }
116-
117- class << self
118- attr_reader :definitions
119-
120- # Register an OpenAPI definition for testing
121- # @param path_or_definition [String, Definition] Path to the OpenAPI file or a Definition object
122- # @param as [Symbol] Name to register the API definition as
123- def register ( path_or_definition , as : :default )
124- if definitions . key? ( as ) && as == :default
125- raise (
126- AlreadyRegisteredError ,
127- "#{ definitions [ as ] . filepath . inspect } is already registered " \
128- "as ':default' so you cannot register #{ path_or_definition . inspect } without " \
129- 'giving it a custom name. Please call register with a custom key like: ' \
130- "OpenapiFirst::Test.register(#{ path_or_definition . inspect } , as: :my_other_api)"
131- )
132- end
133-
134- definition = OpenapiFirst . load ( path_or_definition )
135- definitions [ as ] = definition
136- definition
137- end
138-
139- def []( api )
140- definitions . fetch ( api ) do
141- option = api == :default ? '' : ", as: #{ api . inspect } "
142- raise ( NotRegisteredError ,
143- "API description '#{ api . inspect } ' not found." \
144- "Please call OpenapiFirst::Test.register('myopenapi.yaml'#{ option } ) " \
145- 'once before running tests.' )
146- end
147- end
148-
149- private
150-
151- def after_request_validation ( validated_request , oad )
152- Coverage . track_request ( validated_request , oad )
153- end
154-
155- def after_response_validation ( validated_response , request , oad )
156- raise validated_response . error . exception if configuration . response_raise_error && validated_response . invalid?
157-
158- Coverage . track_response ( validated_response , request , oad )
159- end
160- end
161113 end
162114end
0 commit comments