How to efficiently test accessing a large file wit

2019-07-13 04:44发布

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条回答
贼婆χ
2楼-- · 2019-07-13 05:03

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.

查看更多
登录 后发表回答