我对Rails 3.1应用程序进行了RSPEC集成测试,该应用程序需要通过发出带有JSON参数的POST请求和需要使用http_basic身份验证的移动头来测试移动客户端的API,因为请求对象在集成测试中不可用我有点卡住了
这是我到目前为止的代码
it "successfully posts scores" do
# request.env["HTTP_ACCEPT"] = "application/json" #This causes an error as request is nly available in controller tests
post "scores", :score => {:mobile_user_id => @mobile_user.id, :points => 50, :time_taken => 7275}.to_json,
:format => :json, :user_agent => 'Mobile', 'HTTP_AUTHORIZATION' => get_basic_auth
end
Run Code Online (Sandbox Code Playgroud)
发布请求无法识别我使用的是http基本身份验证,但不确定json的格式是否正确.任何帮助赞赏
get_basic_auth是一个帮助me4thod,看起来像这样
def get_basic_auth
user = 'user'
pw = 'secret'
ActionController::HttpAuthentication::Basic.encode_credentials user, pw
end
Run Code Online (Sandbox Code Playgroud)
我在我的控制器中使用了before_filter来检查看起来像这样的移动和http_basic_authentication
def authorize
logger.debug("@@@@ Authorizing request #{request.inspect}")
if mobile_device?
authenticate_or_request_with_http_basic do |username, password|
username == Mobile::Application.config.mobile_login_name && Mobile::Application.config.mobile_password
end …Run Code Online (Sandbox Code Playgroud)