File: spec_helpers.rb

package info (click to toggle)
ruby-sinatra 4.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 1,932 kB
  • sloc: ruby: 17,700; sh: 25; makefile: 8
file content (40 lines) | stat: -rw-r--r-- 880 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true

require 'forwardable'

module SpecHelpers
  extend Forwardable
  def_delegators :last_response, :body, :headers, :status, :errors
  def_delegators :current_session, :env_for
  attr_writer :app

  def app
    @app ||= nil
    @app || mock_app(DummyApp)
  end

  def mock_app(app = nil, lint: true, &block)
    app = block if app.nil? && (block.arity == 1)
    if app
      klass = described_class
      mock_app do
        use Rack::Head
        use(Rack::Config) { |e| e['rack.session'] ||= {} }
        use klass
        run app
      end
    elsif lint
      @app = Rack::Lint.new Rack::Builder.new(&block).to_app
    else
      @app = Rack::Builder.new(&block).to_app
    end
  end

  def with_headers(headers)
    proc { [200, { 'content-type' => 'text/plain' }.merge(headers), ['ok']] }
  end

  def env
    Thread.current[:last_env]
  end
end