File: response_spec.rb

package info (click to toggle)
ruby-oauth2 2.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 644 kB
  • sloc: ruby: 3,763; makefile: 4; sh: 4
file content (345 lines) | stat: -rw-r--r-- 11,603 bytes parent folder | download
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# frozen_string_literal: true

RSpec.describe OAuth2::Response do
  subject { described_class.new(raw_response) }

  let(:raw_response) { Faraday::Response.new(status: status, response_headers: headers, body: body) }
  let(:status) { 200 }
  let(:headers) { {'foo' => 'bar'} }
  let(:body) { 'foo' }

  describe '#initialize' do
    it 'returns the status, headers and body' do
      expect(subject.headers).to eq(headers)
      expect(subject.status).to eq(status)
      expect(subject.body).to eq(body)
    end
  end

  describe '.register_parser' do
    let(:response) do
      double('response', headers: {'Content-Type' => 'application/foo-bar'},
                         status: 200,
                         body: 'baz')
    end

    before do
      described_class.register_parser(:foobar, ['application/foo-bar']) do |body|
        "foobar #{body}"
      end
    end

    it 'adds to the content types and parsers' do
      expect(described_class.send(:class_variable_get, :@@parsers).keys).to include(:foobar)
      expect(described_class.send(:class_variable_get, :@@content_types).keys).to include('application/foo-bar')
    end

    it 'is able to parse that content type automatically' do
      expect(described_class.new(response).parsed).to eq('foobar baz')
    end
  end

  describe '#content_type' do
    context 'when headers are blank' do
      let(:headers) { nil }

      it 'returns nil' do
        expect(subject.content_type).to be_nil
      end
    end

    context 'when content-type is not present' do
      let(:headers) { {'a fuzzy' => 'fuzzer'} }

      it 'returns empty string' do
        expect(subject.content_type).to eq('')
      end
    end

    context 'when content-type is present' do
      let(:headers) { {'Content-Type' => 'application/x-www-form-urlencoded'} }

      it 'returns the content type header contents' do
        expect(subject.content_type).to eq('application/x-www-form-urlencoded')
      end
    end
  end

  describe '#parsed' do
    subject(:parsed) do
      headers = {'Content-Type' => content_type}
      response = double('response', headers: headers, body: body)
      instance = described_class.new(response)
      instance.parsed
    end

    shared_examples_for 'parsing JSON-like' do
      it 'has num keys' do
        expect(parsed.keys.size).to eq(6)
      end

      it 'parses string' do
        expect(parsed['foo']).to eq('bar')
        expect(parsed.key('bar')).to eq('foo')
      end

      it 'parses non-zero number' do
        expect(parsed['answer']).to eq(42)
        expect(parsed.key(42)).to eq('answer')
      end

      it 'parses nil as NilClass' do
        expect(parsed['krill']).to be_nil
        expect(parsed.key(nil)).to eq('krill')
      end

      it 'parses zero as number' do
        expect(parsed['zero']).to eq(0)
        expect(parsed.key(0)).to eq('zero')
      end

      it 'parses false as FalseClass' do
        expect(parsed['malign']).to be(false)
        expect(parsed.key(false)).to eq('malign')
      end

      it 'parses false as TrueClass' do
        expect(parsed['shine']).to be(true)
        expect(parsed.key(true)).to eq('shine')
      end
    end

    context 'when application/json' do
      let(:content_type) { 'application/json' }
      let(:body) { JSON.dump(foo: 'bar', answer: 42, krill: nil, zero: 0, malign: false, shine: true) }

      it_behaves_like 'parsing JSON-like'
    end

    context 'when application/Json' do
      let(:content_type) { 'application/Json' }
      let(:body) { JSON.dump(foo: 'bar', answer: 42, krill: nil, zero: 0, malign: false, shine: true) }

      it_behaves_like 'parsing JSON-like'
    end

    context 'when application/hal+json' do
      let(:content_type) { 'application/hal+json' }
      let(:body) { JSON.dump(foo: 'bar', answer: 42, krill: nil, zero: 0, malign: false, shine: true) }

      it_behaves_like 'parsing JSON-like'
    end

    context 'when application/x-www-form-urlencoded' do
      let(:content_type) { 'application/x-www-form-urlencoded' }
      let(:body) { 'foo=bar&answer=42&krill=&zero=0&malign=false&shine=true' }

      it 'has num keys' do
        expect(parsed.keys.size).to eq(6)
      end

      it 'parses string' do
        expect(parsed['foo']).to eq('bar')
        expect(parsed.key('bar')).to eq('foo')
      end

      it 'parses non-zero number as string' do
        expect(parsed['answer']).to eq('42')
        expect(parsed.key('42')).to eq('answer')
      end

      it 'parses nil as empty string' do
        expect(parsed['krill']).to eq('')
        expect(parsed.key('')).to eq('krill')
      end

      it 'parses zero as string' do
        expect(parsed['zero']).to eq('0')
        expect(parsed.key('0')).to eq('zero')
      end

      it 'parses false as string' do
        expect(parsed['malign']).to eq('false')
        expect(parsed.key('false')).to eq('malign')
      end

      it 'parses true as string' do
        expect(parsed['shine']).to eq('true')
        expect(parsed.key('true')).to eq('shine')
      end
    end

    it 'parses application/vnd.collection+json body' do
      headers = {'Content-Type' => 'application/vnd.collection+json'}
      body = JSON.dump(collection: {})
      response = double('response', headers: headers, body: body)
      subject = described_class.new(response)
      expect(subject.parsed.keys.size).to eq(1)
    end

    it 'parses application/vnd.api+json body' do
      headers = {'Content-Type' => 'application/vnd.api+json'}
      body = JSON.dump(collection: {})
      response = double('response', headers: headers, body: body)
      subject = described_class.new(response)
      expect(subject.parsed.keys.size).to eq(1)
    end

    it 'parses application/problem+json body' do
      headers = {'Content-Type' => 'application/problem+json'}
      body = JSON.dump(type: 'https://tools.ietf.org/html/rfc7231#section-6.5.4', title: 'Not Found')
      response = double('response', headers: headers, body: body)
      subject = described_class.new(response)
      expect(subject.parsed.keys.size).to eq(2)
      expect(subject.parsed['type']).to eq('https://tools.ietf.org/html/rfc7231#section-6.5.4')
      expect(subject.parsed['title']).to eq('Not Found')
    end

    it "doesn't try to parse other content-types" do
      headers = {'Content-Type' => 'text/html'}
      body = '<!DOCTYPE html><html><head></head><body></body></html>'

      response = double('response', headers: headers, body: body)

      expect(JSON).not_to receive(:parse)
      expect(Rack::Utils).not_to receive(:parse_query)

      subject = described_class.new(response)
      expect(subject.parsed).to be_nil
    end

    it "doesn't parse bodies which have previously been parsed" do
      headers = {'Content-Type' => 'application/json'}
      body = {foo: 'bar', answer: 42, krill: nil, zero: 0, malign: false, shine: true}

      response = double('response', headers: headers, body: body)

      expect(JSON).not_to receive(:parse)
      expect(Rack::Utils).not_to receive(:parse_query)

      subject = described_class.new(response)
      expect(subject.parsed.keys.size).to eq(6)
    end

    it 'snakecases json keys when parsing' do
      headers = {'Content-Type' => 'application/json'}
      body = JSON.dump('accessToken' => 'bar', 'MiGever' => 'Ani')
      response = double('response', headers: headers, body: body)
      subject = described_class.new(response)
      expect(subject.parsed.keys.size).to eq(2)
      expect(subject.parsed['access_token']).to eq('bar')
      expect(subject.parsed['mi_gever']).to eq('Ani')
    end

    context 'when not snaky' do
      it 'does not snakecase json keys when parsing' do
        headers = {'Content-Type' => 'application/json'}
        body = JSON.dump('accessToken' => 'bar', 'MiGever' => 'Ani')
        response = double('response', headers: headers, body: body)
        subject = described_class.new(response, snaky: false)
        expect(subject.parsed.keys.size).to eq(2)
        expect(subject.parsed['accessToken']).to eq('bar')
        expect(subject.parsed['MiGever']).to eq('Ani')
        expect(subject.parsed['access_token']).to be_nil
        expect(subject.parsed['mi_gever']).to be_nil
      end
    end

    it 'supports registered parsers with arity == 0; passing nothing' do
      described_class.register_parser(:arity_0, []) do
        'a-ok'
      end

      headers   = {'Content-Type' => 'text/html'}
      body      = '<!DOCTYPE html><html><head></head><body></body></html>'
      response  = double('response', headers: headers, body: body)

      subject = described_class.new(response, parse: :arity_0)

      expect(subject.parsed).to eq('a-ok')
    end

    it 'supports registered parsers with arity == 2; passing body and response' do
      headers   = {'Content-Type' => 'text/html'}
      body      = '<!DOCTYPE html><html><head></head><body></body></html>'
      response  = double('response', headers: headers, body: body)

      described_class.register_parser(:arity_2, []) do |passed_body, passed_response|
        expect(passed_body).to eq(body)
        expect(passed_response).to eq(response)

        'a-ok'
      end

      subject = described_class.new(response, parse: :arity_2)

      expect(subject.parsed).to eq('a-ok')
    end

    it 'supports registered parsers with arity > 2; passing body and response' do
      headers   = {'Content-Type' => 'text/html'}
      body      = '<!DOCTYPE html><html><head></head><body></body></html>'
      response  = double('response', headers: headers, body: body)

      described_class.register_parser(:arity_3, []) do |passed_body, passed_response, *args|
        expect(passed_body).to eq(body)
        expect(passed_response).to eq(response)
        expect(args).to eq([])

        'a-ok'
      end

      subject = described_class.new(response, parse: :arity_3)

      expect(subject.parsed).to eq('a-ok')
    end

    it 'supports directly passed parsers' do
      headers   = {'Content-Type' => 'text/html'}
      body      = '<!DOCTYPE html><html><head></head><body></body></html>'
      response  = double('response', headers: headers, body: body)

      subject = described_class.new(response, parse: -> { 'a-ok' })

      expect(subject.parsed).to eq('a-ok')
    end

    it 'supports no parsing' do
      headers   = {'Content-Type' => 'text/html'}
      body      = '<!DOCTYPE html><html><head></head><body></body></html>'
      response  = double('response', headers: headers, body: body)

      subject = described_class.new(response, parse: false)

      expect(subject.parsed).to eq(nil)
    end
  end

  context 'with xml parser registration' do
    it 'tries to load multi_xml.rb and use it' do
      expect(described_class.send(:class_variable_get, :@@parsers)[:xml]).not_to be_nil
    end

    it 'is able to parse xml' do
      headers = {'Content-Type' => 'text/xml'}
      body = '<?xml version="1.0" standalone="yes" ?><foo><bar>baz</bar></foo>'

      response = double('response', headers: headers, body: body)
      expect(described_class.new(response).parsed).to eq('foo' => {'bar' => 'baz'})
    end

    it 'is able to parse application/xml' do
      headers = {'Content-Type' => 'application/xml'}
      body = '<?xml version="1.0" standalone="yes" ?><foo><bar>baz</bar></foo>'

      response = double('response', headers: headers, body: body)
      expect(described_class.new(response).parsed).to eq('foo' => {'bar' => 'baz'})
    end
  end

  describe 'converting to json' do
    it 'does not blow up' do
      expect { subject.to_json }.not_to raise_error
    end
  end
end