php – 如何在Laravel中基于applicationjson标头加载路由

php – 如何在Laravel中基于applicationjson标头加载路由,第1张

概述我正在使用application / json标头控制我的控制器在收到请求时的行为方式.我需要在单元测试中使用POST来包含一个application / json头. 我试过了: public function testStore() { $this->validator ->shouldReceive('validate') 我正在使用application / Json标头控制我的控制器在收到请求时的行为方式.我需要在单元测试中使用POST来包含一个application / Json头.

我试过了:

public function testStore()    {        $this->valIDator            ->shouldReceive('valIDate')            ->once()            ->with($this->attributes)            ->andReturn(true);        $this->repository            ->shouldReceive('create')            ->once()            ->with($this->attributes)            ->andReturn($this->entity);        $this->controller            ->shouldReceive('creationSucceeded')            ->once()            ->with($this->entity);        $this->clIEnt->request('POST','shared/users',[],[                'http_CONTENT_TYPE' => 'application/Json'            ],Json_encode($this->attributes));        $this->assertResponseStatus(201);    }

而且我的控制器中的Request :: isJson()继续返回false.

我也尝试过使用’CONTENT_TYPE’=> ‘application / Json’而不是上面的http_CONTENT_TYPE.

就我而言,我使用Content-Type来确定要加载哪些控制器.这对我不起作用,因为当运行TestCase-> createApplication()时路由被加载到内存中.这意味着我的标题没有效果.

我最终制作了一个RouteInflector,允许我强制我的测试使用API路线.

class APITestCase extends TestCase{    /**     * @inheritDoc     */    public static function setUpBeforeClass()    {        /**         * Routes are loaded into memory before tests are run.         * Because of this,we can't have routing logic based on         * heads.  Using the RouteInflector we can overrIDe         * header to createApplication() and must use a constant         * to force the RouteInflector to use API controllers.         */        RouteInflector::isJson(true);    }    public function setUp()    {        parent::setUp();        //Lets do this right        $this->clIEnt->setServerParameter('http_CONTENT_TYPE','application/Json');        $this->clIEnt->setServerParameter('http_ACCEPT','application/Json');    }}

偏转:

class RouteInflector{    /** @var bool */    protected static $isJson = false;    /**     * RevIEw the request details and determine which controller     * subpackage should be used.     * We Could also check the request source to help determine the     * package.     *     * Defaults to Web.     *     * @return string     */    public function getControllerSubpackage()    {        if (self::isJson() || Request::isJson()) {            return 'API';        }        return 'Web';    }    /**     * Used by tests to tell routing that the current request     * is a Json request.     *     * @see \Tests\APITestCase     *     * @param bool|null $isJson     *     * @return bool Only provIDed if parameter is null     */    public static function isJson($isJson = null)    {        if (is_null($isJson)) {            return self::$isJson;        } else {            self::$isJson = $isJson;        }    }}
总结

以上是内存溢出为你收集整理的php – 如何在Laravel中基于application / json标头加载路由全部内容,希望文章能够帮你解决php – 如何在Laravel中基于application / json标头加载路由所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1268021.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存