AppDividend
Latest Code Tutorials

Laravel 9 HTTP Testing: How to Use HTTP Testing

Testing an HTTP request is an important task as a backend developer in Laravel to verify that our project works fine. Gladly, Laravel provides a very fluent API to test the HTTP request.

If you are new to testing the checkout my below article.

Laravel 9 Testing

Laravel HTTP Testing

Laravel provides a remarkably fluent API for making HTTP requests to your application and inspecting the responses. To create a request to your application, you may call the getpostputpatch, or delete methods within your test.

We are learning Laravel HTTP Testing with an example, so first, make one copy of a Laravel project.

Step 1: Make one API route to test.

Go to the routes  >>  web.php file and register this route.

<?php

// web.php

Route::get('/getdata', function(){
  return response()->json(['website'=>'AppDividend']);
});

Step 2: Make one test file.

In your terminal, type the following command in it.

php artisan make:test HTTPTest

Now, this file has a testExample() function by default. Therefore, we need to add the following code to it.

public function testExample()
{
   $response = $this->get('/getdata');
   $response->assertJson(['website'=>'AppDividend']);
}

So it is the example of an assertJson() function.

$response>assertJson(array $data)

This function checks the return response from the original route. For example, in the web.php file, we are returning the following JSON response.

// web.php

Route::get('/getdata', function(){
  return response()->json(['website'=>'AppDividend']);
});

And in code, we are also returning the same array response.

Now, run the test by typing the following command.

vendor/bin/phpunit

or

phpunit

You will see all the tests will pass. Now, change the JSON response in the original web.php route.

Now, rerun the test, and it will fail. So we are testing the response of an API point. So this is one kind of HTTP testing.

$response>assertViewIs($value)

This function checks the return view from either a web.php file or a particular controller file.

For example, let us test the following route.

<?php

// web.php

Route::get('/', function () {
    return view('welcome');
});
Route::get('/getdata', function(){
  return response()->json(['website'=>'AppDividend']);
});

Here, in the home route, we return the welcome view, welcome.blade.php inside resources  >>  views folder.

Now, write another test for it inside the HTTPTest.php file.

We need to create another function inside the HTTPTest.php file and name it whatever you want it.

<?php

// HTTPTest.php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class HTTPTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {
      $response = $this->get('/getdata');
      $response->assertJson(['website'=>'AppDividend']);
    }

    public function testView()
    {
      $response = $this->get('/');

      $response->assertViewIs('welcome');
    }
}

Here, I have named it testView() because I need to test the return view from the web API point.

Now, again run the test, and you will pass all the tests. However, if you change the view name and again run the test, it will surely fail.

$response>assertStatus($code)

This function checks the status code return from the web API point. For example, if we request the route that returns another response code and our test returns another test code, the test will fail; otherwise, both return the same status code, then our test passes.

In the HTTPTest.php file, make the following function in it.

// HTTPTesting.php

public function testStatusCode()
{
      $response = $this->get('/status');

      $response->assertStatus(200);
}

And add the route in the web.php file.

// web.php

Route::get('/status', function () {
    abort(404);
});

Both response codes are different, so our test will fail. And if the test expects the same result as route return, it will pass the test.

Our Final HTTPTest.php file looks like this.

<?php

// HTTPTest.php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class HTTPTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {
      $response = $this->get('/getdata');
      $response->assertJson(['website'=>'AppDividend']);
    }

    public function testView()
    {
      $response = $this->get('/');

      $response->assertViewIs('welcome');
    }

    public function testStatusCode()
    {
      $response = $this->get('/status');

      $response->assertStatus(200);
    }
}

There are more functions to test the results, which you can get at the following link.

That’s it for this tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.