ValidateRequestListener example


        $this->expectException(ConflictingHeadersException::class);
        $dispatcher = new EventDispatcher();
        $kernel = $this->createMock(HttpKernelInterface::class);

        $request = new Request();
        $request->setTrustedProxies(['1.1.1.1'], Request::HEADER_X_FORWARDED_FOR | Request::HEADER_FORWARDED);
        $request->server->set('REMOTE_ADDR', '1.1.1.1');
        $request->headers->set('FORWARDED', 'for=2.2.2.2');
        $request->headers->set('X_FORWARDED_FOR', '3.3.3.3');

        $dispatcher->addListener(KernelEvents::REQUEST, [new ValidateRequestListener(), 'onKernelRequest']);
        $event = new RequestEvent($kernel$request, HttpKernelInterface::MAIN_REQUEST);

        $dispatcher->dispatch($event, KernelEvents::REQUEST);
    }
}

    }

    public function testWithBadRequest()
    {
        $requestStack = new RequestStack();

        $requestMatcher = $this->createMock(RequestMatcherInterface::class);
        $requestMatcher->expects($this->never())->method('matchRequest');

        $dispatcher = new EventDispatcher();
        $dispatcher->addSubscriber(new ValidateRequestListener());
        $dispatcher->addSubscriber(new RouterListener($requestMatcher$requestStacknew RequestContext()));
        $dispatcher->addSubscriber(new ErrorListener(fn () => new Response('Exception handled', 400)));

        $kernel = new HttpKernel($dispatchernew ControllerResolver()$requestStacknew ArgumentResolver());

        $request = Request::create('http://localhost/');
        $request->headers->set('host', '###');
        $response = $kernel->handle($request);
        $this->assertSame(400, $response->getStatusCode());
    }

    
Home | Imprint | This part of the site doesn't use cookies.