Class: OpenapiFirst::Test::App

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/openapi_first/test/app.rb

Overview

A wrapper of the original app with silent request/response validation to track requests/responses.

Instance Method Summary collapse

Constructor Details

#initialize(app, api:, validate_request_before_handling:) ⇒ App

Returns a new instance of App.



13
14
15
16
17
18
# File 'lib/openapi_first/test/app.rb', line 13

def initialize(app, api:, validate_request_before_handling:)
  super(app)
  @app = app
  @definition = Test[api]
  @validate_request_before_handling = validate_request_before_handling
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/openapi_first/test/app.rb', line 20

def call(env)
  request = Rack::Request.new(env)
  if @validate_request_before_handling
    env[Test::REQUEST] = @definition.validate_request(request, raise_error: false)
  end

  response = @app.call(env)
  unless @validate_request_before_handling
    env[Test::REQUEST] = @definition.validate_request(request, raise_error: false)
  end

  status, headers, body = response
  env[Test::RESPONSE] =
    @definition.validate_response(request, Rack::Response[status, headers, body], raise_error: false)
  response
end