Skip to content

Commit a03336a

Browse files
committed
Track the request after letting the app handle it
1 parent d75c649 commit a03336a

5 files changed

Lines changed: 40 additions & 34 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## Unreleased
44

5+
### Changed
6+
- Changed OpenapiFirst::Test to track the request _after_ the app has handled the request. See [PR #434](https://github.com/ahx/openapi_first/pull/434). You can restore the old behavior with
7+
```ruby
8+
include OpenapiFirst::Test::Methods[MyApp, validate_request_before_handling: true]
9+
```
10+
511
### Added
612

713
- Added support for a static `path_prefix` value to be set on the creation of a Definition. See [PR #432](https://github.com/ahx/openapi_first/pull/432):

lib/openapi_first/test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ def self.report_coverage(formatter: Coverage::TerminalFormatter, **)
9595
# Returns the Rack app wrapped with silent request, response validation
9696
# You can use this if you want to track coverage via Test::Coverage, but don't want to use
9797
# the middlewares or manual request, response validation.
98-
def self.app(app, spec: nil, api: :default, validate_request_after_handling: false)
98+
def self.app(app, spec: nil, api: :default, validate_request_before_handling: false)
9999
spec ||= self[api]
100-
App.new(app, api: spec, validate_request_after_handling:)
100+
App.new(app, api: spec, validate_request_before_handling:)
101101
end
102102

103103
def self.install

lib/openapi_first/test/app.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ module Test
1010
# A wrapper of the original app
1111
# with silent request/response validation to track requests/responses.
1212
class App < SimpleDelegator
13-
def initialize(app, api:, validate_request_after_handling:)
13+
def initialize(app, api:, validate_request_before_handling:)
1414
super(app)
1515
@app = app
1616
@definition = Test[api]
17-
@validate_request_after_handling = validate_request_after_handling
17+
@validate_request_before_handling = validate_request_before_handling
1818
end
1919

2020
def call(env)
2121
request = Rack::Request.new(env)
22-
unless @validate_request_after_handling
22+
if @validate_request_before_handling
2323
env[Test::REQUEST] = @definition.validate_request(request, raise_error: false)
2424
end
2525

2626
response = @app.call(env)
27-
if @validate_request_after_handling
27+
unless @validate_request_before_handling
2828
env[Test::REQUEST] = @definition.validate_request(request, raise_error: false)
2929
end
3030

lib/openapi_first/test/methods.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ def self.included(base)
1212
base.include(AssertionMethod)
1313
end
1414

15-
def self.[](application_under_test = nil, api: nil, validate_request_after_handling: false)
15+
def self.[](application_under_test = nil, api: nil, validate_request_before_handling: false)
1616
mod = Module.new do
1717
def self.included(base)
1818
base.include OpenapiFirst::Test::Methods::AssertionMethod
1919
end
2020
end
21-
mod.define_method(:openapi_first_validate_request_after_handling?) { validate_request_after_handling }
21+
mod.define_method(:openapi_first_validate_request_before_handling?) { validate_request_before_handling }
2222

2323
if api
2424
mod.define_method(:openapi_first_default_api) { api }
@@ -30,7 +30,7 @@ def self.included(base)
3030
mod.define_method(:app) do
3131
OpenapiFirst::Test.app(
3232
application_under_test, api: openapi_first_default_api,
33-
validate_request_after_handling: openapi_first_validate_request_after_handling?
33+
validate_request_before_handling: openapi_first_validate_request_before_handling?
3434
)
3535
end
3636
end

spec/test/methods_spec.rb

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ def last_response = Rack::Response.new
2424
end.to raise_error(OpenapiFirst::Error)
2525
end
2626

27+
it 'runs request validation after handling the request' do
28+
OpenapiFirst::Test.register('./examples/openapi.yaml')
29+
myapp = lambda do |env|
30+
expect(env[OpenapiFirst::Test::REQUEST]).to be_nil
31+
Rack::Response.new('hello').finish
32+
end
33+
34+
minitest_class = Class.new(Minitest::Test) do
35+
include OpenapiFirst::Test::Methods[myapp]
36+
end
37+
38+
expect(minitest_class.included_modules).to include(OpenapiFirst::Test::MinitestHelpers)
39+
test_app = minitest_class.new(1).app
40+
env = Rack::MockRequest.env_for('/')
41+
expect(test_app.call(env)).to eq(Rack::Response.new('hello').finish)
42+
expect(env[OpenapiFirst::Test::REQUEST]).to be_valid
43+
expect(env[OpenapiFirst::Test::RESPONSE]).to be_a(OpenapiFirst::ValidatedResponse)
44+
end
45+
2746
context 'with RSpec' do
2847
context 'with metadata', api: :v1 do
2948
include OpenapiFirst::Test::Methods
@@ -88,34 +107,15 @@ def last_response = Rack::Response.new
88107
end
89108

90109
context 'with [arguments]' do
91-
it 'adds an app method that wraps the default API' do
110+
it 'can run validation before handling the request' do
92111
OpenapiFirst::Test.register('./examples/openapi.yaml')
93112
myapp = lambda do |env|
94113
expect(env[OpenapiFirst::Test::REQUEST]).to be_a(OpenapiFirst::ValidatedRequest)
95114
Rack::Response.new('hello').finish
96115
end
97116

98117
minitest_class = Class.new(Minitest::Test) do
99-
include OpenapiFirst::Test::Methods[myapp]
100-
end
101-
102-
expect(minitest_class.included_modules).to include(OpenapiFirst::Test::MinitestHelpers)
103-
test_app = minitest_class.new(1).app
104-
env = Rack::MockRequest.env_for('/')
105-
expect(test_app.call(env)).to eq(Rack::Response.new('hello').finish)
106-
expect(env[OpenapiFirst::Test::REQUEST]).to be_valid
107-
expect(env[OpenapiFirst::Test::RESPONSE]).to be_a(OpenapiFirst::ValidatedResponse)
108-
end
109-
110-
it 'can run validation after handling the request, for the default API' do
111-
OpenapiFirst::Test.register('./examples/openapi.yaml')
112-
myapp = lambda do |env|
113-
expect(env[OpenapiFirst::Test::REQUEST]).to be_nil
114-
Rack::Response.new('hello').finish
115-
end
116-
117-
minitest_class = Class.new(Minitest::Test) do
118-
include OpenapiFirst::Test::Methods[myapp, validate_request_after_handling: true]
118+
include OpenapiFirst::Test::Methods[myapp, validate_request_before_handling: true]
119119
end
120120

121121
expect(minitest_class.included_modules).to include(OpenapiFirst::Test::MinitestHelpers)
@@ -129,7 +129,7 @@ def last_response = Rack::Response.new
129129
it 'adds an app method that wraps the app for a specific API' do
130130
OpenapiFirst::Test.register('./examples/openapi.yaml', as: :v1)
131131
myapp = lambda do |env|
132-
expect(env[OpenapiFirst::Test::REQUEST]).to be_a(OpenapiFirst::ValidatedRequest)
132+
expect(env[OpenapiFirst::Test::REQUEST]).to be_nil
133133
Rack::Response.new('hello').finish
134134
end
135135

@@ -145,15 +145,15 @@ def last_response = Rack::Response.new
145145
expect(env[OpenapiFirst::Test::RESPONSE]).to be_a(OpenapiFirst::ValidatedResponse)
146146
end
147147

148-
it 'can run validation after handling the request, for a specific API' do
148+
it 'can run validation before handling the request, for a specific API' do
149149
OpenapiFirst::Test.register('./examples/openapi.yaml', as: :v1)
150150
myapp = lambda do |env|
151-
expect(env[OpenapiFirst::Test::REQUEST]).to be_nil
151+
expect(env[OpenapiFirst::Test::REQUEST]).to be_a(OpenapiFirst::ValidatedRequest)
152152
Rack::Response.new('hello').finish
153153
end
154154

155155
minitest_class = Class.new(Minitest::Test) do
156-
include OpenapiFirst::Test::Methods[myapp, api: :v1, validate_request_after_handling: true]
156+
include OpenapiFirst::Test::Methods[myapp, api: :v1, validate_request_before_handling: true]
157157
end
158158

159159
expect(minitest_class.included_modules).to include(OpenapiFirst::Test::MinitestHelpers)

0 commit comments

Comments
 (0)