我正在构建一个用户上传文件的系统。我已经设法使用量角器测试文件上传,但现在我需要验证从服务器请求原始文件时响应与上传文件相同。
用户通过单击链接来下载文件,这会触发对该文件的正常 GET 请求。由于我处理的是纯文本文件,因此下载的文件不会作为附件提供,而是显示在浏览器中。相关的PHP代码是:
header('Content-Type: text/plain');
header('Content-Length: ' . $file->getSize());
header('Content-Disposition: filename="file.txt"');
$file->openFile('rb')->fpassthru();
我有两个问题:
- 如何让量角器等到整个文件下载完成?
- 如何将下载的文件与我上传的文件进行比较?
这是我到目前为止得到的:
var path = require('path');
function uploadFile(filename) {
var absolutPath = path.resolve(__dirname, filename);
$('input#fileupload').sendKeys(absolutPath);
}
describe('Download', function() {
it('should be exactly the same as an uploaded text file', function() {
uploadFile('data/2.txt');
browser.wait(function() {
return element(by.id('download-button')).isPresent();
});
element(by.id('download-button')).click();
// Wait until page is loaded
// Compare uploaded file with downloaded file
});
});