【问题标题】:PHPUnit accept application/jsonPHPUnit 接受应用程序/json
【发布时间】:2015-12-18 10:19:31
【问题描述】:

我已经用 PHPUnit 编写了测试,我想向服务器发送信息,我希望得到 json 响应。

我试图发送带有client->request 方法的'accept' => 'application/json' 标头作为第五个参数的第三个,但它不起作用。

我的代码:

class PostControllerTest extends WebTestCase
{
    protected $client;

    public function setUp()
    {
        self::bootKernel();
        $this->client = static::createClient();
    }

    protected function testOK()
    {
        $this->client->request('GET', '/posts', [
            'accept => 'application/json',
        ], [], [
            'accept => 'application/json',
        ]);
    }
}

但是如果我对谷歌浏览器的 RESTplugin 做同样的事情并发送这些标头,一切都可以。

我不知道有什么问题,因为理论上它应该有效。

【问题讨论】:

  • 到底出了什么问题?您的客户端请求中是否出现错误,或者您是否收到类似 application/xml 的结果?
  • 服务器返回正常的 html 站点。就像没有任何特殊的标题一样。
  • 这里的答案是什么?

标签: phpunit webtest


【解决方案1】:

REST 服务查看Content-Type 而不是accepts。添加 'Content-Type' => 'application/json' 到您的请求标头应该可以解决问题。

【讨论】:

  • 还是同样的问题。如果我使用 accept 和 RestClient chrome 扩展服务器返回 json。所以accept 应该没问题。
猜你喜欢
  • 1970-01-01
  • 2014-06-07
  • 2017-08-29
  • 2018-03-21
  • 2015-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多