I’d like to block off access to the application to all non-local requesters (my application’s actual functionality in practice is more sophisticated, but figuring out how to do this will solve my specific issue). How would I go about testing this with request tests in RSpec?
In spec/requests/gatekeeper_spec.rb
describe "A local request to the site root" do
before :each do
get root_path
end
it "should not allow access" do
response.status.should be(401)
end
end
describe "An external (terminology?) request to the site root" do
before :all do
# TODO: make request remote
end
before :each do
get root_path
end
it "should allow access" do
response.status.should be(200)
end
end
How should I implement the # TODO line? I’ve looked into mocks and think that rigging request.remote_ip may be appropriate, but I’m not certain exactly how such a mock is implemented.
If I understand correctly, test requests have a remote address of “0.0.0.0”, so they would normally be considered remote and you’d want to stub the local requests, not the other way around.
I think this should work for controller specs — not sure about request specs: