Joeri Verdeyen bio photo

Joeri Verdeyen

Web-engineer, cyclist, Nespresso lover, Strava pusher.

Twitter LinkedIn Instagram Github Stackoverflow Last.fm Strava

How to test file downloads with Behat

Behat

Behat logo

Definitions

Add the following definitions in your Context class. These definitions allow you to download a file from a specific path, even when you are logged in. The same PHPSESSID cookie is used as in the current running Scenario. If you don’t need to keep the session cookie, you can delete these cookie related lines.

 1 class FeatureContext extends \Behat\Behat\Context\BehatContext
 2 {
 3 
 4    /**
 5     * @When /^I try to download "([^"]*)"$/
 6     */
 7     public function iTryToDownload($url)
 8     {
 9         $cookies = $this->getSession()->getDriver()->getWebDriverSession()->getCookie('PHPSESSID');
10         $cookie = new \Guzzle\Plugin\Cookie\Cookie();
11         $cookie->setName($cookies[0]['name']);
12         $cookie->setValue($cookies[0]['value']);
13         $cookie->setDomain($cookies[0]['domain']);
14 
15         $jar = new \Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar();
16         $jar->add($cookie);
17 
18         $client = new \Guzzle\Http\Client($this->getSession()->getCurrentUrl());
19         $client->addSubscriber(new \Guzzle\Plugin\Cookie\CookiePlugin($jar));
20 
21         $request = $client->get($url);
22         $this->response = $request->send();
23     }
24 
25     /**
26     * @Then /^I should see response status code "([^"]*)"$/
27     */
28     public function iShouldSeeResponseStatusCode($statusCode)
29     {
30         $responseStatusCode = $this->response->getStatusCode();
31 
32         if (!$responseStatusCode == intval($statusCode)) {
33             throw new \Exception(sprintf("Did not see response status code %s, but %s.", $statusCode, $responseStatusCode));
34         }
35     }
36 
37     /**
38     * @Then /^I should see in the header "([^"]*)":"([^"]*)"$/
39     */
40     public function iShouldSeeInTheHeader($header, $value)
41     {
42         $headers = $this->response->getHeaders();
43         if ($headers->get($header) != $value) {
44             throw new \Exception(sprintf("Did not see %s with value %s.", $header, $value));
45         }
46     }
47 }

Features

This is how you can use the new definitions in you features.

1 Scenario: Download a file as a logged in user
2     Given I am logged in as "admin" with password "admin"
3     When I try to download "downloads/pdf/generated.pdf"
4     Then I should see response status code "200"
5     Then I should see in the header "content-type":"application/pdf"

Guzzle

I used Guzzle in the download definition. Guzzle is a PHP HTTP client and framework for building RESTful web service clients. It’s a nice wrapper for PHP cURL, which is a PHP library that allows you to connect and communicate to many different types of servers with many different types of protocols

Thanks for reading

Feel free to leave a comment if you have remarks or like this post