Handling 403 Forbidden Login Pages With (Ruby) Mechanize
Instead of having a dedicated login page, some sites return a 403 Forbidden HTTP status code and include the login form in an HTML body of a custom 403 page. For example, Drupal admin pages work this way. While this may seem a little odd, it works; all modern browser will display the HTML and few, if any, will note the Forbidden status.
Mechanize on the other hand raise an exception when it receives a 403 status. Fortunately, it returns the page it received as part of that exception. Here’s how to handle it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
This code also works in the case where you don’t get a forbidden status, so it can be used generically.
For bonus points you can use the same code in a Cucumber step by changing:
1
|
|
to:
1
|
|
(assuming you’ve set up “the admin page” in paths.rb).