-2

I am writing some functional test and looks like the client can't find the controller for the route, this is the code for my test:

$response = static::createClient()->request(
  'POST',
  '/api/login_check',
  [
    'body' => [
      'username' => 'username',
      'password' => 'secret123!#'
    ]
  ]
);

When I run the test I get this error:

2020-01-13T11:53:54+00:00 [error] Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "Unable to find the controller for path "/api/login_check". The route is wrongly configured." at /var/www/html/vendor/symfony/http-kernel/HttpKernel.php line 130

also the route is configured as per route:debug

+--------------+---------------------------------------------------------+
| Property     | Value                                                   
|
+--------------+---------------------------------------------------------+
| Route Name   | api_login_check                                         
| Path         | /api/login_check                                        
| Path Regex   | #^/api/login_check$#sD                                  
| Host         | ANY                                                     
| Host Regex   |                                                         
| Scheme       | ANY                                                     
| Method       | POST                                                    
| Requirements | NO CUSTOM                                               
| Class        | Symfony\Component\Routing\Route                         
| Defaults     | NONE                                                    
| Options      | compiler_class: Symfony\Component\Routing\RouteCompiler |
+--------------+---------------------------------------------------------+

Which is exactly the same error I use to have using postman before to add the .htaccess file.

Shiny
  • 4,945
  • 3
  • 17
  • 33
user3174311
  • 1,714
  • 5
  • 28
  • 66
  • ok so should I change something in the server configuration? – user3174311 Jan 13 '20 at 12:19
  • I can’t see how what you are trying to do there in regard to the authorization header should work in the first place. As soon as you request anything that doesn’t physically exist, it gets rewritten to the index.php, and then further processing is prevented - so those last two lines would not “execute” at all then. (And sending two parameters named username and password in the request body is not HTTP Auth to begin with. Not sure what you actually wanted to use here …) – 04FS Jan 13 '20 at 12:22
  • @04FS the problem is not be able to log in or not. the problem is getting to the mentioned route/controller – user3174311 Jan 13 '20 at 12:30
  • I did not say that was the problem, I said this does not appear to make sense to begin with either way. // Does it work if you make that same request from within the browser then, instead of from your test code? And where from are you running that test code - is that from within the same symfony instance, or is that an outside system? – 04FS Jan 13 '20 at 12:34
  • @04FS ok maybe the question is badly asked. maybe I should say "why I can use a route from postman and I can't using http_client"? it is the same instance. – user3174311 Jan 13 '20 at 12:39
  • It looks like there is no controller associated with that route. The line with `Defaults` should contain an entry that looks something like: `_controller: App\Controller\SecurityController::loginCheck`. The controller action can be empty, as the firewall will intercept the request, but you should assign it to an action nonethelss, as far as I know. – dbrumann Jan 13 '20 at 12:41
  • 1
    @dbrumann authentication is provided by lexik_jwt_authentication, that's why there's not a specific controller. – user3174311 Jan 13 '20 at 12:49
  • In that case shouldn't you make sure that the content type is set correctly and encode your credentials as json body, like documented? See: https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/3-functional-testing.md#usage – dbrumann Jan 13 '20 at 12:54

1 Answers1

2

You are sending the wrong parameters with Http Client.

You are not sending the authentication payload encoded as a JSON, nor you are sending the appropriate content-type headers. That's the reason the route is not being matched correctly.

Instead of setting body, use the json key and Http Client will do the work for you.

When uploading JSON payloads, use the json option instead of body. The given content will be JSON-encoded automatically and the request will add the Content-Type: application/json automatically too

$response = static::createClient()->request(
        'POST',
        '/api/login_check',
        [
            'json' => [
                'username' => 'username',
                'password' => 'secret123!#'
            ]
        ]
    );
yivi
  • 42,438
  • 18
  • 116
  • 138