2

问题:

当使用带有 PhpBrowser 驱动程序的 REST 模块从代码接收测试发出请求时,没有数据和文件进入 Silex 应用程序。

    // ApiTester $I
    $I->wantTo('Submit files');

    // prepare:
    $data = ['key' => 'value'];
    $files = [
        'file_key' => 'path/to/file.doc',
        'file2_key' => 'path/to/file2.doc'
    ];

    // act:
    $I->haveHttpHeader('Content-Type', 'multipart/form-data');
    $I->sendPOST('/attachments/', $data, $files);

当前响应

 I have http header "Content-Type","multipart/form-data"
 I send post "/attachments/",{"key":"value"},{"file_key":"/path/to/file/...}
  [Request] POST http://localhost/attachments/ {"key":"value"}
  [Request Headers] {"Content-Type":"multipart/form-data"}
  [Page] http://localhost/attachments/
  [Response] 400
  [Request Cookies] []
  [Response Headers] {"Date":["Tue, 25 Oct 2016 09:15:31 GMT"],"Server":["Apache/2.4.10 (Debian)"],"Cache-Control":["no-cache"],"Access-Control-Allow-Origin":["*"],"Access-Control-Allow-Headers":["Content-Type, Authorization"],"Access-Control-Allow-Methods":["GET,PATCH,PUT,POST,HEAD,DELETE,OPTIONS"],"Content-Length":["1235"],"Connection":["close"],"Content-Type":["application/json"]}
  [Response] {"status":400,"meta":{"time":"2016-10-25 09:15:31"},"title":"Invalid Request","errors":"No data received","details":{"error_class":"Symfony\Component\HttpKernel\Exception\BadRequestHttpException"

试过:

  • 更改 Content-Type 标头
  • 将传递给 sendPOST 的文件数组更改为以下数组:
    • 文件路径文件对象( UploadedFile )
    • 文件数组

该测试适用于 Silex 驱动程序,但这不是 CI 服务器上的选项。我们还检查了 Postman,API 路由按预期工作,文件已发送且一切正常。

4

1 回答 1

5

实际问题:

  • $I->haveHttpHeader('Content-Type', 'multipart/form-data');覆盖Content-Typehttp 库(在 phpbrowser 是 guzzle 中)应设置的 以包含边界,它与this相关。
  • 另请注意,$I->header每次请求后不会重置,取消设置它使用$I->deleteHeader('Content-Type');

解决方案

  • 发送文件时不要设置“Content-Type”标头。
于 2016-10-30T09:38:37.373 回答