File: json_params_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 (79 lines) | stat: -rw-r--r-- 2,847 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
RSpec.describe Flipper::Api::JsonParams do
  let(:app) do
    app = lambda do |env|
      request = Rack::Request.new(env)
      [200, { 'Content-Type' => 'application/json' }, [JSON.generate(request.params)]]
    end
    builder = Rack::Builder.new
    builder.use described_class
    builder.run app
    builder
  end

  describe 'json post request' do
    it 'adds request body to params' do
      response = post '/',
                      JSON.generate(flipper_id: 'User;2'),
                      'CONTENT_TYPE' => 'application/json'

      params = JSON.parse(response.body)
      expect(params).to eq('flipper_id' => 'User;2')
    end

    it 'handles request bodies with multiple params' do
      response = post '/',
                      JSON.generate(flipper_id: 'User;2', language: 'ruby'),
                      'CONTENT_TYPE' => 'application/json'

      params = JSON.parse(response.body)
      expect(params).to eq('flipper_id' => 'User;2', 'language' => 'ruby')
    end

    it 'handles request bodies and single query string params' do
      response = post '/?language=ruby',
                      JSON.generate(flipper_id: 'User;2'),
                      'CONTENT_TYPE' => 'application/json'

      params = JSON.parse(response.body)
      expect(params).to eq('flipper_id' => 'User;2', 'language' => 'ruby')
    end

    it 'handles request bodies and multiple query string params' do
      response = post '/?language=ruby&framework=rails',
                      JSON.generate(flipper_id: 'User;2'),
                      'CONTENT_TYPE' => 'application/json'

      params = JSON.parse(response.body)
      expect(params).to eq('flipper_id' => 'User;2', 'language' => 'ruby', 'framework' => 'rails')
    end

    it 'favors request body params' do
      response = post '/?language=javascript',
                      JSON.generate(flipper_id: 'User;2', language: 'ruby'),
                      'CONTENT_TYPE' => 'application/json'

      params = JSON.parse(response.body)
      expect(params).to eq('flipper_id' => 'User;2', 'language' => 'ruby')
    end
  end

  describe 'url-encoded request' do
    it 'handles params the same as a json request' do
      response = post '/', flipper_id: 'User;2'
      params = JSON.parse(response.body)
      expect(params).to eq('flipper_id' => 'User;2')
    end

    it 'handles single query string params' do
      response = post '/?language=ruby', flipper_id: 'User;2'
      params = JSON.parse(response.body)
      expect(params).to eq('flipper_id' => 'User;2', 'language' => 'ruby')
    end

    it 'handles multiple query string params' do
      response = post '/?language=ruby&framework=rails', flipper_id: 'User;2'
      params = JSON.parse(response.body)
      expect(params).to eq('flipper_id' => 'User;2', 'language' => 'ruby', 'framework' => 'rails')
    end
  end
end