NotSteve's avatar

Receiving Empty JSON in Testing

I have this code:

<?php

namespace Tests\Feature;

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

class JSONResponseStockPredictionsTest extends TestCase
{
    use RefreshDatabase;
    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function test_example()
    {
        $response = $this->get('http://146.190.112.250/api/stockpredictions');

        $response->dumpHeaders();

        $response->dump();

        $response->assertStatus(Response::HTTP_OK);

    }
}

Receiving from $response->dumpHeaders();

array:6 [
  "cache-control" => array:1 [
    0 => "no-cache, private"
  ]
  "date" => array:1 [
    0 => "Tue, 08 Aug 2023 07:15:37 GMT"
  ]
  "content-type" => array:1 [
    0 => "application/json"
  ]
  "x-ratelimit-limit" => array:1 [
    0 => 60
  ]
  "x-ratelimit-remaining" => array:1 [
    0 => 59
  ]
  "access-control-allow-origin" => array:1 [
    0 => "*"
  ]
]

and receiving from $response->dump();

{#1421
  +"data": []
}

while you definitely will get a bunch of data in http://146.190.112.250/api/stockpredictions.

Anyone have any idea why my app does this?

0 likes
3 replies
LaryAI's avatar
Level 58

The response you are receiving from the API is an empty JSON object. This could be due to various reasons, such as incorrect API endpoint, authentication issues, or the API not returning any data.

To troubleshoot the issue, you can try the following steps:

  1. Verify the API endpoint: Double-check that the API endpoint you are using is correct. Make sure there are no typos or missing parameters in the URL.

  2. Check authentication: If the API requires authentication, ensure that you are providing the necessary credentials or tokens in your test. You may need to include headers or authentication parameters in your request.

  3. Test the API separately: Try accessing the API endpoint directly in your browser or using a tool like Postman. This will help you determine if the issue is specific to your test or if the API itself is not returning any data.

  4. Debug the API response: Use the dd() function or var_dump() to inspect the response object in your test. This will help you identify any issues with the response data or parsing.

Here's an updated version of your test code with additional debugging statements:

public function test_example()
{
    $response = $this->get('http://146.190.112.250/api/stockpredictions');

    $response->dumpHeaders();

    $response->dump();

    $response->assertStatus(Response::HTTP_OK);

    // Debug the response data
    dd($response->json());
}

By using dd($response->json()), you can inspect the JSON data returned by the API and see if it matches your expectations.

If none of these steps resolve the issue, you may need to reach out to the API provider for further assistance or check their documentation for any specific requirements or limitations.

tisuchi's avatar

@sgfanega Can you explain what exactly you want to test?

Btw, this is how you can test the HTTP response.

public function test_stock_predictions_api_response()
    {
        $response = $this->get('http://146.190.112.250/api/stockpredictions');

        $response->assertStatus(Response::HTTP_OK);
    }
NotSteve's avatar

@tisuchi Sure! So I am trying to get into testing in general, and read up on articles/guides that we need to test JSON that is created/sent from our applications.

In my development, I have it as just ('/api/stockpredictions') instead of the whole ('http://146.190.112.250/api/stockpredictions'). They essentially have the same database because the live url is my own creation as well. I used that in this example to prove that my dataset has data.

So I am trying to test the JSON that comes out of my application whenever someone does a GET Request.

Please or to participate in this conversation.