2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-06 07:48:46 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe Gitlab::RequestContext do
|
2017-02-06 07:48:46 -05:00
|
|
|
describe '#client_ip' do
|
2017-07-25 13:09:00 -04:00
|
|
|
subject { described_class.client_ip }
|
2019-10-18 07:11:44 -04:00
|
|
|
|
2017-02-16 05:21:30 -05:00
|
|
|
let(:app) { -> (env) {} }
|
2017-02-06 07:48:46 -05:00
|
|
|
let(:env) { Hash.new }
|
|
|
|
|
2019-03-12 14:22:29 -04:00
|
|
|
context 'with X-Forwarded-For headers', :request_store 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
|
|
|
|
client_ip = nil
|
|
|
|
|
|
|
|
endpoint = proc do
|
|
|
|
client_ip = Gitlab::SafeRequestStore[:client_ip]
|
|
|
|
[200, {}, ["Hello"]]
|
|
|
|
end
|
|
|
|
|
2019-03-14 07:51:09 -04:00
|
|
|
described_class.new(endpoint).call(env)
|
2019-03-12 14:22:29 -04:00
|
|
|
|
|
|
|
expect(client_ip).to eq(load_balancer_ip)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-06 07:48:46 -05:00
|
|
|
context 'when RequestStore::Middleware is used' do
|
2017-08-10 18:31:42 -04:00
|
|
|
around do |example|
|
2017-02-16 05:21:30 -05:00
|
|
|
RequestStore::Middleware.new(-> (env) { example.run }).call({})
|
2017-02-06 07:48:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'request' do
|
|
|
|
let(:ip) { '192.168.1.11' }
|
|
|
|
|
|
|
|
before do
|
2019-03-12 14:22:29 -04:00
|
|
|
allow_any_instance_of(Rack::Request).to receive(:ip).and_return(ip)
|
2017-07-25 13:09:00 -04:00
|
|
|
described_class.new(app).call(env)
|
2017-02-06 07:48:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(ip) }
|
|
|
|
end
|
|
|
|
|
2017-03-03 12:10:22 -05:00
|
|
|
context 'before RequestContext middleware run' do
|
2017-02-06 07:48:46 -05:00
|
|
|
it { is_expected.to be_nil }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|