如何使用Behat/Mink有效地测试对大文件的访问? [英] How to efficiently test accessing a large file with Behat/Mink?

查看:69
本文介绍了如何使用Behat/Mink有效地测试对大文件的访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写Behat/Mink方案来检查某些用户帐户是否可以下载大文件.我可以使用When I follow "largefile.zip"事件,但这似乎是要下载整个文件.

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.

我不想浪费时间和资源来传输大文件,例如,我只是检查HTTP HEAD请求的结果,或者尝试尝试通过HTTP GET请求开始下载文件然后立即将其取消并检查the response status code.

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.

如何使用Behat/Mink做到这一点?

How can I do that with Behat/Mink?

推荐答案

在扩展方面,我同意@NathanStretch的观点,所以这就是我所做的.

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

此示例基于使用 http://download.thinkbroadband.com/5MB.zip作为下载网址.由于我在响应标题中看不到文件名,因此并不完美,但是它确实具有内容长度.

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.");
  }
}

.feature文件具有以下内容:

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"

如果您可以自行控制下载内容,则可以在标题中设置文件名,然后检查文件名而不是文件大小.如果大小可以更改,肯定会更好,因为我认为这意味着必须拆分内容长度字符串,转换为int然后进行 then 进行比较.啊.可能有一个更优雅的解决方案.

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.

希望能有所帮助.

这篇关于如何使用Behat/Mink有效地测试对大文件的访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆