Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cloudflare_helper_spec.rb « logging « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 87ac438d38dbd3e734e40bda70288f3798bced80 (plain)
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Logging::CloudflareHelper do
  let(:helper) do
    Class.new do
      include Gitlab::Logging::CloudflareHelper
    end.new
  end

  describe '#store_cloudflare_headers!' do
    let(:payload) { {} }
    let(:env) { {} }
    let(:request) { ActionDispatch::Request.new(env) }

    before do
      request.headers.merge!(headers)
    end

    context 'with normal headers' do
      let(:headers) { { 'Cf-Ray' => '592f0aa22b3dea38-IAD', 'Cf-Request-Id' => SecureRandom.hex } }

      it 'adds Cf-Ray-Id and Cf-Request-Id' do
        helper.store_cloudflare_headers!(payload, request)

        expect(payload[:cf_ray]).to eq(headers['Cf-Ray'])
        expect(payload[:cf_request_id]).to eq(headers['Cf-Request-Id'])
      end
    end

    context 'with header values with long strings' do
      let(:headers) { { 'Cf-Ray' => SecureRandom.hex(33), 'Cf-Request-Id' => SecureRandom.hex(33) } }

      it 'filters invalid header values' do
        helper.store_cloudflare_headers!(payload, request)

        expect(payload.keys).not_to include(:cf_ray, :cf_request_id)
      end
    end

    context 'with header values with non-alphanumeric characters' do
      let(:headers) { { 'Cf-Ray' => "Bad\u0000ray", 'Cf-Request-Id' => "Bad\u0000req" } }

      it 'filters invalid header values' do
        helper.store_cloudflare_headers!(payload, request)

        expect(payload.keys).not_to include(:cf_ray, :cf_request_id)
      end
    end
  end
end