File: http_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 (273 lines) | stat: -rw-r--r-- 9,135 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
require 'flipper/adapters/http'
require 'flipper/adapters/pstore'
require 'rack/handler/webrick'

FLIPPER_SPEC_API_PORT = ENV.fetch('FLIPPER_SPEC_API_PORT', 9001).to_i

RSpec.describe Flipper::Adapters::Http do
  context 'adapter' do
    subject do
      described_class.new(url: "http://localhost:#{FLIPPER_SPEC_API_PORT}")
    end

    before :all do
      dir = FlipperRoot.join('tmp').tap(&:mkpath)
      log_path = dir.join('flipper_adapters_http_spec.log')
      @pstore_file = dir.join('flipper.pstore')
      @pstore_file.unlink if @pstore_file.exist?

      api_adapter = Flipper::Adapters::PStore.new(@pstore_file)
      flipper_api = Flipper.new(api_adapter)
      app = Flipper::Api.app(flipper_api)
      server_options = {
        Port: FLIPPER_SPEC_API_PORT,
        StartCallback: -> { @started = true },
        Logger: WEBrick::Log.new(log_path.to_s, WEBrick::Log::INFO),
        AccessLog: [
          [log_path.open('w'), WEBrick::AccessLog::COMBINED_LOG_FORMAT],
        ],
      }
      @server = WEBrick::HTTPServer.new(server_options)
      @server.mount '/', Rack::Handler::WEBrick, app

      Thread.new { @server.start }
      Timeout.timeout(1) { :wait until @started }
    end

    after :all do
      @server.shutdown if @server
    end

    before(:each) do
      @pstore_file.unlink if @pstore_file.exist?
    end

    it_should_behave_like 'a flipper adapter'

    it "can enable and disable unregistered group" do
      flipper = Flipper.new(subject)
      expect(flipper[:search].enable_group(:some_made_up_group)).to be(true)
      expect(flipper[:search].groups_value).to eq(Set["some_made_up_group"])

      expect(flipper[:search].disable_group(:some_made_up_group)).to be(true)
      expect(flipper[:search].groups_value).to eq(Set.new)
    end
  end

  it "sends default headers" do
    headers = {
      'Accept' => 'application/json',
      'Content-Type' => 'application/json',
      'User-Agent' => "Flipper HTTP Adapter v#{Flipper::VERSION}",
    }
    stub_request(:get, "http://app.com/flipper/features/feature_panel")
      .with(headers: headers)
      .to_return(status: 404, body: "", headers: {})

    adapter = described_class.new(url: 'http://app.com/flipper')
    adapter.get(flipper[:feature_panel])
  end

  describe "#get" do
    it "raises error when not successful response" do
      stub_request(:get, "http://app.com/flipper/features/feature_panel")
        .to_return(status: 503, body: "", headers: {})

      adapter = described_class.new(url: 'http://app.com/flipper')
      expect {
        adapter.get(flipper[:feature_panel])
      }.to raise_error(Flipper::Adapters::Http::Error)
    end
  end

  describe "#get_multi" do
    it "raises error when not successful response" do
      stub_request(:get, "http://app.com/flipper/features?keys=feature_panel")
        .to_return(status: 503, body: "", headers: {})

      adapter = described_class.new(url: 'http://app.com/flipper')
      expect {
        adapter.get_multi([flipper[:feature_panel]])
      }.to raise_error(Flipper::Adapters::Http::Error)
    end
  end

  describe "#get_all" do
    it "raises error when not successful response" do
      stub_request(:get, "http://app.com/flipper/features")
        .to_return(status: 503, body: "", headers: {})

      adapter = described_class.new(url: 'http://app.com/flipper')
      expect {
        adapter.get_all
      }.to raise_error(Flipper::Adapters::Http::Error)
    end
  end

  describe "#features" do
    it "raises error when not successful response" do
      stub_request(:get, "http://app.com/flipper/features")
        .to_return(status: 503, body: "", headers: {})

      adapter = described_class.new(url: 'http://app.com/flipper')
      expect {
        adapter.features
      }.to raise_error(Flipper::Adapters::Http::Error)
    end
  end

  describe "#add" do
    it "raises error when not successful" do
      stub_request(:post, /app.com/)
        .to_return(status: 503, body: "{}", headers: {})

      adapter = described_class.new(url: 'http://app.com/flipper')
      expect {
        adapter.add(Flipper::Feature.new(:search, adapter))
      }.to raise_error(Flipper::Adapters::Http::Error)
    end
  end

  describe "#remove" do
    it "raises error when not successful" do
      stub_request(:delete, /app.com/)
        .to_return(status: 503, body: "{}", headers: {})

      adapter = described_class.new(url: 'http://app.com/flipper')
      expect {
        adapter.remove(Flipper::Feature.new(:search, adapter))
      }.to raise_error(Flipper::Adapters::Http::Error)
    end
  end

  describe "#clear" do
    it "raises error when not successful" do
      stub_request(:delete, /app.com/)
        .to_return(status: 503, body: "{}", headers: {})

      adapter = described_class.new(url: 'http://app.com/flipper')
      expect {
        adapter.clear(Flipper::Feature.new(:search, adapter))
      }.to raise_error(Flipper::Adapters::Http::Error)
    end
  end

  describe "#enable" do
    it "raises error when not successful" do
      stub_request(:post, /app.com/)
        .to_return(status: 503, body: "{}", headers: {})

      adapter = described_class.new(url: 'http://app.com/flipper')
      feature = Flipper::Feature.new(:search, adapter)
      gate = feature.gate(:boolean)
      thing = gate.wrap(true)
      expect {
        adapter.enable(feature, gate, thing)
      }.to raise_error(Flipper::Adapters::Http::Error, "Failed with status: 503")
    end

    it "doesn't raise json error if body cannot be parsed" do
      stub_request(:post, /app.com/)
        .to_return(status: 503, body: "barf", headers: {})

      adapter = described_class.new(url: 'http://app.com/flipper')
      feature = Flipper::Feature.new(:search, adapter)
      gate = feature.gate(:boolean)
      thing = gate.wrap(true)
      expect {
        adapter.enable(feature, gate, thing)
      }.to raise_error(Flipper::Adapters::Http::Error)
    end

    it "includes response information if available when raising error" do
      api_response = {
        "code" => "error",
        "message" => "This feature has reached the limit to the number of " +
                     "actors per feature. Check out groups as a more flexible " +
                     "way to enable many actors.",
        "more_info" => "https://www.flippercloud.io/docs",
      }
      stub_request(:post, /app.com/)
        .to_return(status: 503, body: JSON.dump(api_response), headers: {})

      adapter = described_class.new(url: 'http://app.com/flipper')
      feature = Flipper::Feature.new(:search, adapter)
      gate = feature.gate(:boolean)
      thing = gate.wrap(true)
      error_message = "Failed with status: 503\n\nThis feature has reached the " +
                      "limit to the number of actors per feature. Check out " +
                      "groups as a more flexible way to enable many actors.\n" +
                      "https://www.flippercloud.io/docs"
      expect {
        adapter.enable(feature, gate, thing)
      }.to raise_error(Flipper::Adapters::Http::Error, error_message)
    end
  end

  describe "#disable" do
    it "raises error when not successful" do
      stub_request(:delete, /app.com/)
        .to_return(status: 503, body: "{}", headers: {})

      adapter = described_class.new(url: 'http://app.com/flipper')
      feature = Flipper::Feature.new(:search, adapter)
      gate = feature.gate(:boolean)
      thing = gate.wrap(false)
      expect {
        adapter.disable(feature, gate, thing)
      }.to raise_error(Flipper::Adapters::Http::Error)
    end
  end

  describe 'configuration' do
    let(:debug_output) { object_double($stderr) }
    let(:options) do
      {
        url: 'http://app.com/mount-point',
        headers: { 'X-Custom-Header' => 'foo' },
        basic_auth_username: 'username',
        basic_auth_password: 'password',
        read_timeout: 100,
        open_timeout: 40,
        write_timeout: 40,
        debug_output: debug_output,
      }
    end
    subject { described_class.new(options) }
    let(:feature) { flipper[:feature_panel] }

    before do
      stub_request(:get, %r{\Ahttp://app.com*}).
        to_return(body: fixture_file('feature.json'))
    end

    it 'allows client to set request headers' do
      subject.get(feature)
      expect(
        a_request(:get, 'http://app.com/mount-point/features/feature_panel')
        .with(headers: { 'X-Custom-Header' => 'foo' })
      ).to have_been_made.once
    end

    it 'allows client to set basic auth' do
      subject.get(feature)
      expect(
        a_request(:get, 'http://app.com/mount-point/features/feature_panel')
        .with(basic_auth: %w(username password))
      ).to have_been_made.once
    end

    it 'allows client to set debug output' do
      user_agent = Net::HTTP.new("app.com")
      allow(Net::HTTP).to receive(:new).and_return(user_agent)

      expect(user_agent).to receive(:set_debug_output).with(debug_output)
      subject.get(feature)
    end
  end

  def fixture_file(name)
    fixtures_path = File.expand_path('../../../fixtures', __FILE__)
    File.new(fixtures_path + '/' + name)
  end
end