Tramadol News - cheap online order Xanax how does xanax work soma online prescription i want a soma prescription order phentermine cheap online phentermine pay with check helps with tramadol withdrawal buy tramadol cheap online viagra web site cheapest viagra homepage cialis canada health canada buy cialis pharmacy pills ambien online consultation buy ambien on line without a rx script Valium no prescrption cheap Valium cod free fedex fioricet cheap no prescription fioricet purchased online without prescription order meridia no rx buying online Meridia xanax from mexico without rx buy xanax online cod soma online cheap free shipping soma overnight without rx phentermine 37.5 90ct buy phentermine with discount buy tramadol to treat your pain tramadol 50 mg cheapest generic price viagra levitra buy levitra online viagra difference between cialis and tadalafil purchase cialis overnight delivery now ambien fedex without prescription ambien buy on line buy valium online discount cheap cheap valium for sale buy prescriptions online fioricet fioricet ONLINE FEDEX COD FREE CONSULT buy meridia cheap cod no rx meridia 15 mg overnight delivery

灰色的灵魂

黑夜给了我黑色的眼睛, 而我注定要用它来寻找美金

灰色的灵魂 header image 2

functional tests中的form_authenticity_token

February 9th, 2009 · No Comments · Life

又是一个前一阵遇到的很tricky的问题。

Rails2.0中加入了form_authenticity_token来防止部分的cross-site的攻击,ActionView中默认的form_for标签会自动加入类似于

的代码,如果你自己使用form_tag来创建form的话,可以用类似于

<input type="hidden" name="authenticity_token" value="<%= "#{form_authenticity_token}" %>" />

的代码来加入这个隐藏的form中的authenticity_token。

但是手工加入这个token在functional tests中会带来问题,因为functional tests中是把allow_forgery_protection关掉的
可以在environments/test.rb中看到这样的配置

config.action_controller.allow_forgery_protection    = false

但是authenticity现在是hardcode在代码中,所以跑functional tests通常会遇到这样的错误

ActionView::TemplateError: No :secret given to the #protect_from_forgery call.  Set that or use a session store capable of generating its own keys (Cookie Session Store).

一种解决办法是在view中加入当前all_forgery_protection状态的判断代码,比如

<% if  protect_against_forgery? %>
<input type="hidden" name="authenticity_token" value="<%= "#{form_authenticity_token}" %>" />
<% end %>

但是有位同学用了个更巧妙的hook的办法,在test_helper.rb中hook掉form_authenticity_token,如下

module ActionController
  module RequestForgeryProtection
    def form_authenticity_token
      return "form_authenticity_token OVERRIDE!"
    end
  end
end

我比较喜欢后一种啦。

Tags: ··

No Comments so far ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment