mardi 9 février 2021

Symfony 5 tests of API endpoints return html with redirect

In a migration from Symfony 3.4 to Symfony 5.2 I'm facing following issue:

  • FOSRest
  • Symfony 5
  • Lexik

fos_rest.yaml

fos_rest:
    body_listener:
        enabled: true
    disable_csrf_role: ROLE_API
    format_listener:
        rules:
            - { path: '^/v1', priorities: [ 'json' ], fallback_format: json, prefer_extension: false }
    zone:
        - { path: ^/v1/* }

security.yaml

api_login:
  pattern: ^/v1/login
  stateless: true
  anonymous: true
  rovider: mg_users
  json_login:
    provider: mg_users
    check_path: /v1/login
    require_previous_session: false
    username_path: email
    password_path: password
    success_handler: lexik_jwt_authentication.handler.authentication_success
    failure_handler: lexik_jwt_authentication.handler.authentication_failure

Functional tests againt API endpoints that require authentication return following output if I try to output the response:

REQUEST

$client->request(
  'POST',
  '/v1/login',
  [],
  [],
  [
    'CONTENT_TYPE' => 'application/json',
  ],
  '{"email":"admin@email.it","password":"admin"}'
);

RESPONSE

<html>
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="refresh" content="0;url='https://local.api.it/v1/login'" />

        <title>Redirecting to https://local.api.it/v1/login</title>
    </head>
    <body>
        Redirecting to <a href="https://local.api.it/v1/login">https://local.api.it/v1/login</a>.
    </body>
</html>

This issue appears only with those pages/endpoints that require auhentication. If I use $client->followRedirect() the request does no longer contain email and password and I receive an INVALID JSON error in HTML instead of being JSON.

So questions:

  • How can I prevent to receive redirect page?
  • How can I receive a JSON error insteead of a HTML?

Aucun commentaire:

Enregistrer un commentaire