如何在Laravel中基于application/json头加载路由 [英] How to load routes based on application/json header in Laravel

查看:546
本文介绍了如何在Laravel中基于application/json头加载路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用application/json标头来控制控制器在收到请求时的行为.我需要在单元测试中的POST包含application/json标头.

I'm using the application/json header to control how my controller acts when a request is received. I need for the POST in my unit test to include an application/json header.

我尝试过:

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.

And it the Request::isJson() in my controller continues to return false.

我还尝试使用'CONTENT_TYPE' => 'application/json'代替上面的HTTP_CONTENT_TYPE.

I also tried using 'CONTENT_TYPE' => 'application/json' instead of the HTTP_CONTENT_TYPE above.

推荐答案

在我的情况下,我正在使用Content-Type确定要加载的控制器.这对我不起作用,因为在运行TestCase->createApplication()时,路由已加载到内存中.这意味着我的标题无效.

In my case, I was using Content-Type to determine which controllers to load. This didn't work for me, because routes are loaded into memory when TestCase->createApplication() is run. This means my headers had no effect.

我最终制作了RouteInflector,使我可以强制我的测试使用Api路由.

I ended up making a RouteInflector that allows me to force my tests to use the Api routes.

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;
        }
    }
}

这篇关于如何在Laravel中基于application/json头加载路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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