事情的起因:前段时间闲赋在家(这段时间也是……),想练练手。觉得JavaEye的子域名挺有意思,就想仿照做个博客,把子域名用进去。子域名插件用SubdomainFu。结果代码写好了,测试时发现问题了。
我要写一个before_filter方法,在进入控制器之前校验一下子域名。方法名叫check_subdomain,因为比较通用,放在application.rb中。
先看控制器application的代码
# RAILS_ROOT/app/controllers/application.rb
class ApplicationController < ActionController::Base
# 为了简洁,其他代码都删了,只留了关键部分
# 这个设置可以使session跨域,但只能是 anysubdomain.myblog.com
session :session_domain => "myblog.com"
include AuthenticatedSystem
before_filter :login_required
protected
# 对用户的后台管理页面,检查子域名必须和用户login相同,否则跳转到404页面
def check_subdomain
unless current_user && current_user.login == current_subdomain
render_404
return false
end
end
# 简陋的处理,返回404页面
def render_404
render :file => "#{RAILS_ROOT}/public/404.html", :status => 404
end
end
上面的check_subdomain方法要在个人的博客管理控制台相关的控制器中,作为before_filter使用。比如,在categories控制器中,它处理“分类管理”的功能。
# RAILS_ROOT/app/controllers/categories_controller.rb
class CategoriesController < ApplicationController
# 这里做一个校验
before_filter :check_subdomain
# 相信有博客的朋友都会熟悉这个url吧,这是“分类管理”
# GET /admin/categories
def index
@categories = current_user.categories.ordered
end
end
这个逻辑很简单,在进入“分类管理” 页面之前,做个校验,判断子域名和当前用户的login相同。校验通过后,根据当前用户,找到所有分类,然后显示页面。
好了,现在我想测试CategoriesController的index方法,这需要使用功能测试(Functional Test),代码如下:
# RAILS_ROOT/test/functional/categories_controller_test.rb
class CategoriesControllerTest < ActionController::TestCase
fixtures :users, :categories
def setup
# 假设当前用户为alex
@alex = users(:alex)
end
test "index" do
# 这里设置下session,以便通过登录校验
get :index, {}, :user_id => @alex.id
assert_response :success
assert_template "index"
end
end
本该显示index页面,但是因为check_subdomain,跳转到404页面去了
ruby -I test test/functional/categories_controller_test.rb -n test_index ... Expected response to be a <:success>, but was <404>
错误原因在于使用get方法发起提交时,默认是没有子域名的,而且get方法中也没有办法设置url。找了很多地方,最后在一个国外帖子里发现了解决办法。就是用 request.host 来设置域名。
改进的测试代码如下:
# RAILS_ROOT/test/functional/categories_controller_test.rb
class CategoriesControllerTest < ActionController::TestCase
def setup
@alex = users(:alex)
# 利用Request对象的host属性来设定子域名,奇怪的是Rails API中没说明host是个属性,也没找到host=这个方法……
#
# 敏捷开发3rd居然没有说可以用@request,@controller之类的实例变量
# 如果哪位知道这方面的介绍,还请告之,谢谢。
@request.host = "#{@alex.login}.myblog.com"
end
# 测试是否可以正确获取子域名
test "subdomain" do
get :index
assert_equal @alex.login, @controller.send(:current_subdomain)
end
test "index" do
get :index, {}, :user_id => @alex.id
assert_response :success
assert_template "index"
end
end
改进后测试通过,一切正常。
另外,子域名的问题在集成测试(Integration Test)中也有,但集成测试中设置子域名是一件很简单的事情,代码如下:
class AddBlogTest < ActionController::IntegrationTest
fixtures :all
# 测试子域名
test "subdomain" do
# 第一种方法,指定host,host!方法是集成测试专有的方法,功能测试则没有提供
host! "alex.myblog.com"
get "/admin/blogs"
# 第二种方法,在url中加入子域名
# get "http://alex.myblog.com/admin/blogs"
assert_equal "alex", controller.send(:current_subdomain)
end
end
顺便补上Rspec版的controller测试代码,个人觉得Rspec换汤不换药,但写法倒是很舒服:
# RAILS_ROOT/spec/controllers/categories_controller_spec.rb
require 'spec_helper'
describe CategoriesController do
fixtures :users
before :each do
@alex = users(:alex)
# 和Rails的功能测试一样,只是Rspec中用request而非@request
request.host = "#{@alex.login}.myblog.com"
end
it "should tell you the subdomain" do
controller.send(:current_subdomain).should == @alex.login
end
end
参考资料