2017-08-17 22:00:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
describe Gitlab::RequestContext do
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#client_ip' do
|
2017-09-10 17:25:29 +05:30
|
|
|
subject { described_class.client_ip }
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:app) { -> (env) {} }
|
|
|
|
let(:env) { Hash.new }
|
|
|
|
|
2019-05-03 19:53:19 +05:30
|
|
|
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
|
|
|
|
|
|
|
|
Rails.application.middleware.build(endpoint).call(env)
|
|
|
|
|
|
|
|
expect(client_ip).to eq(load_balancer_ip)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when RequestStore::Middleware is used' do
|
2018-03-17 18:26:18 +05:30
|
|
|
around do |example|
|
2017-08-17 22:00:37 +05:30
|
|
|
RequestStore::Middleware.new(-> (env) { example.run }).call({})
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'request' do
|
|
|
|
let(:ip) { '192.168.1.11' }
|
|
|
|
|
|
|
|
before do
|
2019-05-03 19:53:19 +05:30
|
|
|
allow_any_instance_of(Rack::Request).to receive(:ip).and_return(ip)
|
2017-09-10 17:25:29 +05:30
|
|
|
described_class.new(app).call(env)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(ip) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'before RequestContext middleware run' do
|
|
|
|
it { is_expected.to be_nil }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|