Module: OpenapiRuby::Testing::Transport

Defined in:
lib/openapi_ruby/testing/transport.rb

Overview

Bridges the adapters onto whichever request-issuing API the host's test context provides. Rails integration tests take keyword arguments and expose response; rack-test (Hanami, Sinatra, bare Rack) takes positional arguments plus a Rack env and exposes last_response.

Both response objects answer #status, #body and #headers, so callers need no further normalisation.

Defined Under Namespace

Classes: RackTest, RailsIntegration

Class Method Summary collapse

Class Method Details

.describe(context) ⇒ Object



46
47
48
# File 'lib/openapi_ruby/testing/transport.rb', line 46

def self.describe(context)
  context.is_a?(Class) ? context.name.to_s : context.class.name.to_s
end

.for(context) ⇒ Object

Rails is checked first: a suite can include Rack::Test::Methods alongside the integration helpers, and in that case the Rails session is the one that boots the app under test.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/openapi_ruby/testing/transport.rb', line 16

def self.for(context)
  if context.respond_to?(:integration_session)
    RailsIntegration.new(context)
  elsif context.respond_to?(:last_response)
    require_app!(context)
    RackTest.new(context)
  elsif OpenapiRuby.rails_host?
    # A hand-rolled harness that defines the verb methods itself. Left
    # working rather than second-guessed.
    RailsIntegration.new(context)
  else
    # Without rack-test, dispatch would land on the example-group DSL's
    # own `get`, and the user would get told that `get` is unavailable
    # inside an example — true, and no help at all here.
    raise OpenapiRuby::Error,
      "openapi_ruby found no way to issue requests from #{describe(context)}. " \
      "Add rack-test to your bundle, `include Rack::Test::Methods`, and define `app`."
  end
end

.require_app!(context) ⇒ Object

rack-test resolves app lazily, so a missing one surfaces as a bare NameError from inside the gem rather than as the setup mistake it is.

Raises:



38
39
40
41
42
43
44
# File 'lib/openapi_ruby/testing/transport.rb', line 38

def self.require_app!(context)
  return if context.respond_to?(:app)

  raise OpenapiRuby::Error,
    "openapi_ruby needs the Rack app under test in #{describe(context)}. " \
    "Define it as `let(:app) { MyApp }` in RSpec, or an `app` method in Minitest."
end