File: shared_examples.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 (68 lines) | stat: -rw-r--r-- 1,502 bytes parent folder | download | duplicates (4)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# frozen_string_literal: true

RSpec.shared_examples_for 'any rack application' do
  it 'should not interfere with normal get requests' do
    expect(get('/')).to be_ok
    expect(body).to eq('ok')
  end

  it 'should not interfere with normal head requests' do
    expect(head('/')).to be_ok
  end

  it 'should not leak changes to env' do
    klass    = described_class
    detector = Struct.new(:app) do
      def call(env)
        was = env.dup
        res = app.call(env)
        was.each do |k, v|
          next if env[k] == v

          raise "env[#{k.inspect}] changed from #{v.inspect} to #{env[k].inspect}"
        end
        res
      end
    end

    mock_app do
      use Rack::Head
      use(Rack::Config) { |e| e['rack.session'] ||= {} }
      use detector
      use klass
      run DummyApp
    end

    expect(get('/..', foo: '<bar>')).to be_ok
  end

  it 'allows passing on values in env' do
    klass    = described_class
    changer  = Struct.new(:app) do
      def call(env)
        env['foo.bar'] = 42
        app.call(env)
      end
    end
    detector = Struct.new(:app) do
      def call(env)
        app.call(env)
      end
    end

    expect_any_instance_of(detector).to receive(:call).with(
      hash_including('foo.bar' => 42)
    ).and_call_original

    mock_app do
      use Rack::Head
      use(Rack::Config) { |e| e['rack.session'] ||= {} }
      use changer
      use klass
      use detector
      run DummyApp
    end

    expect(get('/')).to be_ok
  end
end