testing for redirection

29 views
Skip to first unread message

lsiden

unread,
Nov 22, 2009, 1:52:29 PM11/22/09
to webrat
I tried running the following step in cucmber:

When /^I visit "([^\"]*)" I should be redirected to "([^\"]*)"/ do |
arg1, arg2|
visit arg1
redirected_to.should == arg2
#response = request_page arg1, :get, {}
#response_location().should == arg2
#response.inspect
#response.headers["Location"].should == arg2
end

responded:

When I visit "http://gator1087.hostgator.com/~tes/application/" I
should be redirected to "https://gator1087.hostgator.com/~tes/
application/" # step_definitions/steps.rb:33
expected: "https://gator1087.hostgator.com/~tes/application/",
got: nil (using ==)
(Spec::Expectations::ExpectationNotMetError)

As you can see from the code, I've been struggling with this a bit.
How do I inspect the location (and also response code)?

Matt Wynne

unread,
Nov 22, 2009, 1:57:34 PM11/22/09
to web...@googlegroups.com
Webrat will follow redirects automatically, so it will implicitly make
a second request. That means you should end up on the right page:

visit arg1
response.should be_success
response.request.request_uri.should =~ /#{arg2}/

> >

cheers,
Matt

+447974 430184
ma...@mattwynne.net
http://mattwynne.net

lsiden

unread,
Nov 22, 2009, 2:20:09 PM11/22/09
to webrat
undefined method `request' for #<WWW::Mechanize::Page:
0xb74d20a8> (NoMethodError)
> m...@mattwynne.nethttp://mattwynne.net

Matt Wynne

unread,
Nov 22, 2009, 2:21:58 PM11/22/09
to web...@googlegroups.com

On 22 Nov 2009, at 19:20, lsiden wrote:

>
> undefined method `request' for #<WWW::Mechanize::Page:
> 0xb74d20a8> (NoMethodError)

Oh, I assumed you were in rails mode, since you didn't say. You'd
better read the source for the mechanize adapter in webrat.

lsiden

unread,
Nov 22, 2009, 2:32:11 PM11/22/09
to webrat
Sorry.

This is what I finally worked:

When /^I visit "([^\"]*)" I should be redirected to "([^\"]*)"/ do |
arg1, arg2|
visit arg1
response.uri.should contain(arg2)
end
> m...@mattwynne.nethttp://mattwynne.net

Rick DeNatale

unread,
Nov 22, 2009, 4:24:44 PM11/22/09
to web...@googlegroups.com
On Sun, Nov 22, 2009 at 2:32 PM, lsiden <lsi...@gmail.com> wrote:
>
> Sorry.
>
> This is what I finally worked:
>
> When /^I visit "([^\"]*)" I should be redirected to "([^\"]*)"/ do |
> arg1, arg2|
>  visit arg1
>  response.uri.should contain(arg2)
> end


It feels to me as though you might be working too hard, why not break
this down just a bit so you can just use the webrat steps provided for
you.

For example, I I wanted to specify that a user would be redirected to
the login page when he wasn't logged in, after the steps necessary to
set up a user who isn't logged in I might just say

When I visit the family jewels page
Then I should be on the login page

and all you need to do is put the code in features/support/paths.rb to
map "the family jewels page" and "the login page" to the right urls.

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

lsiden

unread,
Nov 22, 2009, 4:51:31 PM11/22/09
to webrat
I was thinking that each step is a new session, and therefore I would
lose the context between steps. But I figured it out. Now I've got

Scenario: Potential Customer visits Sales Lead form with HTTP
Given I visit "http://gator1087.hostgator.com/~tes/application/"
Then I should be redirected to "https://gator1087.hostgator.com/
~tes/application/"

and in steps.rb

Then /^I should be redirected to "([^\"]*)"/ do |arg1|
response.uri.should contain(arg1)
end

Is paths.rb only for Rails apps, or can I use it for Mechanize
sessions?

One of the problems I'm having as a Ruby/Cuke/Webrat newbie is finding
what objects are in my current scope that I can take advantage of.

tmorgan99

unread,
Nov 23, 2009, 3:00:28 PM11/23/09
to webrat
I think I may have seen something similar.
When I went through a redirect, and then expected to be able to read
the resulting flash message...
I think that rails inserted a host name 'example.com' and then lost
the flash in XSS?
I can see the code works, but the test fails.

Rick DeNatale

unread,
Nov 23, 2009, 3:26:03 PM11/23/09
to web...@googlegroups.com
On Sun, Nov 22, 2009 at 4:51 PM, lsiden <lsi...@gmail.com> wrote:

> Is paths.rb only for Rails apps, or can I use it for Mechanize
> sessions?

I don't know.

There's nothing rails specific about the idea, it's a method called by
the webrat steps which consists basically of a case stament mapping
user level descriptions to a uri.

It's probably generated by cucumber/rails though. so..

If it is it might be worth while to generate an empty rails app and
then script/generate cucumber and look in the features directory to
see what you get, it might be worthwhile to use that as a model for
how to structure things even for non-rails stuff.

Larry Siden

unread,
Nov 23, 2009, 4:46:39 PM11/23/09
to web...@googlegroups.com
I may have forgotten to mention: this is not a Rails app.  I uses Webrat::Mechanize for testing it.

I was afraid this thread was getting old so I posted a new one here you might want to look at: http://groups.google.com/group/webrat/browse_thread/thread/f3b412d36f5cf7c4

Larry Siden
http://umich.edu/~lsiden
734-926-9614
skype: lsiden
Reply all
Reply to author
Forward
0 new messages