File: headers_processor_spec.rb

package info (click to toggle)
ruby-httparty 0.21.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 908 kB
  • sloc: ruby: 6,782; xml: 425; sh: 35; makefile: 14
file content (54 lines) | stat: -rw-r--r-- 1,556 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
require 'spec_helper'

RSpec.describe HTTParty::HeadersProcessor do
  subject(:headers) { options[:headers] }
  let(:options) { { headers: {} } }
  let(:global_headers) { {} }

  before { described_class.new(global_headers, options).call }

  context 'when headers are not set at all' do
    it 'returns empty hash' do
      expect(headers).to eq({})
    end
  end

  context 'when only global headers are set' do
    let(:global_headers) { { accept: 'text/html' } }

    it 'returns stringified global headers' do
      expect(headers).to eq('accept' => 'text/html')
    end
  end

  context 'when only request specific headers are set' do
    let(:options) { { headers: {accept: 'text/html' } } }

    it 'returns stringified request specific headers' do
      expect(headers).to eq('accept' => 'text/html')
    end
  end

  context 'when global and request specific headers are set' do
    let(:global_headers) { { 'x-version' => '123' } }

    let(:options) { { headers: { accept: 'text/html' } } }

    it 'returns merged global and request specific headers' do
      expect(headers).to eq('accept' => 'text/html', 'x-version' => '123')
    end
  end

  context 'when headers are dynamic' do
    let(:global_headers) { {'x-version' => -> { 'abc'.reverse } } }

    let(:options) do
      { body: '123',
        headers: { sum: lambda { |options| options[:body].chars.map(&:to_i).inject(:+) } } }
    end

    it 'returns processed global and request specific headers' do
      expect(headers).to eq('sum' => 6, 'x-version' => 'cba')
    end
  end
end