Skip to content

Commit d1950a5

Browse files
committed
Dont' observe requests/responses to OADs that have not been registered
1 parent ba7afb5 commit d1950a5

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#### Changed
88
- Test now raises a `OpenapiFirst::Test::UnknownQueryParameterError` when it sees unknown query parameters
9+
- Test does not track requests/responses unless the OAD was registered via Test.register (or OpenapiFirst.register)
910

1011
## 3.0.1
1112
- Add missing gem dependency "drb", which is no longer installed by default with newer rubies. This is used in openapi_first/test to make parallel tests work.

lib/openapi_first/test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ def self.install
105105

106106
OpenapiFirst.configure do |config|
107107
@after_request_validation = config.after_request_validation do |validated_request, oad|
108+
next unless registered?(oad)
108109
raise validated_request.error.exception if raise_request_error?(validated_request)
109110

110111
check_unknown_query_parameters(validated_request)
@@ -113,6 +114,7 @@ def self.install
113114
end
114115

115116
@after_response_validation = config.after_response_validation do |validated_response, rack_request, oad|
117+
next unless registered?(oad)
116118
if validated_response.invalid? && raise_response_error?(validated_response)
117119
raise validated_response.error.exception
118120
end

spec/test_spec.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,19 @@ def call(_env)
485485

486486
expect(described_class::Coverage.result.coverage).to eq 50
487487
end
488+
489+
it 'tracks the request' do
490+
expect(described_class::Coverage).to receive(:track_request)
491+
492+
app.call(Rack::MockRequest.env_for('/stuff/nostring'))
493+
end
494+
495+
it 'does not track the request for for unregistered OADs' do
496+
expect(described_class).to receive(:registered?).twice.and_return(false) # once for request, once for response tracking
497+
expect(described_class::Coverage).not_to receive(:track_request)
498+
499+
app.call(Rack::MockRequest.env_for('/stuff/1'))
500+
end
488501
end
489502

490503
describe 'handling unknown requests paths' do
@@ -712,4 +725,52 @@ def call(_env)
712725
end
713726
end
714727
end
728+
729+
describe 'handling valid responses' do
730+
let(:definition) do
731+
OpenapiFirst.parse(YAML.load(%(
732+
openapi: 3.1.0
733+
info:
734+
title: Dice
735+
version: 1
736+
paths:
737+
"/roll":
738+
post:
739+
responses:
740+
'200':
741+
content:
742+
application/json:
743+
schema:
744+
type: integer
745+
min: 1
746+
max:
747+
)))
748+
end
749+
750+
let(:app) do
751+
described_class.app(
752+
->(_env) { [200, { 'content-type' => 'application/json' }, ['1']] },
753+
spec: definition
754+
)
755+
end
756+
757+
before(:each) do
758+
described_class.setup do |test|
759+
test.register(definition)
760+
end
761+
end
762+
763+
it 'tracks the response' do
764+
expect(described_class::Coverage).to receive(:track_response)
765+
766+
app.call(Rack::MockRequest.env_for('/roll', method: 'POST'))
767+
end
768+
769+
it 'does not track the response for for unregistered OADs' do
770+
expect(described_class).to receive(:registered?).twice.and_return(false) # once for request, once for response tracking
771+
expect(described_class::Coverage).not_to receive(:track_response)
772+
773+
app.call(Rack::MockRequest.env_for('/roll', method: 'POST'))
774+
end
775+
end
715776
end

0 commit comments

Comments
 (0)