factory, streamFactory: $this->factory); $request = $this->factory->createServerRequest('POST', 'http://localhost/'); $response = $middleware->process($request, $this->passthroughHandler); $this->assertSame(200, $response->getStatusCode()); } #[TestDox('rejects missing header when 2025-03-26 backwards-compat default is not in supportedVersions')] public function testMissingHeaderRejectedByStrictServer(): void { $middleware = new ProtocolVersionMiddleware( supportedVersions: [ProtocolVersion::V2025_11_25], responseFactory: $this->factory, streamFactory: $this->factory, ); $request = $this->factory->createServerRequest('POST', 'http://localhost/'); $response = $middleware->process($request, $this->passthroughHandler); $this->assertSame(400, $response->getStatusCode()); } #[TestDox('accepts every handshake-era version by default')] public function testAcceptsSupportedVersions(): void { $middleware = new ProtocolVersionMiddleware(responseFactory: $this->factory, streamFactory: $this->factory); foreach (ProtocolVersion::handshakeVersions() as $version) { $request = $this->factory->createServerRequest('POST', 'http://localhost/') ->withHeader(StreamableHttpTransport::PROTOCOL_VERSION_HEADER, $version->value); $response = $middleware->process($request, $this->passthroughHandler); $this->assertSame(200, $response->getStatusCode(), 'Expected '.$version->value.' to be accepted.'); } } #[TestDox('rejects modern-era versions by default, since the server cannot serve them yet')] public function testRejectsModernVersionsByDefault(): void { $middleware = new ProtocolVersionMiddleware(responseFactory: $this->factory, streamFactory: $this->factory); foreach (ProtocolVersion::modernVersions() as $version) { $request = $this->factory->createServerRequest('POST', 'http://localhost/') ->withHeader(StreamableHttpTransport::PROTOCOL_VERSION_HEADER, $version->value); $response = $middleware->process($request, $this->passthroughHandler); $this->assertSame(400, $response->getStatusCode(), 'Expected '.$version->value.' to be rejected.'); } } #[TestDox('rejects unsupported well-formed version with 400')] public function testRejectsUnsupportedVersion(): void { $middleware = new ProtocolVersionMiddleware(responseFactory: $this->factory, streamFactory: $this->factory); $request = $this->factory->createServerRequest('POST', 'http://localhost/') ->withHeader(StreamableHttpTransport::PROTOCOL_VERSION_HEADER, '1900-01-01'); $response = $middleware->process($request, $this->passthroughHandler); $this->assertSame(400, $response->getStatusCode()); $this->assertSame('application/json', $response->getHeaderLine('Content-Type')); } #[TestDox('rejects malformed version with 400')] public function testRejectsMalformedVersion(): void { $middleware = new ProtocolVersionMiddleware(responseFactory: $this->factory, streamFactory: $this->factory); $request = $this->factory->createServerRequest('POST', 'http://localhost/') ->withHeader(StreamableHttpTransport::PROTOCOL_VERSION_HEADER, 'not-a-version'); $response = $middleware->process($request, $this->passthroughHandler); $this->assertSame(400, $response->getStatusCode()); } #[TestDox('accepts only the supportedVersions whitelist when provided')] public function testRestrictedSupportedVersions(): void { $middleware = new ProtocolVersionMiddleware( supportedVersions: [ProtocolVersion::V2025_11_25], responseFactory: $this->factory, streamFactory: $this->factory, ); $accepted = $this->factory->createServerRequest('POST', 'http://localhost/') ->withHeader(StreamableHttpTransport::PROTOCOL_VERSION_HEADER, ProtocolVersion::V2025_11_25->value); $rejected = $this->factory->createServerRequest('POST', 'http://localhost/') ->withHeader(StreamableHttpTransport::PROTOCOL_VERSION_HEADER, ProtocolVersion::V2024_11_05->value); $this->assertSame(200, $middleware->process($accepted, $this->passthroughHandler)->getStatusCode()); $this->assertSame(400, $middleware->process($rejected, $this->passthroughHandler)->getStatusCode()); } }