PhpRiot
Follow phpriot on Twitter
Sponsored Link
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Behat and HTTP Auth (and Goutte)

Note: This article was originally published at Planet PHP on 21 March 2012.
Planet PHP

So I've been trying out Behat for some REST testing recently and came across an interesting situation - I needed to send HTTP Auth credentials as a part of my test. Of course, there's a method for this on the Session object (setBasicAuth) but it's not implemented as a default phrase. It took me a bit to make it to the (now logcial) point that I needed to set these before I used the a€oGiven I am ona€Ša€ phrase to go to the page.

So, to accomplish this, I set up a custom a€oGivena€ phrase to set them: a€oGiven that I log in with a€˜username' and a€˜password'a€ so that my full Scenario looks like:

Scenario: Get the main Index view Given that I log in with "myuser" and "mypass" And I am on "/index/view" Then the response should contain "TextFromPageTitle"

And the code is super simple:

/** * Features context. */ class FeatureContext extends Behat\Mink\Behat\Context\MinkContext { /** * @Given /^that I log in with "([^"]*)" and "([^"]*)"$/ */ public function thatILogInWithAnd($username, $password) { $this-getSession()-setBasicAuth($username,$password); } }

Hope this helps someone else out there trying to send HTTP Auth pre-connection. I'm sure there's probably an easier way that I'm missing, but this seemed the simplest to me.