-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Add shorthands for fake HTTP responses #53663
Add shorthands for fake HTTP responses #53663
Conversation
Interpreting any number between 100 and 600 as a status code can lead to issues when this behavior is not expected (principle of least astonishment) |
@shaedrich, I might agree if this were a change to app-level code. However, this is a shorthand for faking an HTTP response in a test. At this level, the developer is in full control. Furthermore this is a new feature. So there's not chance of breaking existing code. |
I'm aware of it being new. But being new doesn't mean it cannot be confusing. Sure, you can say "if you're in full control, it's your fault if it doesn't work and it's your responsibility to figure out on your own why it doesn't work", but this just isn't following the principle of least astonishment nonetheless. |
What if you only need to return a number in response? For example: Http::fake([
'google.com' => 'Hello World',
'github.com' => ['foo' => 'bar'],
'forge.laravel.com' => 204,
'example.com' => Http::response(204),
]); In this case, the number 204 for requests to This is a non-obvious behavior. |
@andrey-helldar, you can make it a string. This is a new shorthand. You don't have to use it. It's new behavior and has no conflict. The following are equivalent: Http::fake([
'forge.laravel.com' => '204',
'example.com' => Http::response(204),
]); |
@jasonmccreary, you know the application context, now imagine a new developer on a project not reading the changelog for Laravel releases. And he sees the following, for example: Http::fake([
'foo.laravel.com' => 204,
'bar.laravel.com' => '204',
'baz.laravel.com' => Http::response(204),
]); Question: how is he supposed to realize that:
? That's why I say it's non-obvious behavior. It's clearly misleading. With array and text I can still understand how and if just the number When I read this code: Http::fake([
'google.com' => 'Hello World',
'github.com' => ['foo' => 'bar'],
'forge.laravel.com' => 204,
]); I perceive and expect to see the following: Http::fake([
'google.com' => Http::response(body: 'Hello World'),
'github.com' => Http::response(body: ['foo' => 'bar']),
'forge.laravel.com' => Http::response(body: 204),
]); But no, I would have to spend some time to understand why 204 is returned not in the body of the response with code 200, but with a response without a body and code 204. Then I'd have to spend some more time rewriting it to: Http::fake([
'google.com' => Http::response(body: 'Hello World'),
'github.com' => Http::response(body: ['foo' => 'bar']),
'forge.laravel.com' => Http::response(status: 204),
]); I'll simplify my question: what is the difference between |
@andrey-helldar, if you find it misleading, you don't have to use it. This is provided as a shorthand for devs that write a lot of tests and want to save a few keystrokes. They understand the tradeoff and can educate their team. 👍 As noted in the PR, there are other shorthands for fakes within Laravel. So this isn't a new concept. |
@jasonmccreary, I like the proposed shorthands, but not for statuses. They are misleading, IMHO :) I would suggest for statuses to explicitly return |
@andrey-helldar I agree. The overall idea is pretty convenient and neat. Just the status code is encouraging trouble. Thanks for your explanations. They should make the problem clearer 👍🏻 |
Just noticed this discussion. Wanted to drop in another opinion: the status code feature is lovely. |
Similar to
Process::fake()
, this adds shorthands when faking HTTP responses. A shorthand for passing arrays already existed. This adds an explicit test for that, plus shorthands for strings for the body or an integer for a valid HTTP status code (1xx-5xx).Before
After