Skip to content

Commit

Permalink
Allow requests through when preventing stray requests (#608)
Browse files Browse the repository at this point in the history
* Allow requests through when preventing stray requests

* CHANGELOG
  • Loading branch information
srtfisher authored Dec 17, 2024
1 parent 9fdb1a9 commit 8d4c417
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions src/mantle/testing/concerns/trait-interacts-with-requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,6 +54,13 @@ trait Interacts_With_Requests {
*/
protected mixed $preventing_stray_requests = false;

/**
* Stray requests that should be ignored (not reported).
*
* @var Collection<int, string>
*/
protected Collection $ignored_strayed_requests;

/**
* Recorded actual HTTP requests made during the test.
*
Expand All @@ -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 );
Expand Down Expand Up @@ -96,6 +105,15 @@ public function allow_stray_requests(): void {
$this->preventing_stray_requests = false;
}

/**
* Ignore a stray request.
*
* @param array<string>|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.
*
Expand Down Expand Up @@ -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." );
}

Expand Down
18 changes: 18 additions & 0 deletions tests/Testing/Concerns/InteractsWithExternalRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down

0 comments on commit 8d4c417

Please sign in to comment.