Rspec Rails - 在请求规范中模拟远程请求
Rspec Rails - Mocking remote requests in requests spec
我的问题是在 Rspec 请求规范中模拟 IP。我想模拟来自远程(非本地主机)请求的请求。这是为了测试一些路由约束。
到目前为止,这是我的规格:
require 'rails_helper'
RSpec.describe 'AdminWhitelistBlocked', type: :request do
before :each do
RSpec.configure do |config|
config.before(:each, allow_rescue: true) do
Rails.application.config.action_dispatch.stub(:show_exceptions) { true }
Rails.application.config.stub(:consider_all_requests_local) { false }
end
end
end
it 'Allow local access access to the active admin area' do
get '/admin/login'
expect(request.remote_ip).to eq('0.0.0.0')
expect(request.headers['REMOTE_ADDR']).to eq('0.0.0.0')
expect(response).to have_http_status(:not_found)
end
end
我希望远程 IP 不是本地主机。
Failures:
1) AdminWhitelistBlocked Allow local access access to the active admin area
Failure/Error: expect(request.remote_ip).to eq('0.0.0.0')
expected: "0.0.0.0"
got: "127.0.0.1"
(compared using ==)
更新:
我也试过预先设置请求的远程地址:
require 'rails_helper'
RSpec.describe 'AdminWhitelistBlocked', type: :request do
before :each do
RSpec.configure do |config|
config.before(:each, allow_rescue: true) do
@request.remote_addr = '0.0.0.0'
end
end
end
it 'Allow local access access to the active admin area' do
get '/admin/login'
expect(request.remote_addr).to eq('0.0.0.0')
expect(request.headers['REMOTE_ADDR']).to eq('0.0.0.0')
expect(response).to have_http_status(:not_found)
end
end
然而,还是没有成功:
Failures:
1) AdminWhitelistBlocked Allow local access access to the active admin area
Failure/Error: expect(request.remote_addr).to eq('0.0.0.0')
expected: "0.0.0.0"
got: "127.0.0.1"
(compared using ==)
require 'rails_helper'
RSpec.describe 'AdminWhitelistAccess', type: :request do
it 'Allow local access access to the active admin area' do
get '/admin/login'
expect(request.remote_addr).to eq('127.0.0.1')
expect(request.headers['REMOTE_ADDR']).to eq('127.0.0.1')
expect(response).to have_http_status(:ok)
end
end
RSpec.describe 'AdminWhitelistBlocked', type: :request do
before :each do
allow_any_instance_of(ActionDispatch::Request).to receive(:remote_addr).and_return('0.0.0.0')
end
it 'Allow local access access to the active admin area' do
expect { get '/admin/login' }.to raise_error(ActionController::RoutingError)
end
end
我的问题是在 Rspec 请求规范中模拟 IP。我想模拟来自远程(非本地主机)请求的请求。这是为了测试一些路由约束。
到目前为止,这是我的规格:
require 'rails_helper'
RSpec.describe 'AdminWhitelistBlocked', type: :request do
before :each do
RSpec.configure do |config|
config.before(:each, allow_rescue: true) do
Rails.application.config.action_dispatch.stub(:show_exceptions) { true }
Rails.application.config.stub(:consider_all_requests_local) { false }
end
end
end
it 'Allow local access access to the active admin area' do
get '/admin/login'
expect(request.remote_ip).to eq('0.0.0.0')
expect(request.headers['REMOTE_ADDR']).to eq('0.0.0.0')
expect(response).to have_http_status(:not_found)
end
end
我希望远程 IP 不是本地主机。
Failures:
1) AdminWhitelistBlocked Allow local access access to the active admin area
Failure/Error: expect(request.remote_ip).to eq('0.0.0.0')
expected: "0.0.0.0"
got: "127.0.0.1"
(compared using ==)
更新:
我也试过预先设置请求的远程地址:
require 'rails_helper'
RSpec.describe 'AdminWhitelistBlocked', type: :request do
before :each do
RSpec.configure do |config|
config.before(:each, allow_rescue: true) do
@request.remote_addr = '0.0.0.0'
end
end
end
it 'Allow local access access to the active admin area' do
get '/admin/login'
expect(request.remote_addr).to eq('0.0.0.0')
expect(request.headers['REMOTE_ADDR']).to eq('0.0.0.0')
expect(response).to have_http_status(:not_found)
end
end
然而,还是没有成功:
Failures:
1) AdminWhitelistBlocked Allow local access access to the active admin area
Failure/Error: expect(request.remote_addr).to eq('0.0.0.0')
expected: "0.0.0.0"
got: "127.0.0.1"
(compared using ==)
require 'rails_helper'
RSpec.describe 'AdminWhitelistAccess', type: :request do
it 'Allow local access access to the active admin area' do
get '/admin/login'
expect(request.remote_addr).to eq('127.0.0.1')
expect(request.headers['REMOTE_ADDR']).to eq('127.0.0.1')
expect(response).to have_http_status(:ok)
end
end
RSpec.describe 'AdminWhitelistBlocked', type: :request do
before :each do
allow_any_instance_of(ActionDispatch::Request).to receive(:remote_addr).and_return('0.0.0.0')
end
it 'Allow local access access to the active admin area' do
expect { get '/admin/login' }.to raise_error(ActionController::RoutingError)
end
end