Home > Blockchain >  Invalid JSON in Tests Authentifications PHPUnit
Invalid JSON in Tests Authentifications PHPUnit

Time:11-04

Good morning all , I did a complete migration of my symfony 2.8 application to version 5.4. I am now at the unit testing stage. I copied the tests to my new project, however I'm having some difficulties with the API authentication. I launched the unit tests without modification with the classic configuration of rest bundle. In my tests, I test the authentication of a user upstream in order to recover the rights necessary to test the different endpoints. When I want to authenticate with _username, and _password, I get the following error in my response content:

<!-- Invalid JSON. (400 Bad Request) -->

Here is the content of my authUser function of my abstract class which allows to authenticate the user :

<?php

namespace WORD\UserBundle\Tests\Model;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\FrameworkBundle\Client;

abstract class AbstractAuthTestCase extends WebTestCase
{
    /**
         * @var Client
         */
        protected $client = null;
    
        /**
         * ContainerInterface
         */
        //protected $container;
    
    //    public function setUp(): void
    //    {
    //        self::ensureKernelShutdown();
    //        $this->client = static::createClient();
    //       // $this->container = $this->client->getContainer();
    //        $this->client->setServerParameter('HTTPS', true);
    //        $this->client->setServerParameter('HTTP_HOST', self::$container->getParameter('api_host'));
    //        $this->client->setServerParameter('HTTP_CONTENT_TYPE', 'application/json');
    //    }
    
        public function authUser($username='stack.fr', $password='overflow', $referer='https://stack.over.local/')
        {
            $this->newClient();
            $this->client->setServerParameter('HTTP_REFERER', $referer);
    
            $this->client->request('POST', '/api/login_check', array(
                '_username' => $username,
                '_password' => $password,
            ));
    
            $response = json_decode($this->client->getResponse()->getContent(), true);
    
            if (isset($response['token'])) {
                $this->client->setServerParameter('HTTP_AUTHORIZATION', 'Bearer ' . $response['token']);
            }
    
            return $response;
        }
    
        private function newClient()
        {
            $this->client = static::createClient();
            //$this->container = $this->client->getContainer();
            $this->client->setServerParameter('HTTPS', true);
            $this->client->setServerParameter('HTTP_HOST', self::$container->getParameter('api_host'));
            $this->client->setServerParameter('HTTP_CONTENT_TYPE', 'application/json');
        }
    }

Could you tell me if I forgot something please? I'm working on Symfony 5.4 with the FOS/RESTBundle 3.4

According to the documentation, this test method is still functional on symfony. 5 version. Thank you for your help

CodePudding user response:

I finally found the solution to my problem. I had to change the format of the query:

$this->client->request(
    'POST',
    '/api/login_check',
    [],
    [],
    ['CONTENT_TYPE' => 'application/json'],
    '{"_username":"'.$username.'","_password": "'.$password.'" }'
);
    
  • Related