boson-php/http-static-provider
Laravel/PHP package that provides a static HTTP provider for Boson, useful for serving fixed responses in tests, mocks, or offline scenarios. Simple setup to return predefined status, headers, and body without making real network requests.
Installation Add the package via Composer:
composer require boson-php/http-static-provider
Register the service provider in config/app.php:
Boson\Http\StaticProvider\StaticProviderServiceProvider::class,
Basic Usage The package provides a static HTTP client provider for testing or lightweight requests. Initialize it in a Laravel service or controller:
use Boson\Http\StaticProvider\StaticProvider;
$provider = new StaticProvider();
$response = $provider->get('https://example.com');
First Use Case
Replace Http facade calls in tests or scripts with this provider for predictable, non-network-dependent responses:
// In a test
$this->app->singleton(StaticProvider::class, function () {
return new StaticProvider();
});
Mocking HTTP Calls Use the provider to stub API responses in tests or local development:
$provider = new StaticProvider();
$provider->addResponse('GET', 'https://api.example.com/users', 200, ['name' => 'John']);
$response = $provider->get('https://api.example.com/users');
Dependency Injection
Bind the provider to Laravel’s container for seamless replacement of Http facade:
$this->app->bind(\Boson\Http\StaticProvider\StaticProvider::class, function () {
$provider = new StaticProvider();
$provider->addResponse('POST', '/login', 200, ['token' => 'fake']);
return $provider;
});
Dynamic Responses Use closures for dynamic responses (e.g., based on request data):
$provider->addResponse('POST', '/search', function ($request) {
return response()->json(['results' => $request->query('q')]);
});
Middleware Simulation Chain middleware to simulate auth, rate-limiting, or other HTTP behaviors:
$provider->addMiddleware(function ($request) {
$request->headers->set('X-Test-Header', 'value');
});
No Retry Logic The provider does not implement retries or exponential backoff. Use it only for static responses or simple workflows.
Case-Sensitive Matching URLs are matched exactly (including case). Normalize URLs before adding responses:
$provider->addResponse('GET', strtolower('https://EXAMPLE.com'), ...);
No Cookies or Sessions The provider does not persist cookies or sessions across requests. Use it for stateless APIs only.
Thread Safety The provider is not thread-safe. Avoid sharing a single instance across concurrent requests (e.g., in Laravel queues).
Response Logging Enable debug mode to log all requests/responses:
$provider->setDebug(true);
Missing Responses
If a request returns null, verify:
Middleware Order
Middleware runs in the order they are added. Use addMiddleware() at the start of the chain for auth checks.
Custom Response Factories Extend the provider to support custom response types (e.g., XML):
$provider->addResponse('GET', '/data', 200, '<root>...</root>', 'application/xml');
Request Validation Validate incoming requests before processing:
$provider->addMiddleware(function ($request) {
if (!$request->hasHeader('Authorization')) {
throw new \Exception('Unauthorized');
}
});
Global Configuration Override defaults (e.g., base URL) via Laravel config:
'static_provider' => [
'base_url' => 'https://stubbed.example.com',
],
Then access via:
$provider->setBaseUrl(config('static_provider.base_url'));
How can I help you explore Laravel packages today?