File: api_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 (113 lines) | stat: -rw-r--r-- 3,063 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
RSpec.describe Flipper::Api do
  describe 'Initializing middleware with flipper instance' do
    let(:app) { build_api(flipper) }

    it 'works' do
      flipper.enable :a
      flipper.disable :b

      get '/features'

      expect(last_response.status).to be(200)
      feature_names = json_response.fetch('features').map { |feature| feature.fetch('key') }
      expect(feature_names).to eq(%w(a b))
    end
  end

  describe 'Initializing middleware lazily with a block' do
    let(:app) { build_api(-> { flipper }) }

    it 'works' do
      flipper.enable :a
      flipper.disable :b

      get '/features'

      expect(last_response.status).to be(200)
      feature_names = json_response.fetch('features').map { |feature| feature.fetch('key') }
      expect(feature_names).to eq(%w(a b))
    end
  end

  context 'when initialized with flipper instance and flipper instance in env' do
    let(:app) { build_api(flipper) }

    it 'uses env instance over initialized instance' do
      flipper[:built_a].enable
      flipper[:built_b].disable

      env_flipper = build_flipper
      env_flipper[:env_a].enable
      env_flipper[:env_b].disable

      params = {}
      env = {
        'flipper' => env_flipper,
      }
      get '/features', params, env

      expect(last_response.status).to eq(200)
      feature_names = json_response.fetch('features').map { |feature| feature.fetch('key') }
      expect(feature_names).to eq(%w(env_a env_b))
    end
  end

  context 'when initialized without flipper instance but flipper instance in env' do
    let(:app) { described_class.app }

    it 'uses env instance' do
      flipper[:a].enable
      flipper[:b].disable

      params = {}
      env = {
        'flipper' => flipper,
      }
      get '/features', params, env

      expect(last_response.status).to eq(200)
      feature_names = json_response.fetch('features').map { |feature| feature.fetch('key') }
      expect(feature_names).to eq(%w(a b))
    end
  end

  context 'when initialized with env_key' do
    let(:app) { build_api(flipper, env_key: 'flipper_api') }

    it 'uses provided env key instead of default' do
      flipper[:a].enable
      flipper[:b].disable

      default_env_flipper = build_flipper
      default_env_flipper[:env_a].enable
      default_env_flipper[:env_b].disable

      params = {}
      env = {
        'flipper' => default_env_flipper,
        'flipper_api' => flipper,
      }
      get '/features', params, env

      expect(last_response.status).to eq(200)
      feature_names = json_response.fetch('features').map { |feature| feature.fetch('key') }
      expect(feature_names).to eq(%w(a b))
    end
  end

  context "when request does not match any api routes" do
    let(:app) { build_api(flipper) }

    it "returns 404" do
      get '/gibberish'
      expect(last_response.status).to eq(404)
      expect(json_response).to eq({})
    end
  end

  describe 'Inspecting the built Rack app' do
    it 'returns a String' do
      expect(build_api(flipper).inspect).to eq("Flipper::Api")
    end
  end
end