【问题标题】:How to load routes based on application/json header in Laravel如何在 Laravel 中基于 application/json 标头加载路由
【发布时间】:2014-05-11 08:00:33
【问题描述】:

我正在使用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

【问题讨论】:

  • 对不起没有实现测试方法..检查这个链接,它可能会有所帮助。stackoverflow.com/questions/20093897/…
  • @ytsejam - 感谢发帖。不幸的是,使用 $this->client->setServerParameter('HTTP_CONTENT_TYPE', 'application/json'); 对我不起作用,无论是 GET、DELETE、POST 还是 PUT 请求。
  • 您的共享/用户看起来如何?请求可以不绑定到响应 http-content-wise。
  • HTTP_Content-Type 怎么样?
  • @kapad 查看这个问题的答案

标签: php unit-testing laravel phpunit


【解决方案1】:

就我而言,我使用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;
        }
    }
}

【讨论】:

    猜你喜欢
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    • 2020-12-16
    • 2020-03-07
    • 2020-11-16
    相关资源
    最近更新 更多