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

request_context_spec.rb « middleware « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a0a6609c8bb8a39dee00354c6d463cf5046ddf1f (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
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true
require 'spec_helper'
require 'rack'
require 'request_store'
require 'gitlab/rspec/next_instance_of'

RSpec.describe Gitlab::Middleware::RequestContext, feature_category: :application_instrumentation do
  include NextInstanceOf

  let(:app) { -> (env) {} }
  let(:env) { {} }

  around do |example|
    RequestStore.begin!
    example.run
    RequestStore.end!
    RequestStore.clear!
  end

  describe '#call' do
    let(:instance) { Gitlab::RequestContext.instance }

    subject { described_class.new(app).call(env) }

    context 'setting the client ip' do
      context 'with X-Forwarded-For headers' do
        let(:load_balancer_ip) { '1.2.3.4' }
        let(:headers) do
          {
            'HTTP_X_FORWARDED_FOR' => "#{load_balancer_ip}, 127.0.0.1",
            'REMOTE_ADDR' => '127.0.0.1'
          }
        end

        let(:env) { Rack::MockRequest.env_for("/").merge(headers) }

        it 'returns the load balancer IP' do
          expect { subject }.to change { instance.client_ip }.from(nil).to(load_balancer_ip)
        end
      end

      context 'request' do
        let(:ip) { '192.168.1.11' }

        before do
          allow_next_instance_of(Rack::Request) do |request|
            allow(request).to receive(:ip).and_return(ip)
          end
        end

        it 'sets the `client_ip`' do
          expect { subject }.to change { instance.client_ip }.from(nil).to(ip)
        end

        it 'sets the `request_start_time`' do
          expect { subject }.to change { instance.request_start_time }.from(nil).to(Float)
        end

        it 'sets the `spam_params`' do
          expect { subject }.to change { instance.spam_params }.from(nil).to(::Spam::SpamParams)
        end
      end
    end
  end
end