Symfony phpunit test JWT token not working

Symfony phpunit test JWT token not working

I am trying to do the tests for my Restful API, but even though I sent the token through the headers with PHPUnit, the server always responds with: 401: JWT Token not found.

I would like to know what am I doing wrong because when I do the same request with Postman it simply works perfectly.

My unit test.

        self::ensureKernelShutdown();
        $client = static::createClient();
        $client->request(
            'GET',
            'api/inventario',
            [],
            [],
            [
               // 'HTTP_AUTHORIZATION' => 'Bearer ' . $this->token,
                'Authorization' => 'Bearer ' . $this->token
            ]
        );

        $this->assertEquals(200, $client->getResponse()->getStatusCode());

My token is perfectly stored in the $this->token variable. That is not the problem, I have also tried copying the token directly without using this variable but neither works.

If I debug the $client->getRequest()
I can find that the token is in the headers:

        'parameters' => Array &5 (
            'SERVER_NAME' => 'localhost'
            'SERVER_PORT' => 80
            'HTTP_HOST' => 'localhost'
            'HTTP_USER_AGENT' => 'Symfony BrowserKit'
            'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
            'HTTP_ACCEPT_LANGUAGE' => 'en-us,en;q=0.5'
            'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'
            'REMOTE_ADDR' => '127.0.0.1'
            'SCRIPT_NAME' => ''
            'SCRIPT_FILENAME' => ''
            'SERVER_PROTOCOL' => 'HTTP/1.1'
            'REQUEST_TIME' => 1579637668
            'Authorization' => 'Bearer eyJ0eXAiOiJKV1QiLCJhbGcaOiJSUzI1NiJ9.eyJpYXQiOjE1Nzk2Mzc2NjgsImV4cCI6MTU3OTcyNDA2OCwicm9sZXMiOlsiUk9MRV9VU0VSIl0sInVzZXJuYW1lIjoiYWRtaW5AYWRtaW4uY29tIn0.HhY34ZC4LiLrWjlgk5sgRQfyDqbXyDYRr-xKA7ueTXG-ADa-nXIPYqDMWqpPVBvxUPAKBvhaJxVS5xlQi2MmOnP3qNXzD5-oDG4Xc5Fa-3H0IYmM4Mjh4gg__sQL7dyxEgIVXMFMbKeQA0wyWg3OKF9TEbampHQZtAbDXq0IHVcuA4z2osWX07kr-NsvAJnYsgR29XhpJuFWfy3aAiM1Y2TpTB1orNq0EB8sA7o2zsUaC1DPNsKL3c2oncwERDLV3-IH9Xzha14tUzxBXvzlwhLLk3hGpcxpXRM31ZXlQIgxtRGSWVTQgkHjouZi6XUUDXpC4fH6KL-E_bGCRr6ZKCTxetdZtY39bIq1Gw8-0QFBtJMSBbMuckWPbf16YxdQFA0wJi84I05A00YL2JUNYlkzJi9dMk-Os97yiZZV6Vw_NDC9E8ly1audQXhks1kGoOxelpwHcwN-QboLSPjQkdxMQl9pVm-YT22iA2oC7LeeDfYvTMbE8RIkAXiGec0hcwRl54CFtFaZsJCdW87P24lBTzmLwExSV1FGLuzl9GaFc_Cqhp38lRG1YT3YGMyW5RflBLxdLgonM1ZF_UE9pKEpnKtSo7rnaAiJ-uwoFN000PGzqkNWH933fzEMhrpRJqsSPwjyZSWi0M1iQquxqyYvM8MeQlLi4sU375nUQu4'
            'PATH_INFO' => ''
            'REQUEST_METHOD' => 'GET'
            'REQUEST_URI' => '/api/inventario'
            'QUERY_STRING' => ''
        )

I have to mention that I have a JWT listener on success and then I create a cookie, I think this has nothing to do with the problem but I’ll mention that aswell

The problem may be because of the NelmioCorsBundle that I also have?
Here is the nelmio cors configuration

nelmio_cors:
    defaults:
        allow_credentials: true
        origin_regex: true
        allow_origin: ['%env(CORS_ALLOW_ORIGIN)%']
        allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE']
        allow_headers: ['Content-Type', 'Authorization']
        expose_headers: ['Link']
        max_age: 3600
    paths:
        '^/api/':
            allow_origin: ['*']
            allow_headers: ['X-Custom-Auth']
            allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
            max_age: 3600

        '^/':
            origin_regex: true
            allow_origin: ['^http://localhost:[0-9]+']
            allow_headers: ['X-Custom-Auth']
            allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
            max_age: 3600
            hosts: ['^api.']

Source: Symfony Questions

Leave a Reply

Your email address will not be published. Required fields are marked *