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

[8.x] Stop test suite making a live HTTP request to example.com #38410

Conversation

derekmd
Copy link
Contributor

@derekmd derekmd commented Aug 16, 2021

#37596 introduced a test that's sending an actual HTTP request to https://example.com/ on every test run. Mock that request to remove this external dependency that can intermittently fail. Running Laravel's test suite locally today I had testClonedClientsWorkSuccessfullyWithTheRequestObject() fail because a connection to example.com failed.

Also move Mockery m::close() into PHPUnit's tearDown() lifecycle since failed test cases were causing registered mocks to leak into subsequent test cases.


Fun with test suites: try disconnecting your WiFi and running the whole lot locally. A couple Laravel tests that rely on external services:

  • ValidationValidatorTest@testValidateActiveUrl()

    The active_url validation rule calls PHP global function dns_get_record(). It could be wrapped in a new container-bound class.

  • ValidationPasswordRuleTest@testUncompromised() & testMessagesOrder()

    The new Password@uncompromised() rule option does bind Illuminate\Contracts\Validation\UncompromisedVerifier to the container so this is mock-ready. A test fake could be added here to stop calling api.pwnedpasswords.com although for a full-coverage integration test we may want to actually call the API as long as requests don't get rate limited.

Event Illuminate\Http\Client\Events\ConnectionFailed can randomly be
dispatched by testClonedClientsWorkSuccessfullyWithTheRequestObject()
on some test suite runs which causes this test case to fail. This HTTP
client test is making an actual HTTP request to example.com that can
intermittently fail. Remove this unreliable external dependency.

Mock the response so the Guzzle handler middleware stack still runs to
dispatch events RequestSending & ResponseReceived.

Also move Mockery `m::close()` into PHPUnit's `tearDown()` lifecycle
since failed test cases will cause registered mocks to leak into
subsequent test cases.
@driesvints
Copy link
Member

Thanks @derekmd. I actually think the tests you mention at the end are okay to perform to the actual endpoints. We also run the Cashier Stripe tests against the actual Stripe API for example.

@taylorotwell taylorotwell merged commit f3be71b into laravel:8.x Aug 17, 2021
@derekmd derekmd deleted the http-client-test-doesnt-visit-example-dot-com branch August 17, 2021 15:42
victorvilella pushed a commit to cdsistemas/framework that referenced this pull request Oct 12, 2021
Event Illuminate\Http\Client\Events\ConnectionFailed can randomly be
dispatched by testClonedClientsWorkSuccessfullyWithTheRequestObject()
on some test suite runs which causes this test case to fail. This HTTP
client test is making an actual HTTP request to example.com that can
intermittently fail. Remove this unreliable external dependency.

Mock the response so the Guzzle handler middleware stack still runs to
dispatch events RequestSending & ResponseReceived.

Also move Mockery `m::close()` into PHPUnit's `tearDown()` lifecycle
since failed test cases will cause registered mocks to leak into
subsequent test cases.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants