Skip to content

Commit 7d2d9f2

Browse files
committed
Add Test-configuration to ignore certain unknown response status
Add `OpenapiFirst::Test::Configuration#ignored_unknown_status` to configure response status(es) that do not have to be descriped in the API description.
1 parent fded0c5 commit 7d2d9f2

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

33
## Unreleased
4+
- Add `OpenapiFirst::Test::Configuration#ignored_unknown_status` to configure response status(es) that do not have to be descriped in the API description.
45
- Change `OpenapiFirst::Test` to make tests fail if API description is not covered by tests. You can adapt this behavior via `OpenapiFirst::Test.setup` / `skip_response_coverage_if` or deactivate coverage with `report_coverage = false` or `report_coverage = :warn`
56
- Added `OpenapiFirst::Test::Configuration#report_coverage=` to configure the behavior if not all requests/responses of the API under test have been tested.
67
- Deprecate `OpenapiFirst::Test::Configuration#minimum_coverage=` use "#report_coverage" instead to modify the behavior

lib/openapi_first/test.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,26 @@ def self.install
9292
@after_request_validation = config.after_request_validation do |validated_request, oad|
9393
Coverage.track_request(validated_request, oad)
9494
end
95-
@after_response_validation = config.after_response_validation do |validated_response, rack_request, oad|
96-
raise validated_response.error.exception if configuration.response_raise_error && validated_response.invalid?
9795

98-
Coverage.track_response(validated_response, rack_request, oad)
96+
@after_response_validation = config.after_response_validation do |validated_response, rack_request, oad|
97+
after_response_validation(validated_response, rack_request, oad)
9998
end
10099
end
101100
@installed = true
102101
end
103102

103+
def self.after_response_validation(validated_response, rack_request, oad)
104+
if validated_response.invalid? && raise_response_error?(validated_response)
105+
raise validated_response.error.exception
106+
end
107+
108+
Coverage.track_response(validated_response, rack_request, oad)
109+
end
110+
111+
def self.raise_response_error?(validated_response)
112+
configuration.response_raise_error && !configuration.ignored_unknown_status.include?(validated_response.status)
113+
end
114+
104115
def self.uninstall
105116
configuration = OpenapiFirst.configuration
106117
configuration.hooks[:after_request_validation].delete(@after_request_validation)

lib/openapi_first/test/configuration.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def initialize
1010
@coverage_formatter_options = {}
1111
@skip_response_coverage = nil
1212
@response_raise_error = true
13+
@ignored_unknown_status = []
1314
@report_coverage = true
1415
@registry = {}
1516
end
@@ -20,7 +21,7 @@ def register(oad, as: :default)
2021
end
2122

2223
attr_accessor :coverage_formatter_options, :coverage_formatter, :response_raise_error
23-
attr_reader :registry, :minimum_coverage, :report_coverage
24+
attr_reader :registry, :minimum_coverage, :report_coverage, :ignored_unknown_status
2425

2526
def minimum_coverage=(value)
2627
warn 'Setting OpenapiFirst::Test::Configuration#minimum_coverage= is deprecated ' \

spec/test_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,4 +424,41 @@
424424
end
425425
end
426426
end
427+
428+
describe 'handling unknown response status' do
429+
let(:definition) do
430+
OpenapiFirst.load('./spec/data/dice.yaml')
431+
end
432+
433+
let(:app) do
434+
described_class.app(
435+
->(_env) { [302, { 'content-type' => 'application/json' }, ['5']] },
436+
spec: definition
437+
)
438+
end
439+
440+
before(:each) do
441+
described_class.setup do |test|
442+
test.register(definition)
443+
end
444+
end
445+
446+
it 'raises an error' do
447+
expect do
448+
app.call(Rack::MockRequest.env_for('/roll', method: 'POST'))
449+
end.to raise_error(OpenapiFirst::ResponseNotFoundError)
450+
end
451+
452+
context 'with ignored_unknown_status' do
453+
before(:each) do
454+
described_class.configuration.ignored_unknown_status << 302
455+
end
456+
457+
it 'does not raise an error' do
458+
expect do
459+
app.call(Rack::MockRequest.env_for('/roll', method: 'POST'))
460+
end.not_to raise_error
461+
end
462+
end
463+
end
427464
end

0 commit comments

Comments
 (0)