在代码接收中将文件发送到Restful服务 [英] Send file to Restful service in codeception

查看:76
本文介绍了在代码接收中将文件发送到Restful服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试用于文件上传的静态API测试.

I would like to test restful API test for file uploading.

我尝试运行:

 $I->sendPOST($this->endpoint, $postData, ['file' => 'example.jpg']);

,并且我希望它的行为与用户在名称为file的文件输入中发送的example.jpg文件相同,但这种方式似乎不起作用.我得到了:

and I would like it to behave the same as user sent example.jpg file in file input with name file but it doesn't seem to work this way. I'm getting:

[PHPUnit_Framework_ExceptionWrapper]上传的文件必须是数组或UploadedFile的实例.

[PHPUnit_Framework_ExceptionWrapper] An uploaded file must be an array or an instance of UploadedFile.

是否可以在代码接收中使用REST插件上传文件?文档非常有限,很难说怎么做.

Is it possible to upload file using REST plugin in codeception? Documentation is very limited and it's hard to say how to do it.

我还在使用Postman插件测试Google Chrome的API,并且可以使用该插件毫无问题地上传文件.

I'm also testing API using Postman plugin to Google Chrome and I can upload file without a problem using this plugin.

推荐答案

经过测试似乎可以正常工作,我们需要使用UploadedFile对象作为文件.

After testing it seems to make it work we need to use UploadedFile object as file.

例如:

$path = codecept_data_dir();
$filename = 'example-image.jpg';

// copy original test file to have at the same place after test
copy($path . 'example.jpg', $path . $filename);

$mime = 'image/jpeg';

$uploadedFile = new \Symfony\Component\HttpFoundation\File\UploadedFile($path . $filename, $filename, $mime,
    filesize($path . $filename));

$I->sendPOST($this->endpoint, $postData, ['file' => $uploadedFile]);

这篇关于在代码接收中将文件发送到Restful服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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