I'm not sure how well this applies in Cucumber (using Shoulda here), but after trying a few recommendations elsewhere, this appears to work reliably:
def in_subdomain(str)
# test.host == default testing domain, feel free to change to match your usage
@request.host = "#{str}.test.host"
end
And then prior to calling get
, you just need to make sure you're in_subdomain('subdomain-fuuuuuu')
. That properly sets the URL and current_subdomain
at least (I haven't checked everything), redirects w/o specifying subdomain stay in the subdomain, and any redirects to other subdomains (or :subdomain => false
) still set the correct redirected_to value.
These (high-quality, I'm sure you can tell) tests pass, for instance, and they have a check on current_subdomain in the controller:
should "show on the owner's subdomain" do
in_subdomain(@user.domain)
get :show, :id => @user.things.first.id
assert_response :success
end
should "not show on another users' subdomain" do
in_subdomain(@random_user.domain)
get :show, :id => @user.things.first.id
assert_redirected_to user_url(@random_user, :subdomain => @random_user.domain)
end
should "not show on a non-existent subdomain" do
in_subdomain("cthulhu-fhtagn")
get :show, :id => @user.things.first.id
assert_redirected_to root_url(:subdomain => false)
end