How do you set a signed cookie in a Rails integration test?

455 views Asked by At

How do you set a signed cookie within a Rails integration test?

For example, assuming I load the :user_id from the cookie, this is the way I would expect to test it, but no joy:

  test "success when cookie properly set" do
    cookies.signed[:user_id] = @user.id
    get user_url(@user)
    assert_response :success
  end

I understand that its testing the implementation, but in some cases its necessary.

Update: Works But Ugly

I have a work around, but its pretty ugly. This passes the cookie as a header to the get request and requires that you manually sign the and format the cookie:

  test "success when cookie properly set" do
    signed_user_id="eyJfcmFpb..."
    get user_url(@user), headers: { "HTTP_COOKIE" => "user_id=#{ signed_user_id }" }
    assert_response :success
  end
0

There are 0 answers