Sha256: 573d89a7d5e8fb22ced8aeb1476329a9bda5ea8f3fcc207b4aa2ceae19a02269

Contents?: true

Size: 1.13 KB

Versions: 45

Compression:

Stored size: 1.13 KB

Contents

module Rack
  module PactBroker

    # If the HTML and the CSV group resources are both requested by the browser,
    # Chrome gets confused by the content types, and when you click back, it tries to load the CSV
    # instead of the HTML page. So we have to give the CSV resource a different URL (.csv)

    class ConvertFileExtensionToAcceptHeader

      EXTENSIONS = {".csv" => "text/csv"}
      EXTENSION_REGEXP = /\.\w+$/

      def initialize app
        @app = app
      end

      def call env
        file_extension = extension(env)
        if convert_to_accept_header? file_extension
          @app.call(set_accept_header_and_path_info(env, file_extension))
        else
          @app.call(env)
        end
      end

      def convert_to_accept_header? file_extension
        EXTENSIONS[file_extension]
      end

      def extension env
        env["PATH_INFO"] =~ EXTENSION_REGEXP && $~[0]
      end

      def set_accept_header_and_path_info env, file_extension
        env.merge(
          "PATH_INFO" => env["PATH_INFO"].gsub(EXTENSION_REGEXP, ''),
          "HTTP_ACCEPT" => EXTENSIONS[file_extension]
        )
      end

    end

  end
end

Version data entries

45 entries across 45 versions & 1 rubygems

Version Path
pact_broker-2.2.0 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.1.1 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.1.0 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.0.5 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.0.4 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.0.3 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.0.2 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.0.1 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.0.0 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.0.0.beta.8 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.0.0.beta.7 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.0.0.beta.6 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-1.18.0 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.0.0.beta.5 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-1.18.0.beta.1 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-1.17.2 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-1.17.1 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.0.0.beta.4 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.0.0.beta.3 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb
pact_broker-2.0.0.beta.2 lib/rack/pact_broker/convert_file_extension_to_accept_header.rb