Ruby on rails 设置http状态代码
201是创建资源时应设置的状态。虽然上述方法适用于创建操作,但其操作的规范不再适用:Ruby on rails 设置http状态代码,ruby-on-rails,rspec,Ruby On Rails,Rspec,201是创建资源时应设置的状态。虽然上述方法适用于创建操作,但其操作的规范不再适用: redirect_to browse_path(asset.parent_id), notice: "successfully created file!", status: 201 状态预期通过,但重定向到预期失败: subject { response } describe '.create' do context 'when orphan' do before do
redirect_to browse_path(asset.parent_id), notice: "successfully created file!", status: 201
状态预期通过,但重定向到预期失败:
subject { response }
describe '.create' do
context 'when orphan' do
before do
post :create, asset: { parent_id: nil, uploaded_file: file }
end
it { should have_http_status 201 }
it { should redirect_to '/' }
end
end
那么我应该设置状态码吗?我承认,我实际上不知道浏览器是如何使用它们的,我的应用程序也是如何工作的,如果我在我的操作中刻意设置它们,或者不设置它们(只使用302个重定向和200个成功)
如果状态代码很重要,我应该如何通过上述规范?您可以在rspec中声明off response.body或其他响应属性。在这种情况下,您所追求的是
response.header[“Location”]
您可以选择避开capybara/rspec的问题,您可以断言当前的url,但仍然断言状态代码
重定向到只是一个愚蠢的中级助手,你需要到达一个稍微低一点的级别作为回应。使用水豚进行某种或更高级别的操作才能到达你想要的位置。一种方法是:
redirect_to browse_path(asset.parent_id), notice: "successfully created file!", status: 302
从:
状态代码可以是标准HTTP状态代码,也可以是
整数,或表示下框、带下划线和
符号化描述。请注意,状态代码必须是3xxHTTP
代码,否则将不会发生重定向
(增加重点)
简单地说,用201状态代码重定向是错误的。虽然我可以这样做,
its(:status){should eq 201}
,但我不能its(:path){should eq'/'}
。路径不是响应对象的方法。我试过检查,但找不到我想要的。。。有什么想法吗?响应。标题[“位置”]是重定向的方向。我得到的参数数量错误(2对1)?
redirect_to browse_path(asset.parent_id), notice: "successfully created file!", status: 302
its(:status){ should eq 201 }
its(:location){ should eq 'http://test.host/' }