From 78d075f7369ff8a0b8f0ed5ee8726a3a4a5f5e63 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Tue, 17 Dec 2024 13:27:21 -0500 Subject: [PATCH 1/2] Allow requests through when preventing stray requests --- .../trait-interacts-with-requests.php | 23 +++++++++++++++++++ .../InteractsWithExternalRequestsTest.php | 18 +++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/mantle/testing/concerns/trait-interacts-with-requests.php b/src/mantle/testing/concerns/trait-interacts-with-requests.php index e116088d..082fb95e 100644 --- a/src/mantle/testing/concerns/trait-interacts-with-requests.php +++ b/src/mantle/testing/concerns/trait-interacts-with-requests.php @@ -13,6 +13,7 @@ use InvalidArgumentException; use Mantle\Contracts\Support\Arrayable; use Mantle\Http_Client\Request; +use Mantle\Support\Arr; use Mantle\Support\Collection; use Mantle\Support\Str; use Mantle\Testing\Mock_Http_Response; @@ -53,6 +54,13 @@ trait Interacts_With_Requests { */ protected mixed $preventing_stray_requests = false; + /** + * Stray requests that should be ignored (not reported). + * + * @var Collection + */ + protected Collection $ignored_strayed_requests; + /** * Recorded actual HTTP requests made during the test. * @@ -66,6 +74,7 @@ trait Interacts_With_Requests { public function interacts_with_requests_set_up(): void { $this->stub_callbacks = collect(); $this->recorded_requests = collect(); + $this->ignored_strayed_requests = collect(); $this->recorded_actual_requests = collect(); \add_filter( 'pre_http_request', [ $this, 'pre_http_request' ], PHP_INT_MAX, 3 ); @@ -96,6 +105,15 @@ public function allow_stray_requests(): void { $this->preventing_stray_requests = false; } + /** + * Ignore a stray request. + * + * @param array|string $url URL to ignore. Supports wildcard matching with *. + */ + public function ignore_stray_request( array|string $url ): void { + $this->ignored_strayed_requests = $this->ignored_strayed_requests->merge( $url ); + } + /** * Fake a remote request. * @@ -306,6 +324,11 @@ protected function get_stub_response( string $url, array $request_args ): array| return $prevent->to_array(); } + // Check if the stray request should be ignored. + if ( $this->ignored_strayed_requests->contains( fn ( $ignored_url ) => Str::is( $ignored_url, $url ) ) ) { + return null; + } + throw new RuntimeException( "Attempted request to [{$url}] without a matching fake." ); } diff --git a/tests/Testing/Concerns/InteractsWithExternalRequestsTest.php b/tests/Testing/Concerns/InteractsWithExternalRequestsTest.php index 1afc5e58..915f2e44 100644 --- a/tests/Testing/Concerns/InteractsWithExternalRequestsTest.php +++ b/tests/Testing/Concerns/InteractsWithExternalRequestsTest.php @@ -302,6 +302,24 @@ public function test_prevent_stray_requests_no_fallback() { Http::get( 'https://example.org/path/' ); } + // Note: This test will require a working internet connection and alley.com to be up. + public function test_prevent_stray_requests_but_ignore_some() { + $this->prevent_stray_requests(); + + $this->ignore_stray_request( 'https://alley.com/*' ); + + $request = Http::get( 'https://alley.com/' ); + + $this->assertEquals( 200, $request->status() ); + $this->assertStringContainsString( 'Alley', $request->body() ); + + $this->assertRequestSent( 'https://alley.com/' ); + + // A non-ignored request will throw an exception. + $this->expectException( RuntimeException::class ); + Http::get( 'https://example.com/' ); + } + public function test_prevent_remote_requests_trait() { // The trait sets up the default response. $this->assertInstanceOf( Mock_Http_Response::class, $this->preventing_stray_requests ); From c7d4c357f5e93c0bfe067c67ac48c39fa45cabb1 Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Tue, 17 Dec 2024 13:29:03 -0500 Subject: [PATCH 2/2] CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6aeec7cd..65c78251 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +## v1.3.2 - 2024-12-17 + +- Allow stray requests to be ignored and pass through when being prevented. + ## v1.3.1 - 2024-12-13 ### Fixed