Symfony basic auth doesn't work specifically in test

24 Views Asked by At

I am using symfony 6.4.
My security config is very simple:

security:
    # https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
    password_hashers:
        Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'plaintext'
    # https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider
    providers:
        users_in_memory:
            memory:
                users:
                    'dev':
                        password: 'dev'
                        roles:
                            - 'ROLE_HTTP_AUTH_API'
    firewalls:
        main:
            stateless: true
            http_basic:
                provider:
                    users_in_memory

For the sake of testing I specified user and password in security.yaml.
If I use Postman for requests, everything works fine.
I use Basic Auth with dev:dev credentials. In headers of request I have Authorization Basic ZGV2OmRldg==. And everything works as intended.

However in functional tests I constantly getting 401 error. If I var_dump Request headers in test, there is a header Authorization Basic ZGV2OmRldg==, so authorization should work, because same header work in postman request

What I tried and it didn't work:

  1. Passing creds in createClient: self::createClient([], ['PHP_AUTH_USER' => 'dev', 'PHP_AUTH_PW' => 'dev']);
  2. Adding header to request manually: $client->request('GET', self::URL, [], [], ['HTTP_AUTHORIZATION' => 'Basic ' . base64_encode('dev:dev')]);
  3. Adding headr 'Content-Type' => 'application/json' just in case Nothing worked. What else can I try?

My test is very straightforward:

$this->client->request(
            'PUT',
            self::URL,
            [],
            [],
            [],
            self::REQUEST_BODY
        );

        self::assertResponseStatusCodeSame(404); //here I get 401 error and test fails
0

There are 0 best solutions below