如何在rspec rails请求测试中发布缺少authenticity_token的POST?

Jam*_* EJ 12 rspec ruby-on-rails csrf authenticity-token

我正在模拟来自外部服务的请求,该服务不具有真实性令牌.如果skip_before_action :verify_authenticity_token丢失,我希望测试失败.

我如何从Rspec请求规范中执行此操作?

目前我正在使用post如下,但它很高兴被接受.

post endpoint, my_json_string, {'CONTENT_TYPE' => "application/json"}
Run Code Online (Sandbox Code Playgroud)

Max*_*kov 35

在测试环境中禁用 CSRF保护.尝试启用它:

before do
  ActionController::Base.allow_forgery_protection = true
end

after do
  ActionController::Base.allow_forgery_protection = false
end
Run Code Online (Sandbox Code Playgroud)