File: setup_env_spec.rb

package info (click to toggle)
ruby-flipper 0.26.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,288 kB
  • sloc: ruby: 16,377; sh: 61; javascript: 24; makefile: 14
file content (94 lines) | stat: -rw-r--r-- 2,555 bytes parent folder | download | duplicates (2)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
RSpec.describe Flipper::Middleware::SetupEnv do
  context 'with flipper instance' do
    let(:app) do
      app = lambda do |env|
        [200, { 'Content-Type' => 'text/html' }, [env['flipper'].object_id.to_s]]
      end
      builder = Rack::Builder.new
      builder.use described_class, flipper
      builder.run app
      builder
    end

    it 'sets flipper in env' do
      get '/'
      expect(last_response.body).to eq(flipper.object_id.to_s)
    end
  end

  context 'with block that returns flipper instance' do
    let(:flipper_block) do
      -> { flipper }
    end
    let(:app) do
      app = lambda do |env|
        [200, { 'Content-Type' => 'text/html' }, [env['flipper'].object_id.to_s]]
      end
      builder = Rack::Builder.new
      builder.use described_class, flipper_block
      builder.run app
      builder
    end

    it 'sets flipper in env' do
      get '/'
      expect(last_response.body).to eq(flipper.object_id.to_s)
    end
  end

  context 'when env already has flipper setup' do
    let(:app) do
      app = lambda do |env|
        [200, { 'Content-Type' => 'text/html' }, [env['flipper'].object_id.to_s]]
      end
      builder = Rack::Builder.new
      builder.use described_class, flipper
      builder.run app
      builder
    end

    it 'leaves env flipper alone' do
      env_flipper = build_flipper
      get '/', {}, 'flipper' => env_flipper
      expect(last_response.body).to eq(env_flipper.object_id.to_s)
    end
  end

  context 'when flipper instance or block are nil but env flipper is configured' do
    let(:app) do
      app = lambda do |env|
        [200, { 'Content-Type' => 'text/html' }, [env['flipper'].object_id.to_s]]
      end
      builder = Rack::Builder.new
      builder.use described_class
      builder.run app
      builder
    end

    it 'can use env flipper' do
      env_flipper = build_flipper
      get '/', {}, 'flipper' => env_flipper
      expect(last_response.body).to eq(env_flipper.object_id.to_s)
    end
  end

  context 'when flipper instance or block are nil and default Flipper is configured' do
    let(:app) do
      Flipper.configure do |config|
        config.default { flipper }
      end
      app = lambda do |env|
        [200, { 'Content-Type' => 'text/html' }, [env['flipper'].object_id.to_s]]
      end
      builder = Rack::Builder.new
      builder.use described_class
      builder.run app
      builder
    end

    it 'can use env flipper' do
      get '/', {}, {}
      expect(last_response.body).to eq(Flipper.object_id.to_s)
    end
  end
end