How to efficiently test accessing a large file with Behat/Mink?

戏子无情 提交于 2019-12-12 20:44:55

问题


I'd like to write Behat/Mink scenarios to check whether certain user accounts can download a large file. I can use the When I follow "largefile.zip" event, but that appears to download the entire file.

Rather than wasting time and resources streaming the large file, I'd like to (for example) just check the result of an HTTP HEAD request, or just try to start downloading the file with an HTTP GET request and then cancel it immediately and check the response status code.

How can I do that with Behat/Mink?


回答1:


I agree with @NathanStretch regarding extending, so here's what i did.

This example is based on using http://download.thinkbroadband.com/5MB.zip as the download url. Not perfect since i don't see the file name in the response headers, but it does have Content Length.

<?php

class DownloadContext extends Behat\MinkExtension\Context\RawMinkContext {
  private $headers = [];

  /**
   * @When /^I try to download "([^"]*)"$/
   */
  public function iTryToDownload($url)
  {
    $this->headers = get_headers($url);
  }

  /**
   * @Then /^I should see in the header "([^"]*)"$/
   */
  public function iShouldSeeInTheHeader($header)
  {
    assert(in_array($header, $this->headers), "Did not see \"$header\" in the headers.");
  }
}

With a .feature file that had the following:

  Scenario: Try to download a file
    When I try to download "http://download.thinkbroadband.com/5MB.zip"
    Then I should see in the header "Content-Length: 5242880"

If you have control over the downloads themselves then you can set the filename in the headers and check for that instead of the size. Certainly better if the size can be variable since i think that would mean having to split the content length string, converting to an int and then doing a comparison. Ugh. There's probably a more elegant solution to that.

Hope that helps somewhat.



来源:https://stackoverflow.com/questions/19933924/how-to-efficiently-test-accessing-a-large-file-with-behat-mink

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!