- How does donatj/mock-webserver compare to Laravel’s built-in Http::fake() for testing API calls?
- donatj/mock-webserver offers more granular control, especially for complex scenarios like dynamic responses, rate-limiting, or custom headers. Http::fake() is simpler for basic assertions but lacks the flexibility to simulate real-world HTTP behaviors like sequential IDs or delayed replies. Use mock-webserver when Http::fake()’s limitations become a bottleneck.
- Can I use this package to mock OAuth or JWT authentication flows in Laravel?
- Yes, but with some setup. You can configure the mock server to return token responses or validate headers (e.g., `Authorization: Bearer ...`). For OAuth, define routes to return access tokens or simulate token expiration. Pair it with Laravel’s `Http::withHeaders()` to test authenticated requests. Dynamic callbacks let you simulate token refresh logic.
- Will this work with Laravel’s HTTP client (Guzzle) for making requests in tests?
- Absolutely. The mock server runs as a local HTTP endpoint, so Guzzle (used by Laravel’s `Http` facade) will treat it like any other API. Just point your `Http::get()` or `Http::post()` calls to the mock server’s URL. It’s ideal for testing API integrations without hitting live services.
- How do I mock a POST request with a JSON body in donatj/mock-webserver?
- Use the `setResponseOfPath()` method with a `Response` object. For JSON bodies, pass the raw string or an array (auto-converted to JSON). Example: `$server->setResponseOfPath('/api/webhook', new Response(200, [], json_encode(['success' => true])));`. The mock server will parse POST data and match it against your route definitions.
- Does this package support testing WebSocket connections in Laravel?
- No, this package is HTTP-only. For WebSocket testing, use dedicated tools like `pusher/pusher-php-server` or mock the WebSocket client logic manually. The mock server excels at HTTP/REST endpoints, which cover most Laravel API and webhook use cases.
- How can I simulate rate-limiting or delayed responses in my Laravel tests?
- Use PHP callbacks in the `Response` object. For rate-limiting, return a `429 Too Many Requests` status with a `Retry-After` header. For delays, pass a `Closure` that sleeps before returning a response. Example: `$server->setResponseOfPath('/api/limited', new Response(function() { sleep(2); return new Response(200, [], 'Delayed reply'); }));`
- Is donatj/mock-webserver compatible with Laravel 8 or older versions?
- Officially supported for Laravel 10+ (PHP 8.1+), but it may work with older versions if your project meets the PHP 7.2+ and `ext-sockets` requirements. Test thoroughly, as Laravel’s HTTP client (Guzzle) or testing utilities might introduce compatibility quirks. For legacy projects, consider alternatives like `wiremock/wiremock-php`.
- How do I inspect requests sent to the mock server in Laravel tests?
- The mock server logs all incoming requests by default. Access them via `$server->getRequests()` to verify paths, headers, or payloads. For Laravel, combine this with `Http::assertSent()` to cross-check. Example: `assertEquals('POST', $server->getRequests()[0]->getMethod());` for method validation.
- Can I use this package in CI/CD pipelines for parallel test execution?
- Yes, but ensure each test process uses a unique port (default: random). Shared ports will cause conflicts. For distributed CI (e.g., GitHub Actions), consider tools like WireMock for better isolation. The mock server’s lightweight design minimizes resource overhead, making it suitable for parallelized test suites.
- What are the alternatives to donatj/mock-webserver for Laravel HTTP testing?
- For Laravel, alternatives include `Http::fake()` (built-in, simple), `wiremock/wiremock-php` (feature-rich but heavier), or `vcr/vcr` (record/replay). Choose `mock-webserver` if you need dynamic responses, PHP-native control, or minimal setup. Use WireMock for advanced scenarios like persistent state or complex stubbing. Laravel’s `Http::fake()` is best for basic assertions.