Sha256: e46d4c4c778a10734a8f369b6253f236670138605663e605d0cc566be068053c

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

require "action_view"
require "action_dispatch" # This is needed to use Mime::Type
require "web_console"
require "web_console/testing/helper"

module WebConsole
  module Testing
    class FakeMiddleware
      I18n.load_path.concat(Dir[Helper.gem_root.join("lib/web_console/locales/*.yml")])

      DEFAULT_HEADERS = { Rack::CONTENT_TYPE => "application/javascript" }

      def initialize(opts)
        @headers        = opts.fetch(:headers, DEFAULT_HEADERS)
        @req_path_regex = opts[:req_path_regex]
        @view_path      = opts[:view_path]
      end

      def call(env)
        body = render(req_path(env))
        @headers[Rack::CONTENT_LENGTH] = body.bytesize.to_s

        [ 200, @headers, [ body ] ]
      end

      def view
        @view = View.with_empty_template_cache.with_view_paths(@view_path)
      end

      private

        # extract target path from REQUEST_PATH
        def req_path(env)
          File.basename(env["REQUEST_PATH"].match(@req_path_regex)[1], ".*")
        end

        def render(template)
          view.render(template: template, layout: nil)
        end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/web-console-4.2.1/lib/web_console/testing/fake_middleware.rb
web-console-4.2.1 lib/web_console/testing/fake_middleware.rb