Sha256: 1472e7c79b076a4ebf11af62981b8844b87b486a2605622e5827769323ff36db

Contents?: true

Size: 1.74 KB

Versions: 76

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

# Rack app used to test the Rack adapter.
# Uses Webmock to check if requests are registered, in which case it returns
# the registered response.
class WebmockRackApp
  def call(env)
    req_signature = WebMock::RequestSignature.new(
      req_method(env),
      req_uri(env),
      body: req_body(env),
      headers: req_headers(env)
    )

    WebMock::RequestRegistry
      .instance
      .requested_signatures
      .put(req_signature)

    process_response(req_signature)
  end

  def req_method(env)
    env['REQUEST_METHOD'].downcase.to_sym
  end

  def req_uri(env)
    scheme = env['rack.url_scheme']
    host = env['SERVER_NAME']
    port = env['SERVER_PORT']
    path = env['PATH_INFO']
    query = env['QUERY_STRING']

    url = +"#{scheme}://#{host}:#{port}#{path}"
    url += "?#{query}" if query

    uri = WebMock::Util::URI.heuristic_parse(url)
    uri.path = uri.normalized_path.gsub('[^:]//', '/')
    uri
  end

  def req_headers(env)
    http_headers = env.select { |k, _| k.start_with?('HTTP_') }
                      .map { |k, v| [k[5..-1], v] }
                      .to_h

    special_headers = Faraday::Adapter::Rack::SPECIAL_HEADERS
    http_headers.merge(env.select { |k, _| special_headers.include?(k) })
  end

  def req_body(env)
    env['rack.input'].read
  end

  def process_response(req_signature)
    res = WebMock::StubRegistry.instance.response_for_request(req_signature)

    if res.nil? && req_signature.uri.host == 'localhost'
      raise Faraday::ConnectionFailed, 'Trying to connect to localhost'
    end

    raise WebMock::NetConnectNotAllowedError, req_signature unless res

    raise Faraday::TimeoutError if res.should_timeout

    [res.status[0], res.headers || {}, [res.body || '']]
  end
end

Version data entries

76 entries across 63 versions & 15 rubygems

Version Path
faraday-1.10.4 spec/support/webmock_rack_app.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/faraday-1.2.0/spec/support/webmock_rack_app.rb
avalara_sdk-24.2.29 vendor/bundle/ruby/2.7.0/gems/faraday-1.10.3/spec/support/webmock_rack_app.rb
faraday-1.10.3 spec/support/webmock_rack_app.rb
faraday-1.10.2 spec/support/webmock_rack_app.rb
faraday-1.10.1 spec/support/webmock_rack_app.rb
alloy_sdk-0.1.0 vendor/bundle/ruby/3.0.0/gems/faraday-1.0.1/spec/support/webmock_rack_app.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/faraday-1.10.0/spec/support/webmock_rack_app.rb
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/faraday-1.10.0/spec/support/webmock_rack_app.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/faraday-1.10.0/spec/support/webmock_rack_app.rb
faraday-1.10.0 spec/support/webmock_rack_app.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/faraday-1.9.3/spec/support/webmock_rack_app.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/faraday-1.8.0/spec/support/webmock_rack_app.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/faraday-1.9.3/spec/support/webmock_rack_app.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/faraday-1.9.3/spec/support/webmock_rack_app.rb
faraday-1.9.3 spec/support/webmock_rack_app.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/faraday-1.8.0/spec/support/webmock_rack_app.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/faraday-1.4.2/spec/support/webmock_rack_app.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/faraday-1.8.0/spec/support/webmock_rack_app.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/faraday-1.8.0/spec/support/webmock_rack_app.rb