Sha256: ab62c51c49d267e548ca876d687c12e6c11096822cc29c78ac1fcf90a604cac1

Contents?: true

Size: 837 Bytes

Versions: 3

Compression:

Stored size: 837 Bytes

Contents

# frozen_string_literal: true

require 'faraday'

module Mock
  module Twilio
    module Middleware
      class Proxy < Faraday::Middleware
        def initialize(app)
          super(app)
        end

        def call(env)
          env.url.host = env.request.proxy.host
          env.url.port = env.request.proxy.port
          env.url.scheme = env.request.proxy.scheme
          env.request_headers.merge!({ 'Prefer' => 'code=429' }) if forced_error(env.request_body)
          super
        end

        def forced_error(request_body)
          return unless request_body && request_body["Body"]

          string = ::Rack::Utils.parse_nested_query(request_body)
          string["Body"].downcase.include?('error')
        end
      end
    end
  end
end

Faraday::Request.register_middleware(proxy: Mock::Twilio::Middleware::Proxy)

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mock-twilio-1.4 lib/mock/twilio/middleware/proxy.rb
mock-twilio-1.3.2 lib/mock/twilio/middleware/proxy.rb
mock-twilio-1.3.1 lib/mock/twilio/middleware/proxy.rb