Sha256: da39c1f645715c1a3a0a4f06e356a00067a73101212dec293e2432f1409edcb7

Contents?: true

Size: 1.11 KB

Versions: 20

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

module ActionDispatch
  module TestHelpers
    module PageDumpHelper
      class InvalidResponse < StandardError; end

      # Saves the content of response body to a file and tries to open it in your browser.
      # Launchy must be present in your Gemfile for the page to open automatically.
      def save_and_open_page(path = html_dump_default_path)
        save_page(path).tap { |s_path| open_file(s_path) }
      end

      private
        def save_page(path = html_dump_default_path)
          raise InvalidResponse.new("Response is a redirection!") if response.redirection?
          path = Pathname.new(path)
          path.dirname.mkpath
          File.write(path, response.body)
          path
        end

        def open_file(path)
          require "launchy"
          Launchy.open(path)
        rescue LoadError
          warn "File saved to #{path}.\nPlease install the launchy gem to open the file automatically."
        end

        def html_dump_default_path
          Rails.root.join("tmp/html_dump", "#{method_name}_#{DateTime.current.to_i}.html").to_s
        end
    end
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
actionpack-8.0.0 lib/action_dispatch/testing/test_helpers/page_dump_helper.rb
actionpack-7.2.2 lib/action_dispatch/testing/test_helpers/page_dump_helper.rb
actionpack-8.0.0.rc2 lib/action_dispatch/testing/test_helpers/page_dump_helper.rb
actionpack-7.2.1.2 lib/action_dispatch/testing/test_helpers/page_dump_helper.rb
actionpack-8.0.0.rc1 lib/action_dispatch/testing/test_helpers/page_dump_helper.rb
actionpack-7.2.1.1 lib/action_dispatch/testing/test_helpers/page_dump_helper.rb
actionpack-8.0.0.beta1 lib/action_dispatch/testing/test_helpers/page_dump_helper.rb
omg-actionpack-8.0.0.alpha9 lib/action_dispatch/testing/test_helpers/page_dump_helper.rb
omg-actionpack-8.0.0.alpha8 lib/action_dispatch/testing/test_helpers/page_dump_helper.rb
omg-actionpack-8.0.0.alpha7 lib/action_dispatch/testing/test_helpers/page_dump_helper.rb
omg-actionpack-8.0.0.alpha4 lib/action_dispatch/testing/test_helpers/page_dump_helper.rb
omg-actionpack-8.0.0.alpha3 lib/action_dispatch/testing/test_helpers/page_dump_helper.rb
omg-actionpack-8.0.0.alpha2 lib/action_dispatch/testing/test_helpers/page_dump_helper.rb
omg-actionpack-8.0.0.alpha1 lib/action_dispatch/testing/test_helpers/page_dump_helper.rb
actionpack-7.2.1 lib/action_dispatch/testing/test_helpers/page_dump_helper.rb
actionpack-7.2.0 lib/action_dispatch/testing/test_helpers/page_dump_helper.rb
actionpack-7.2.0.rc1 lib/action_dispatch/testing/test_helpers/page_dump_helper.rb
actionpack-7.2.0.beta3 lib/action_dispatch/testing/test_helpers/page_dump_helper.rb
actionpack-7.2.0.beta2 lib/action_dispatch/testing/test_helpers/page_dump_helper.rb
actionpack-7.2.0.beta1 lib/action_dispatch/testing/test_helpers/page_dump_helper.rb