1
1
# frozen_string_literal: true
2
2
3
3
require_relative 'test/configuration'
4
+ require_relative 'test/registry'
4
5
5
6
module OpenapiFirst
6
7
# Test integration
7
8
module Test
8
9
autoload :Coverage , 'openapi_first/test/coverage'
9
10
autoload :Methods , 'openapi_first/test/methods'
11
+ extend Registry
10
12
11
13
class CoverageError < Error ; end
12
14
@@ -59,8 +61,7 @@ def self.handle_exit
59
61
)
60
62
return unless configuration . report_coverage == true
61
63
62
- coverage = Coverage . result . coverage
63
- return if coverage >= configuration . minimum_coverage
64
+ return if Coverage . result . coverage >= configuration . minimum_coverage
64
65
65
66
raise OpenapiFirst ::Test ::CoverageError , 'Not all described requests and responses have been tested.'
66
67
end
@@ -69,8 +70,7 @@ def self.handle_exit
69
70
# @param formatter A formatter to define the report.
70
71
# @output [IO] An output where to puts the report.
71
72
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 )
74
74
end
75
75
76
76
# Returns the Rack app wrapped with silent request, response validation
@@ -90,10 +90,12 @@ def self.install
90
90
91
91
OpenapiFirst . configure do |config |
92
92
@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 )
94
94
end
95
95
@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 )
97
99
end
98
100
end
99
101
@installed = true
@@ -108,55 +110,5 @@ def self.uninstall
108
110
@installed = nil
109
111
@exit_handler = nil
110
112
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
161
113
end
162
114
end
0 commit comments