When "visit" returns a 403

134 views
Skip to first unread message

lsiden

unread,
Nov 16, 2009, 4:14:01 PM11/16/09
to webrat
There is a website that I want my script to log into. This site is
closed, so when you direct your browser to the default page, it
returns a 403 (not authorized), but there is still a login form. In
fact, this is the only way for an unauthenticated user to get to the
login form. Nevertheless, if the "visit" method is returned a 403,
the subsequent "fill_in" command will generate:

undefined method `content' for nil:NilClass (NoMethodError)

Here is the snippet of code. It's part of a cucumber step definition:

begin
visit site
rescue
puts "\nSomething happened."
end
fill_in 'name', :with => user
fill_in 'pass', :with => pwd
click_button "Log in"

Does anyone know of a workaround for this?

Matt Wynne

unread,
Nov 17, 2009, 5:34:43 AM11/17/09
to web...@googlegroups.com

Are you using rails mode or mechanize? Where is the site hosted that
you want to visit?

cheers,
Matt

http://mattwynne.net
+447974 430184

Larry Siden

unread,
Nov 17, 2009, 12:17:47 PM11/17/09
to web...@googlegroups.com
I'm using Mechanize.

The site is http://gator1087.hostgator.com/~tes/admin/.

Curiously, when I run these commands from a cucumber step,

  puts "site=#{site}"
  visit site

The output is:
     site=http://gator1087.hostgator.com/~tes/admin/
...
      403 => Net::HTTPForbidden (WWW::Mechanize::ResponseCodeError)
...

But if I load the page in Firefox with Firebug enabled, the Net panel shows that the response was 200.

Then, from the command line:

$ wget http://gator1087.hostgator.com/~tes/admin/

--2009-11-17 12:07:21--  http://gator1087.hostgator.com/~tes/admin/
Resolving gator1087.hostgator.com... 174.120.171.2
Connecting to gator1087.hostgator.com|174.120.171.2|:80... connected.
HTTP request sent, awaiting response... 403 Forbidden
2009-11-17 12:07:22 ERROR 403: Forbidden.

So I'm a bit confused.  wget and Webrat::Mechanize both report 403, but Firebug report 200 and Firefox displays the page with a login form.  I thought there might be some redirection at work here, but 403's don't come with a redirect header (HTTP 403 - Wikipedia, the free encyclopedia ), and Webrat is supposed to follow all redirects in any case: http://blog.andrew.premdas.org/articles/2008/10/15/webrat-visits-and-redirects

Larry Siden
http://umich.edu/~lsiden
734-926-9614
skype: lsiden

Martin Emde

unread,
Nov 17, 2009, 6:17:37 PM11/17/09
to web...@googlegroups.com
This looks like a basic auth challenge with a custom login page. These aren't that common. It may be necessary to send basic auth credentials with your request rather than logging in using this form. I believe this is browser only and that http does not specify this behaviour.

I see a 403 response in every http checker that I use. I'm not sure why your firebug is responding otherwise. You can at least get on with your code by sending basic auth with this request.

It seems like the web server should be responding with a 401 Unauthorized to follow http correctly.

Martin Emde

lsiden

unread,
Nov 22, 2009, 3:01:27 PM11/22/09
to webrat
Is this what it's supposed to look like?

Given /^I am logged into "([^\"]*)" as user="([^\"]*)", password="([^
\"]*)",$/ do |site, user, pwd|
basic_auth(user, pwd)
visit site
#fill_in 'name', :with => user
#fill_in 'pass', :with => pwd
#click_button "Log in"
end


On Nov 17, 6:17 pm, Martin Emde <martin.e...@gmail.com> wrote:
> This looks like a basic auth challenge with a custom login page. These
> aren't that common. It may be necessary to send basic auth credentials with
> your request rather than logging in using this form. I believe this is
> browser only and that http does not specify this behaviour.
>
> I see a 403 response in every http checker that I use. I'm not sure why your
> firebug is responding otherwise. You can at least get on with your code by
> sending basic auth with this request.
>
> It seems like the web server should be responding with a 401 Unauthorized to
> follow http correctly.
>
> Martin Emde
>
> On Tue, Nov 17, 2009 at 9:17 AM, Larry Siden <lsi...@gmail.com> wrote:
> > I'm using Mechanize.
>
> > The site ishttp://gator1087.hostgator.com/~tes/admin/.
>
> > Curiously, when I run these commands from a cucumber step,
>
> >   puts "site=#{site}"
> >   visit site
>
> > The output is:
> >      site=http://gator1087.hostgator.com/~tes/admin/
> > ...
> >       403 => Net::HTTPForbidden (WWW::Mechanize::ResponseCodeError)
> > ...
>
> > But if I load the page in Firefox with Firebug enabled, the Net panel shows
> > that the response was 200.
>
> > Then, from the command line:
>
> > $ wgethttp://gator1087.hostgator.com/~tes/admin/
>
> > --2009-11-17 12:07:21--  http://gator1087.hostgator.com/~tes/admin/
> > Resolving gator1087.hostgator.com... 174.120.171.2
> > Connecting to gator1087.hostgator.com|174.120.171.2|:80... connected.
> > HTTP request sent, awaiting response... 403 Forbidden
> > 2009-11-17 12:07:22 ERROR *403*: Forbidden.
>
> > So I'm a bit confused.  wget and Webrat::Mechanize both report 403, but
> > Firebug report 200 and Firefox displays the page with a login form.  I
> > thought there might be some redirection at work here, but 403's don't come
> > with a redirect header (HTTP 403 - Wikipedia, the free encyclopedia<http://en.wikipedia.org/wiki/HTTP_403>), and Webrat is supposed to follow all redirects in any case:
> >http://blog.andrew.premdas.org/articles/2008/10/15/webrat-visits-and-...
>
> > Larry Siden
> >http://umich.edu/~lsiden
> > 734-926-9614
> > skype: lsiden
>
Reply all
Reply to author
Forward
0 new messages