How do I set HTTP_REFERER when testing in Rails?

后端 未结 7 1298
我在风中等你
我在风中等你 2020-12-05 22:12

I\'m trying to test a controller and I got this error. I understand the error, but don\'t know how to fix it.

test: on CREATE to :user with completely invali         


        
7条回答
  •  爱一瞬间的悲伤
    2020-12-05 22:59

    The accepted answer doesn't work for integration tests because the @request variable doesn't exist.

    According to RailsGuides, you can pass headers to the helpers.

    Rails <= 4:

    test "blah" do
      get root_path, {}, {'HTTP_REFERER' => 'http://foo.com'}
      ...
    end
    

    Rails >= 5:

    test "blah" do
      get root_path, params: { id: 12 }, headers: { "HTTP_REFERER" => "http://foo.com" }
      ...
    end
    

提交回复
热议问题