Sha256: cc898649aca40ca33377ce4b23a00d4bb7c885b0735b666947ea7731006bc406

Contents?: true

Size: 1.25 KB

Versions: 46

Compression:

Stored size: 1.25 KB

Contents

module Rack
  module PactBroker
    class SetBaseUrl
      X_FORWARDED_PATTERN = /_X_FORWARDED_/.freeze

      def initialize app, base_urls
        @app = app
        @base_urls = base_urls
      end

      def call(env)
        if env["pactbroker.base_url"]
          app.call(env)
        else
          app.call(env.merge("pactbroker.base_url" => select_matching_base_url(env)))
        end
      end

      private

      attr_reader :app, :base_urls

      def select_matching_base_url(env)
        if base_urls.size > 1
          return matching_base_url_considering_x_forwarded_headers(env) ||
            matching_base_url_not_considering_x_forwarded_headers(env) ||
            default_base_url
        end
        default_base_url
      end

      def default_base_url
        base_urls.first
      end

      def matching_base_url_considering_x_forwarded_headers(env)
        matching_base_url(env)
      end

      def matching_base_url_not_considering_x_forwarded_headers(env)
        matching_base_url(env.reject{ |k, _| k =~ X_FORWARDED_PATTERN} )
      end

      def matching_base_url(env)
        request_base_url = Rack::Request.new(env).base_url
        if base_urls.include?(request_base_url)
          request_base_url
        end
      end
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
pact_broker-2.112.0 lib/rack/pact_broker/set_base_url.rb
pact_broker-2.111.0 lib/rack/pact_broker/set_base_url.rb
pact_broker-2.109.1 lib/rack/pact_broker/set_base_url.rb
pact_broker-2.109.0 lib/rack/pact_broker/set_base_url.rb
pact_broker-2.108.0 lib/rack/pact_broker/set_base_url.rb
pact_broker-2.107.1 lib/rack/pact_broker/set_base_url.rb
pact_broker-2.107.0 lib/rack/pact_broker/set_base_url.rb
pact_broker-2.107.0.beta.1 lib/rack/pact_broker/set_base_url.rb
pact_broker-2.106.0 lib/rack/pact_broker/set_base_url.rb
pact_broker-2.105.0 lib/rack/pact_broker/set_base_url.rb
pact_broker-2.104.0 lib/rack/pact_broker/set_base_url.rb
pact_broker-2.103.0 lib/rack/pact_broker/set_base_url.rb
pact_broker-2.102.2 lib/rack/pact_broker/set_base_url.rb
pact_broker-2.102.1 lib/rack/pact_broker/set_base_url.rb
pact_broker-2.102.0 lib/rack/pact_broker/set_base_url.rb
pact_broker-2.101.0 lib/rack/pact_broker/set_base_url.rb
pact_broker-2.100.0 lib/rack/pact_broker/set_base_url.rb
pact_broker-2.99.0 lib/rack/pact_broker/set_base_url.rb
pact_broker-2.98.0 lib/rack/pact_broker/set_base_url.rb
pact_broker-2.97.0 lib/rack/pact_broker/set_base_url.rb