Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test only a part of the URL I am redirected to (using assert_redirected_to)?

Tags:

In a functional test of my Rails app, I want to test where I am redirected to. The expected URL points to an external resource (that means it's not part of my application).

The URL looks like this: https://my.url.com/foo?bar1=xyz&bar2=123

Unfortunately I can't predict the parameters, because they are generated by an external resource.*
However, the rest of the URL always stays the same: https://my.url.com/foo

I usually use assert_redirected_to for this kind of test, but this expects the whole URL, including the parameters.

Can anyone think of another way to test that redirection but only check for the first part of the URL without the parameters?

(the URL is not in the assigns Hash)

*(I make an API call to an application, which responses with the URL I shall redirect_to)

like image 606
Daniel Pietzsch Avatar asked Jan 08 '10 00:01

Daniel Pietzsch


1 Answers

The http request commands like post, get etc. create an instance variable called @response when they are called*. @response itself contains a method called redirect_url which stores the URL you have been redirected to (in case you have really been redirected).

Therefor, I can just use a normal assert_match to compare a regular expression to @response.redirect_url:

post :my_action_to_test assert_response :redirect assert_match /https:\/\/my.url.com\/foo/, @response.redirect_url 

*(actually, these http methods just use the private method process, which creates the @response variable).

like image 170
Daniel Pietzsch Avatar answered Sep 17 '22 13:09

Daniel Pietzsch