mercredi 16 septembre 2020

Laravel TestCase->postJson to external url?

I'm trying to write a Laravel test case where I need to make an HTTP Post Request to an external API. But my $this->postJson() keeps giving me an exception. Here's an excerpt from my code:

namespace Tests\Feature;

use Tests\TestCase;

class PurchaseTest extends TestCase
{

    protected function setUp(): void
    {
        parent::setUp();
    }

    public function testPurchasePolicy()
    {
        $response = $this->postJson('https://apitest.cybersource.com/flex/v1/',['cardNumber'=>'4111111111111111']);
        print_r($response->json());
        $response->assertOk();
    }
}

This gives the result:

Array
(
    [message] =>
    [exception] => Symfony\Component\HttpKernel\Exception\NotFoundHttpException
    [file] => /var/www/projects/local/vendor/laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php
    [line] => 43
    [trace] => Array
 (
            [0] => Array
                (
                    [file] => /var/www/projects/local/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php
                    [line] => 162
                    [function] => handleMatchedRoute
                    [class] => Illuminate\Routing\AbstractRouteCollection
             ///  --> then a stack trace of laravel code that failed
)

You won't be able to fully run my code because it requires some API keys with cybersource. But i'm pretty sure my code is failing because I'm not properly telling Laravel that the API i want to access is an external resource.

What is the idiomatic way for Laravel to connect with an external URL?

Aucun commentaire:

Enregistrer un commentaire