Skip to content
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
17 changes: 8 additions & 9 deletions src/Interceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use League\OpenAPIValidation\PSR7\OperationAddress;
use LogicException;
use OasFake\Exception\ReplayMismatchError;
use OasFake\Exception\ValidationException;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use VCR\Cassette;
Expand Down Expand Up @@ -123,12 +122,10 @@ public function isRunning(): bool
public function handle(VcrRequest $vcrRequest): VcrResponse
{
$psrRequest = $this->converter->requestToPsr7($vcrRequest);

$operation = $this->resolveOperation($psrRequest);

$path = $this->operationPathResolver->resolve($this->schema, $psrRequest);
$method = $psrRequest->getMethod();
$operationInfo = $this->operationLookup->findByRequestPathAndMethod($path, $method);
$operation = $this->resolveOperation($psrRequest, $operationInfo);
$response = $this->operationResponder->respond($psrRequest, $path, $method, $operationInfo);
$response = $this->middlewarePipeline->process($psrRequest, $response);

Expand Down Expand Up @@ -160,7 +157,9 @@ public function handle(VcrRequest $vcrRequest): VcrResponse
public function replay(VcrRequest $request): VcrResponse
{
$psrRequest = $this->converter->requestToPsr7($request);
$operation = $this->resolveOperation($psrRequest);
$path = $this->operationPathResolver->resolve($this->schema, $psrRequest);
$operationInfo = $this->operationLookup->findByRequestPathAndMethod($path, $psrRequest->getMethod());
$operation = $this->resolveOperation($psrRequest, $operationInfo);
$response = $this->converter->vcrResponseToPsr7($this->playback($request));
$response = $this->middlewarePipeline->process($psrRequest, $response);

Expand Down Expand Up @@ -193,17 +192,17 @@ private function playback(VcrRequest $request): VcrResponse
return $response;
}

private function resolveOperation(ServerRequestInterface $request): ?OperationAddress
private function resolveOperation(ServerRequestInterface $request, ?OperationInfo $operationInfo): ?OperationAddress
{
if ($this->validateRequests) {
return $this->validator->validateRequest($request);
}

try {
return $this->validator->validateRequest($request);
} catch (ValidationException) {
if ($operationInfo === null) {
return null;
}

return new OperationAddress($operationInfo->pathPattern, $operationInfo->method);
}

private function initCassette(): void
Expand Down
11 changes: 11 additions & 0 deletions tests/Unit/InterceptorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,17 @@ public function testHandleValidatesRequestWhenEnabled(): void
$interceptor->handle($vcrRequest);
}

public function testHandleValidatesResponseWhenRequestValidationDisabled(): void
{
$this->handlers->forOperation('listPets', Handler::response(200, ['not' => 'an array']));
$interceptor = $this->createInterceptor(validateRequests: false, validateResponses: true);
$vcrRequest = new VcrRequest('GET', 'https://api.petstore.example.com/pets?limit=invalid', []);

$this->expectException(ValidationException::class);

$interceptor->handle($vcrRequest);
}

public function testHandleReturns500WhenOperationCannotBeResolved(): void
{
$interceptor = $this->createInterceptor(validateRequests: false, validateResponses: false);
Expand Down