Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Http Static Provider Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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,
    
  2. 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');
    
  3. 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();
    });
    

Implementation Patterns

Workflow Integration

  1. 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');
    
  2. 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;
    });
    
  3. 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')]);
    });
    
  4. Middleware Simulation Chain middleware to simulate auth, rate-limiting, or other HTTP behaviors:

    $provider->addMiddleware(function ($request) {
        $request->headers->set('X-Test-Header', 'value');
    });
    

Gotchas and Tips

Pitfalls

  1. No Retry Logic The provider does not implement retries or exponential backoff. Use it only for static responses or simple workflows.

  2. Case-Sensitive Matching URLs are matched exactly (including case). Normalize URLs before adding responses:

    $provider->addResponse('GET', strtolower('https://EXAMPLE.com'), ...);
    
  3. No Cookies or Sessions The provider does not persist cookies or sessions across requests. Use it for stateless APIs only.

  4. Thread Safety The provider is not thread-safe. Avoid sharing a single instance across concurrent requests (e.g., in Laravel queues).

Debugging Tips

  1. Response Logging Enable debug mode to log all requests/responses:

    $provider->setDebug(true);
    
  2. Missing Responses If a request returns null, verify:

    • The URL/method matches exactly.
    • The response was added before the request.
    • No middleware is blocking the request.
  3. Middleware Order Middleware runs in the order they are added. Use addMiddleware() at the start of the chain for auth checks.

Extension Points

  1. Custom Response Factories Extend the provider to support custom response types (e.g., XML):

    $provider->addResponse('GET', '/data', 200, '<root>...</root>', 'application/xml');
    
  2. Request Validation Validate incoming requests before processing:

    $provider->addMiddleware(function ($request) {
        if (!$request->hasHeader('Authorization')) {
            throw new \Exception('Unauthorized');
        }
    });
    
  3. 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'));
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky