Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Http::fake does not preserve trailing zeros in JSON responses #54482

Closed
maxwellmezadre opened this issue Feb 5, 2025 · 4 comments
Closed

Http::fake does not preserve trailing zeros in JSON responses #54482

maxwellmezadre opened this issue Feb 5, 2025 · 4 comments

Comments

@maxwellmezadre
Copy link

maxwellmezadre commented Feb 5, 2025

Laravel Version

11.39.1

PHP Version

8.4

Database Driver & Version

SQLite for macOS 15.3 on arm64 (Homebrew)

Description

When using Http::fake() in tests with fixture responses containing decimal numbers formatted with trailing zeros (e.g., 10.0), the numeric values are decoded without the trailing zero (resulting in 10). This causes tests that rely on exact formatting to fail. Although this behavior is a consequence of PHP’s json_decode() (which converts numbers to their numeric representation), it negatively impacts tests that expect the exact string representation as defined in the fixture.

Steps To Reproduce

  1. Create a JSON Fixture:

    • Create a file at tests/fixtures/sample.json with the following content:
      {
          "value": 10.0,
          "other": "data"
      }
    • Note: The fixture contains 10.0 with a trailing zero.
  2. Set Up the Test with Http::fake():

    • In your test file, add the following code to load the fixture and set up the fake response:
      use Illuminate\Support\Facades\Http;
      
      // Load the fixture file
      $fixture = json_decode(file_get_contents(base_path('tests/fixtures/sample.json')), true);
      
      // Fake the HTTP response
      Http::fake([
          'api.example.com/*' => Http::response($fixture),
      ]);
  3. Trigger the HTTP Request:

    • Make the HTTP request that will be intercepted by Http::fake():
      $response = Http::get('api.example.com/data');
      $data = $response->json();
  4. Compare the Returned Data:

    • Write an assertion comparing the returned data with the fixture:
      expect($data)->toBe($fixture);
    • The test will fail because the numeric value 10.0 from the fixture is decoded as 10, causing a mismatch.
@cosmastech
Copy link
Contributor

You'll have to fetch the content and parse it to JSON yourself, using the json_decode option to preserve zero fractions.

I think that's something like:

$response = Http::get('api.example.com/data');
$data = json_decode($response->body(), true, flags: JSON_PRESERVE_ZERO_FRACTION);

@maxwellmezadre
Copy link
Author

Hi,

Thanks for your response. I have an alternative suggestion: would it be possible to add an option directly to the Http facade to specify JSON decode flags? For example, something like:

$response = Http::get('api.example.com/data');
$data = $response->json(flags: JSON_PRESERVE_ZERO_FRACTION);

This would allow developers to preserve trailing zeros in decimals (e.g., 10.0) without having to manually decode the response with json_decode().

Is there any plan or possibility to introduce such a feature in the Http facade? It would greatly simplify cases where the exact numeric formatting is important.

Thanks!

Copy link

github-actions bot commented Feb 6, 2025

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

@maxwellmezadre
Copy link
Author

maxwellmezadre commented Feb 6, 2025

I created this PR #54492

Edit: My PR was refused, so I'll use the solution above anyway, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants