Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

RonanH's avatar

Can't call API endpoint from HTTP test

Hi,

I am trying to run HTTP tests on my Passport API Controllers but can't seem to access the Controller methods. I have the API setup and working fine when i access the API through Postman when using a personal access token.

When i try and call the same API controllers when running the HTTP test i created i.e. when running the phpunit command from the command line, the test never gets to the required API controller method. I think it's probably an issue with API authentication.

As per the documentation the HTTP test is calling the API endpoint with a GET request and also sending a header with the Authorization Token

$response = $this->withHeaders([ 'Authorization' => 'Bearer '.$accessToken ])->get(route('api.v1.idp-mailbox'));

This will get to the constructor of the required Class but never hits the Index method that the API route should call. Any ideas?

0 likes
1 reply
martinbean's avatar

@ronanh It’s probably an idea to follow the docs more closely.

If it’s an API endpoint, then you probably want to be calling your route with getJson instead. Otherwise, if there’s an authentication issue, Laravel is going to redirect rather than send a JSON response, and your test is going to fail.

Passport also has helpers so you don’t have to pass access tokens manually using withHeaders. You can call actingAs instead. So your test should look something like this:

Passport::actingAs($user);

$uri = route('api.v1.idp-mailbox');

$this
    ->getJson($uri)
    ->assertSuccessful();

This test will fail if the status code in the response isn’t in the 2xx range.

Please or to participate in this conversation.