Skip to content
Advertisement

Symfony 5.1 to 5.2 unit testing do not authenticate via guard authenticator

I ‘m working with Symfony 5.1 using the firewall and a guard authenticator;

        secured_area:
        pattern:  ^/
        stateless: true
        provider: chain_provider
        guard:
            authenticators:
                - App<pathToAuthenticator>
            entry_point: App<pathToAuthenticator>

All was working fine with 5.1. I’ve recently upgrade to Symfony 5.2 and almost all working fine except Unit tests. When i do a step by step debugging, it’s not stepping into the Authenticator anymore like it was the case previously (in 5.1).

Any parameter missing in my configuration ? Default parameter have change ?

Advertisement

Answer

Eventually, i ‘ve found the pb, and the solution, in my test do :

self::$_client->request(
    'GET',
    '/items',
    array(),
    array(),
    array('HTTP_Accept' => 'application/json', 'CONTENT_TYPE' => 'application/json'),
    json_encode($payload)
);

And i have a request matcher that filter on JSON request :

class JsonRequestMatcher implements RequestMatcherInterface
{
    public function matches(Request $request)
    {
        $format = $request->getPreferredFormat();
        return $format === "json";
    }
}

For a reason i have not determined, when the header send ‘HTTP_Accept’ $format value in the request matcher is “html” and the request is not processed … then it crash futher on.

When i change ‘HTTP_Accept’ to ‘HTTP_ACCEPT’, (yes, just upper case the header !) it works, and the value for $format is the one expected (ie : “json”). Thanks to Cerad for his support and for some commands i just don’t know was existing in Symfony 😉

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement