Sha256: 9f3011ebd8dc508175acbf71fc1df10eb83dfdf0b9d1317e8174254b3ed303cd

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true
require 'pact_broker/api/paths'
require 'pact_broker/configuration'

module Rack
  module PactBroker
    class HalBrowserRedirect
      include ::PactBroker::Api::Paths

      def initialize(app, options = {}, &block)
        @app = app
      end

      def call(env)
        if redirect?(env)
          return [303, {'Location' => hal_browser_url_from_request(env)}, []]
        else
          app.call(env)
        end
      end

      private

      attr_reader :app

      def redirect?(env)
        is_get_for_html?(env) && is_verification_results_path?(env["PATH_INFO"])
      end

      def is_get_for_html?(env)
        env["REQUEST_METHOD"] == "GET" && env["HTTP_ACCEPT"]&.include?("text/html")
      end

      # Doesn't support non root URLs currently
      def hal_browser_url_from_request(env)
        url = URI.parse("#{env["pactbroker.base_url"]}/hal-browser/browser.html")
        url.fragment = "#{env["pactbroker.base_url"]}#{env["PATH_INFO"]}"
        url.to_s
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pact_broker-2.76.0 lib/rack/pact_broker/hal_browser_redirect.rb