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

Allow requests through when preventing stray requests #608

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading