Randomness for you since 1976
While building a Rest API call for our product, we needed the server to respond to a post with a “303 see other” response. Unfortunately, in Rails when you do a redirect_to, it always uses the 302 response code, even when you set the status explicitly:
These don’t fail, but they dont do what you would expect. In my test I asserted that the response code was 303, and the test failed. After some digging I found that I could use the ‘head’ method to force rails to respond correctly:
My response assertion passed. Yay. But it was short lived, because this assertion failed after I made the change:
with an error deep inside response_assertions.rb. Inside the ‘assert_redirected_to’ method, the following line failed because @response.redirected_to returns nil, and then explodes when it tries to call dup.
I did some digging, and it turns out that when I used ‘head’ instead of ‘redirect_to’, the @response.redirected_to was never set. I fixed the test by asserting string equality directly in the response headers:
This blog is a dumping grounds for my experiences as a web developer, a parent, an artist, a writer and a human being. Maybe someday there will be something here that is worth your time, some sort of useful information or words that piece your very soul. Probably not though.
Leave a reply