Skip to content

Commit 8d4c417

Browse files
authored
Allow requests through when preventing stray requests (#608)
* Allow requests through when preventing stray requests * CHANGELOG
1 parent 9fdb1a9 commit 8d4c417

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
## v1.3.2 - 2024-12-17
11+
12+
- Allow stray requests to be ignored and pass through when being prevented.
13+
1014
## v1.3.1 - 2024-12-13
1115

1216
### Fixed

src/mantle/testing/concerns/trait-interacts-with-requests.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use InvalidArgumentException;
1414
use Mantle\Contracts\Support\Arrayable;
1515
use Mantle\Http_Client\Request;
16+
use Mantle\Support\Arr;
1617
use Mantle\Support\Collection;
1718
use Mantle\Support\Str;
1819
use Mantle\Testing\Mock_Http_Response;
@@ -53,6 +54,13 @@ trait Interacts_With_Requests {
5354
*/
5455
protected mixed $preventing_stray_requests = false;
5556

57+
/**
58+
* Stray requests that should be ignored (not reported).
59+
*
60+
* @var Collection<int, string>
61+
*/
62+
protected Collection $ignored_strayed_requests;
63+
5664
/**
5765
* Recorded actual HTTP requests made during the test.
5866
*
@@ -66,6 +74,7 @@ trait Interacts_With_Requests {
6674
public function interacts_with_requests_set_up(): void {
6775
$this->stub_callbacks = collect();
6876
$this->recorded_requests = collect();
77+
$this->ignored_strayed_requests = collect();
6978
$this->recorded_actual_requests = collect();
7079

7180
\add_filter( 'pre_http_request', [ $this, 'pre_http_request' ], PHP_INT_MAX, 3 );
@@ -96,6 +105,15 @@ public function allow_stray_requests(): void {
96105
$this->preventing_stray_requests = false;
97106
}
98107

108+
/**
109+
* Ignore a stray request.
110+
*
111+
* @param array<string>|string $url URL to ignore. Supports wildcard matching with *.
112+
*/
113+
public function ignore_stray_request( array|string $url ): void {
114+
$this->ignored_strayed_requests = $this->ignored_strayed_requests->merge( $url );
115+
}
116+
99117
/**
100118
* Fake a remote request.
101119
*
@@ -306,6 +324,11 @@ protected function get_stub_response( string $url, array $request_args ): array|
306324
return $prevent->to_array();
307325
}
308326

327+
// Check if the stray request should be ignored.
328+
if ( $this->ignored_strayed_requests->contains( fn ( $ignored_url ) => Str::is( $ignored_url, $url ) ) ) {
329+
return null;
330+
}
331+
309332
throw new RuntimeException( "Attempted request to [{$url}] without a matching fake." );
310333
}
311334

tests/Testing/Concerns/InteractsWithExternalRequestsTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,24 @@ public function test_prevent_stray_requests_no_fallback() {
302302
Http::get( 'https://example.org/path/' );
303303
}
304304

305+
// Note: This test will require a working internet connection and alley.com to be up.
306+
public function test_prevent_stray_requests_but_ignore_some() {
307+
$this->prevent_stray_requests();
308+
309+
$this->ignore_stray_request( 'https://alley.com/*' );
310+
311+
$request = Http::get( 'https://alley.com/' );
312+
313+
$this->assertEquals( 200, $request->status() );
314+
$this->assertStringContainsString( 'Alley', $request->body() );
315+
316+
$this->assertRequestSent( 'https://alley.com/' );
317+
318+
// A non-ignored request will throw an exception.
319+
$this->expectException( RuntimeException::class );
320+
Http::get( 'https://example.com/' );
321+
}
322+
305323
public function test_prevent_remote_requests_trait() {
306324
// The trait sets up the default response.
307325
$this->assertInstanceOf( Mock_Http_Response::class, $this->preventing_stray_requests );

0 commit comments

Comments
 (0)