2020-03-13 15:44:24 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
require 'fast_spec_helper'
|
|
|
|
require 'rack'
|
|
|
|
require 'request_store'
|
|
|
|
require_relative '../../../support/helpers/next_instance_of'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::Middleware::RequestContext do
|
2020-03-13 15:44:24 +05:30
|
|
|
include NextInstanceOf
|
|
|
|
|
|
|
|
let(:app) { -> (env) {} }
|
|
|
|
let(:env) { {} }
|
|
|
|
|
|
|
|
around do |example|
|
|
|
|
RequestStore.begin!
|
|
|
|
example.run
|
|
|
|
RequestStore.end!
|
|
|
|
RequestStore.clear!
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#call' do
|
2021-03-11 19:13:27 +05:30
|
|
|
let(:instance) { Gitlab::RequestContext.instance }
|
|
|
|
|
|
|
|
subject { described_class.new(app).call(env) }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
context 'setting the client ip' do
|
2020-03-13 15:44:24 +05:30
|
|
|
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
|
2021-03-11 19:13:27 +05:30
|
|
|
expect { subject }.to change { instance.client_ip }.from(nil).to(load_balancer_ip)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'request' do
|
|
|
|
let(:ip) { '192.168.1.11' }
|
|
|
|
|
|
|
|
before do
|
2021-03-11 19:13:27 +05:30
|
|
|
allow_next_instance_of(Rack::Request) do |request|
|
|
|
|
allow(request).to receive(:ip).and_return(ip)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'sets the `client_ip`' do
|
|
|
|
expect { subject }.to change { instance.client_ip }.from(nil).to(ip)
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'sets the `request_start_time`' do
|
|
|
|
expect { subject }.to change { instance.request_start_time }.from(nil).to(Float)
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|