Creates a new Request object from the given data.
public static create ( |
||
$uri | The request URI | |
$method | string | Request method, for example "GET" |
$arguments | array | Arguments to send in the request body |
$files | array | |
$server | array | |
return |
/** * @return void */ public function setUp() { $this->viewHelperVariableContainer = $this->createMock(\TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer::class); $this->viewHelperVariableContainer->expects($this->any())->method('exists')->will($this->returnCallback(array($this, 'viewHelperVariableContainerExistsCallback'))); $this->viewHelperVariableContainer->expects($this->any())->method('get')->will($this->returnCallback(array($this, 'viewHelperVariableContainerGetCallback'))); $this->templateVariableContainer = $this->createMock(TemplateVariableContainer::class); $this->uriBuilder = $this->createMock(\Neos\Flow\Mvc\Routing\UriBuilder::class); $this->uriBuilder->expects($this->any())->method('reset')->will($this->returnValue($this->uriBuilder)); $this->uriBuilder->expects($this->any())->method('setArguments')->will($this->returnValue($this->uriBuilder)); $this->uriBuilder->expects($this->any())->method('setSection')->will($this->returnValue($this->uriBuilder)); $this->uriBuilder->expects($this->any())->method('setFormat')->will($this->returnValue($this->uriBuilder)); $this->uriBuilder->expects($this->any())->method('setCreateAbsoluteUri')->will($this->returnValue($this->uriBuilder)); $this->uriBuilder->expects($this->any())->method('setAddQueryString')->will($this->returnValue($this->uriBuilder)); $this->uriBuilder->expects($this->any())->method('setArgumentsToBeExcludedFromQueryString')->will($this->returnValue($this->uriBuilder)); // BACKPORTER TOKEN #1 $httpRequest = \Neos\Flow\Http\Request::create(new \Neos\Flow\Http\Uri('http://localhost/foo')); $this->request = $this->getMockBuilder(\Neos\Flow\Mvc\ActionRequest::class)->setConstructorArgs(array($httpRequest))->getMock(); $this->request->expects($this->any())->method('isMainRequest')->will($this->returnValue(true)); $this->controllerContext = $this->getMockBuilder(\Neos\Flow\Mvc\Controller\ControllerContext::class)->disableOriginalConstructor()->getMock(); $this->controllerContext->expects($this->any())->method('getUriBuilder')->will($this->returnValue($this->uriBuilder)); $this->controllerContext->expects($this->any())->method('getRequest')->will($this->returnValue($this->request)); $this->tagBuilder = $this->createMock(TagBuilder::class); $this->arguments = array(); $this->renderingContext = new \Neos\FluidAdaptor\Core\Rendering\RenderingContext(new StandaloneView(), []); $this->renderingContext->setVariableProvider($this->templateVariableContainer); $this->renderingContext->setViewHelperVariableContainer($this->viewHelperVariableContainer); $this->renderingContext->setControllerContext($this->controllerContext); }
/** * @test */ public function updateCredentialsSetsTheCorrectAuthenticationStatusIfNoCredentialsArrived() { $httpRequest = Request::create(new Uri('http://foo.com')); $mockActionRequest = $this->getMockBuilder(ActionRequest::class)->disableOriginalConstructor()->getMock(); $mockActionRequest->expects($this->atLeastOnce())->method('getHttpRequest')->will($this->returnValue($httpRequest)); $this->token->updateCredentials($mockActionRequest); $this->assertSame(TokenInterface::NO_CREDENTIALS_GIVEN, $this->token->getAuthenticationStatus()); }
/** * Create a mock controllerContext * * @return \PHPUnit_Framework_MockObject_MockObject */ protected function getMockControllerContext() { $httpRequest = Request::create(new Uri('http://robertlemke.com/blog')); $mockRequest = $this->getMockBuilder(\Neos\Flow\Mvc\ActionRequest::class)->setConstructorArgs(array($httpRequest))->getMock(); $mockRequest->expects($this->any())->method('getControllerPackageKey')->will($this->returnValue('Acme.Demo')); $mockControllerContext = $this->getMockBuilder(\Neos\Flow\Mvc\Controller\ControllerContext::class)->setMethods(array('getRequest'))->disableOriginalConstructor()->getMock(); $mockControllerContext->expects($this->any())->method('getRequest')->will($this->returnValue($mockRequest)); return $mockControllerContext; }
/** * @test */ public function actionRequestDoesNotStripParentActionRequest() { $httpRequest = Request::create(new Uri('http://neos.io')); $parentActionRequest = new ActionRequest($httpRequest); $actionRequest = new ActionRequest($parentActionRequest); $serializedActionRequest = serialize($actionRequest); /* @var $unserializedActionRequest ActionRequest */ $unserializedActionRequest = unserialize($serializedActionRequest); $this->assertNotNull($unserializedActionRequest->getParentRequest(), 'Parent action request should not be NULL after deserialization'); }
/** * @test */ public function renderEscapesBaseUri() { $baseUri = new Uri('<some nasty uri>'); $this->request->expects($this->any())->method('getHttpRequest')->will($this->returnValue(Request::create($baseUri))); $viewHelper = new BaseViewHelper(); $this->injectDependenciesIntoViewHelper($viewHelper); $expectedResult = '<base href="http://' . htmlspecialchars($baseUri) . '/" />'; $actualResult = $viewHelper->render(); $this->assertSame($expectedResult, $actualResult); }
/** * @test */ public function initializeControllerInitializesRequestUriBuilderArgumentsAndContext() { $request = new ActionRequest(Request::create(new Uri('http://localhost/foo'))); $controller = $this->getAccessibleMock(AbstractController::class, ['processRequest']); $this->inject($controller, 'flashMessageContainer', new FlashMessageContainer()); $this->assertFalse($request->isDispatched()); $controller->_call('initializeController', $request, $this->mockHttpResponse); $this->assertTrue($request->isDispatched()); $this->assertInstanceOf(Arguments::class, $controller->_get('arguments')); $this->assertSame($request, $controller->_get('uriBuilder')->getRequest()); $this->assertSame($request, $controller->getControllerContext()->getRequest()); }
/** * @test */ public function startAuthenticationSetsTheCorrectValuesInTheResponseObjectIfRouteValuesAreSpecified() { $request = Request::create(new Uri('http://robertlemke.com/admin')); $response = new Response(); $entryPoint = $this->getAccessibleMock(WebRedirect::class, ['dummy']); $routeValues = ['@package' => 'SomePackage', '@subpackage' => 'SomeSubPackage', '@controller' => 'SomeController', '@action' => 'someAction', '@format' => 'someFormat', 'otherArguments' => ['foo' => 'bar']]; $entryPoint->setOptions(['routeValues' => $routeValues]); $mockUriBuilder = $this->createMock(UriBuilder::class); $mockUriBuilder->expects($this->once())->method('setCreateAbsoluteUri')->with(true)->will($this->returnValue($mockUriBuilder)); $mockUriBuilder->expects($this->once())->method('uriFor')->with('someAction', ['otherArguments' => ['foo' => 'bar'], '@format' => 'someFormat'], 'SomeController', 'SomePackage', 'SomeSubPackage')->will($this->returnValue('http://resolved/redirect/uri')); $entryPoint->_set('uriBuilder', $mockUriBuilder); $entryPoint->startAuthentication($request, $response); $this->assertEquals('303', substr($response->getStatus(), 0, 3)); $this->assertEquals('http://resolved/redirect/uri', $response->getHeader('Location')); }
/** * @return void */ public function setUp() { parent::setup(); vfsStream::setup('Foo'); $this->httpRequest = Http\Request::create(new Http\Uri('http://localhost')); $this->httpResponse = new Http\Response(); $mockRequestHandler = $this->createMock(Http\RequestHandler::class); $mockRequestHandler->expects($this->any())->method('getHttpRequest')->will($this->returnValue($this->httpRequest)); $mockRequestHandler->expects($this->any())->method('getHttpResponse')->will($this->returnValue($this->httpResponse)); $this->mockBootstrap = $this->createMock(Bootstrap::class); $this->mockBootstrap->expects($this->any())->method('getActiveRequestHandler')->will($this->returnValue($mockRequestHandler)); $this->mockSecurityContext = $this->createMock(Context::class); $this->mockObjectManager = $this->createMock(ObjectManagerInterface::class); $this->mockObjectManager->expects($this->any())->method('get')->with(Context::class)->will($this->returnValue($this->mockSecurityContext)); }
/** * Helper to build mock controller context needed to test expandGenericPathPattern. * * @param string $packageKey * @param string $subPackageKey * @param string $controllerName * @param string $format * @return ControllerContext */ protected function setupMockControllerContextForPathResolving($packageKey, $subPackageKey, $controllerName, $format) { $controllerObjectName = 'TYPO3\\' . $packageKey . '\\' . ($subPackageKey != $subPackageKey . '\\' ?: '') . 'Controller\\' . $controllerName . 'Controller'; $httpRequest = Request::create(new Uri('http://robertlemke.com/blog')); $mockRequest = $this->createMock(\Neos\Flow\Mvc\ActionRequest::class, array(), array($httpRequest)); $mockRequest->expects($this->any())->method('getControllerPackageKey')->will($this->returnValue($packageKey)); $mockRequest->expects($this->any())->method('getControllerSubPackageKey')->will($this->returnValue($subPackageKey)); $mockRequest->expects($this->any())->method('getControllerName')->will($this->returnValue($controllerName)); $mockRequest->expects($this->any())->method('getControllerObjectName')->will($this->returnValue($controllerObjectName)); $mockRequest->expects($this->any())->method('getFormat')->will($this->returnValue($format)); /** @var $mockControllerContext ControllerContext */ $mockControllerContext = $this->createMock(\Neos\Flow\Mvc\Controller\ControllerContext::class, array('getRequest'), array(), '', false); $mockControllerContext->expects($this->any())->method('getRequest')->will($this->returnValue($mockRequest)); return $mockControllerContext; }
/** * @test */ public function processRequestShouldSetWidgetConfiguration() { /** @var \Neos\Flow\Mvc\ActionRequest $mockActionRequest */ $mockActionRequest = $this->createMock(\Neos\Flow\Mvc\ActionRequest::class); $mockResponse = $this->createMock(\Neos\Flow\Http\Response::class); $httpRequest = Request::create(new Uri('http://localhost')); $mockActionRequest->expects($this->any())->method('getHttpRequest')->will($this->returnValue($httpRequest)); $expectedWidgetConfiguration = array('foo' => uniqid()); $widgetContext = new WidgetContext(); $widgetContext->setAjaxWidgetConfiguration($expectedWidgetConfiguration); $mockActionRequest->expects($this->atLeastOnce())->method('getInternalArgument')->with('__widgetContext')->will($this->returnValue($widgetContext)); $abstractWidgetController = $this->getAccessibleMock(\Neos\FluidAdaptor\Core\Widget\AbstractWidgetController::class, array('resolveActionMethodName', 'initializeActionMethodArguments', 'initializeActionMethodValidators', 'mapRequestArgumentsToControllerArguments', 'detectFormat', 'resolveView', 'callActionMethod')); $abstractWidgetController->_set('mvcPropertyMappingConfigurationService', $this->createMock(\Neos\Flow\Mvc\Controller\MvcPropertyMappingConfigurationService::class)); $abstractWidgetController->processRequest($mockActionRequest, $mockResponse); $actualWidgetConfiguration = $abstractWidgetController->_get('widgetConfiguration'); $this->assertEquals($expectedWidgetConfiguration, $actualWidgetConfiguration); }
/** * @test */ public function postRequestOnRestrictedActionWithoutCsrfTokenCausesAccessDeniedException() { $this->markTestIncomplete('Needs to be implemented'); $arguments = []; $arguments['__authentication']['TYPO3']['Flow']['Security']['Authentication']['Token']['UsernamePassword']['username'] = '******'; $arguments['__authentication']['TYPO3']['Flow']['Security']['Authentication']['Token']['UsernamePassword']['password'] = '******'; $request = Request::create(new Uri('http://localhost/test/security/authentication/usernamepassword/authenticate'), 'POST', $arguments); $response = $this->browser->sendRequest($request); $sessionCookie = $response->getCookie('TYPO3_Flow_Session'); $request = Request::create(new Uri('http://localhost/test/security/restricted/admin')); $request->setCookie($sessionCookie); $response = $this->browser->sendRequest($request); // Expect an exception because no account is authenticated: $response = $this->browser->request(new Uri('http://localhost/test/security/restricted/customer'), 'POST'); // ... // Expect an different exception because although an account is authenticated, the request lacks a CSRF token: $response = $this->browser->request(new Uri('http://localhost/test/security/restricted/customer'), 'POST', $arguments); // ... // Expect that it works after you logged in $csrfToken = $this->securityContext->getCsrfProtectionToken(); $request = Request::create(new Uri('http://localhost/test/security/restricted/customer'), 'POST'); // ... }
/** * @test */ public function trustedPropertiesConfigurationDoesNotIgnoreWildcardConfigurationInController() { $entity = new TestEntity(); $entity->setName('Foo'); $this->persistenceManager->add($entity); $identifier = $this->persistenceManager->getIdentifierByObject($entity); $trustedPropertiesService = new MvcPropertyMappingConfigurationService(); $trustedProperties = $trustedPropertiesService->generateTrustedPropertiesToken(['entity[__identity]', 'entity[subEntities][0][content]', 'entity[subEntities][0][date]', 'entity[subEntities][1][content]', 'entity[subEntities][1][date]']); $form = ['entity' => ['__identity' => $identifier, 'subEntities' => [['content' => 'Bar', 'date' => '1.1.2016'], ['content' => 'Baz', 'date' => '30.12.2016']]], '__trustedProperties' => $trustedProperties]; $request = Request::create(new Uri('http://localhost/test/mvc/actioncontrollertestc/' . $identifier . '/update'), 'POST', $form); $response = $this->browser->sendRequest($request); $this->assertSame('Entity "Foo" updated', $response->getContent()); }
/** * @param string $requestUri request URI * @param string $expectedMatchingRouteName expected route * @param string $expectedControllerObjectName expected controller object name * @param array $expectedArguments expected request arguments after routing or NULL if this should not be checked * @test * @dataProvider routeTestsDataProvider */ public function routeTests($requestUri, $expectedMatchingRouteName, $expectedControllerObjectName = null, array $expectedArguments = null) { $request = Request::create(new Uri($requestUri)); $matchResults = $this->router->route($request); $actionRequest = $this->createActionRequest($request, $matchResults); $matchedRoute = $this->router->getLastMatchedRoute(); if ($expectedMatchingRouteName === null) { if ($matchedRoute !== null) { $this->fail('Expected no route to match URI "' . $requestUri . '" but route "' . $matchedRoute->getName() . '" matched'); } } else { if ($matchedRoute === null) { $this->fail('Expected route "' . $expectedMatchingRouteName . '" to match, but no route matched request URI "' . $requestUri . '"'); } else { $this->assertEquals('Flow :: Functional Test: ' . $expectedMatchingRouteName, $matchedRoute->getName()); } } $this->assertEquals($expectedControllerObjectName, $actionRequest->getControllerObjectName()); if ($expectedArguments !== null) { $this->assertEquals($expectedArguments, $actionRequest->getArguments()); } }
/** * RFC 2616 / 14.28 (If-Unmodified-Since) * * @test */ public function makeStandardsCompliantReturns412StatusIfUnmodifiedSinceDoesNotMatch() { $request = Request::create(new Uri('http://localhost')); $response = new Response(); $unmodifiedSince = \DateTime::createFromFormat(DATE_RFC2822, 'Tue, 15 May 2012 09:00:00 GMT'); $lastModified = \DateTime::createFromFormat(DATE_RFC2822, 'Sun, 20 May 2012 08:00:00 UTC'); $request->setHeader('If-Unmodified-Since', $unmodifiedSince); $response->setHeader('Last-Modified', $lastModified); $response->makeStandardsCompliant($request); $this->assertSame(412, $response->getStatusCode()); $response = new Response(); $unmodifiedSince = \DateTime::createFromFormat(DATE_RFC2822, 'Tue, 15 May 2012 09:00:00 GMT'); $lastModified = \DateTime::createFromFormat(DATE_RFC2822, 'Tue, 15 May 2012 08:00:00 UTC'); $request->setHeader('If-Unmodified-Since', $unmodifiedSince); $response->setHeader('Last-Modified', $lastModified); $response->makeStandardsCompliant($request); $this->assertSame(200, $response->getStatusCode()); }
/** * @test */ public function successfulAuthenticationDoesNotStartASessionIfNoTokenRequiresIt() { $uri = new Uri('http://localhost/test/security/authentication/httpbasic'); $request = Request::create($uri); $request->setHeader('Authorization', 'Basic ' . base64_encode('functional_test_account:a_very_secure_long_password')); $response = $this->browser->sendRequest($request); $this->assertEmpty($response->getCookies()); }
/** * Requests the given URI with the method and other parameters as specified. * If a Location header was given and the status code is of response type 3xx * (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html, 14.30 Location) * * @param string|Uri $uri * @param string $method Request method, for example "GET" * @param array $arguments Arguments to send in the request body * @param array $files * @param array $server * @param string $content * @return Response The HTTP response * @throws \InvalidArgumentException * @throws InfiniteRedirectionException * @api */ public function request($uri, $method = 'GET', array $arguments = [], array $files = [], array $server = [], $content = null) { if (is_string($uri)) { $uri = new Uri($uri); } if (!$uri instanceof Uri) { throw new \InvalidArgumentException('$uri must be a URI object or a valid string representation of a URI.', 1333443624); } $request = Request::create($uri, $method, $arguments, $files, $server); if ($content !== null) { $request->setContent($content); } $response = $this->sendRequest($request); $location = $response->getHeader('Location'); if ($this->followRedirects && $location !== null && $response->getStatusCode() >= 300 && $response->getStatusCode() <= 399) { if (in_array($location, $this->redirectionStack) || count($this->redirectionStack) >= $this->maximumRedirections) { throw new InfiniteRedirectionException('The Location "' . $location . '" to follow for a redirect will probably result into an infinite loop.', 1350391699); } $this->redirectionStack[] = $location; return $this->request($location); } $this->redirectionStack = []; return $response; }
/** * @test * @dataProvider forwardHeaderTestsDataProvider */ public function forwardHeaderTests($forwardedProtocol, $forwardedPort, $requestUri, $expectedUri) { $server = array(); if ($forwardedProtocol !== null) { $server['HTTP_X_FORWARDED_PROTO'] = $forwardedProtocol; } if ($forwardedPort !== null) { $server['HTTP_X_FORWARDED_PORT'] = $forwardedPort; } $request = Request::create(new Uri($requestUri), 'GET', array(), array(), $server); $trustedRequest = $this->callWithRequest($request); $this->assertEquals($expectedUri, (string) $trustedRequest->getUri()); }
/** * Tests the wrong interceptor behavior described in ticket FLOW-430 * Basically the rendering should be consistent regardless of cache flushes, * but due to the way the interceptor configuration was build the second second * rendering was bound to fail, this should never happen. * * @test */ public function interceptorsWorkInPartialRenderedInStandaloneSection() { $httpRequest = Request::create(new Uri('http://localhost')); $actionRequest = new ActionRequest($httpRequest); $actionRequest->setFormat('html'); $standaloneView = new StandaloneView($actionRequest, $this->standaloneViewNonce); $standaloneView->assign('hack', '<h1>HACK</h1>'); $standaloneView->setTemplatePathAndFilename(__DIR__ . '/Fixtures/NestedRenderingConfiguration/TemplateWithSection.txt'); $expected = 'Christian uses <h1>HACK</h1>'; $actual = trim($standaloneView->renderSection('test')); $this->assertSame($expected, $actual, 'First rendering was not escaped.'); $partialCacheIdentifier = $standaloneView->getTemplatePaths()->getPartialIdentifier('Test'); $templateCache = $this->objectManager->get(CacheManager::class)->getCache('Fluid_TemplateCache'); $templateCache->remove($partialCacheIdentifier); $standaloneView = new StandaloneView($actionRequest, $this->standaloneViewNonce); $standaloneView->assign('hack', '<h1>HACK</h1>'); $standaloneView->setTemplatePathAndFilename(__DIR__ . '/Fixtures/NestedRenderingConfiguration/TemplateWithSection.txt'); $expected = 'Christian uses <h1>HACK</h1>'; $actual = trim($standaloneView->renderSection('test')); $this->assertSame($expected, $actual, 'Second rendering was not escaped.'); }
/** * @test */ public function getRelativePathReturnsEmptyStringForHomepage() { $request = Request::create(new Uri('http://dev.blog.rob/'), 'GET'); $relativePath = $request->getRelativePath(); $this->assertSame($relativePath, ''); }
/** * Sets up a virtual browser and web environment for seamless HTTP and MVC * related tests. * * @return void */ protected function setupHttp() { $this->browser = new \Neos\Flow\Http\Client\Browser(); $this->browser->setRequestEngine(new \Neos\Flow\Http\Client\InternalRequestEngine()); $this->router = $this->browser->getRequestEngine()->getRouter(); $this->router->setRoutesConfiguration(null); $requestHandler = self::$bootstrap->getActiveRequestHandler(); $request = Request::create(new \Neos\Flow\Http\Uri('http://localhost/typo3/flow/test')); $componentContext = new ComponentContext($request, new \Neos\Flow\Http\Response()); $requestHandler->setComponentContext($componentContext); }
/** * @test */ public function matchRequestReturnsFalseIfAuthorizationChecksAreDisabled() { $httpRequest = Request::create(new Uri('http://localhost'), 'POST'); $this->mockActionRequest->expects($this->any())->method('getHttpRequest')->will($this->returnValue($httpRequest)); $mockAuthenticationManager = $this->getMockBuilder(AuthenticationManagerInterface::class)->disableOriginalConstructor()->getMock(); $mockAuthenticationManager->expects($this->any())->method('isAuthenticated')->will($this->returnValue(true)); $mockSecurityContext = $this->createMock(Security\Context::class); $mockSecurityContext->expects($this->atLeastOnce())->method('areAuthorizationChecksDisabled')->will($this->returnValue(true)); $mockCsrfProtectionPattern = $this->getAccessibleMock(Security\RequestPattern\CsrfProtection::class, ['dummy']); $mockCsrfProtectionPattern->_set('authenticationManager', $mockAuthenticationManager); $mockCsrfProtectionPattern->_set('systemLogger', $this->mockSystemLogger); $mockCsrfProtectionPattern->_set('securityContext', $mockSecurityContext); $this->assertFalse($mockCsrfProtectionPattern->matchRequest($this->mockActionRequest)); }
/** * @return ControllerContext */ protected function buildMockControllerContext() { $httpRequest = Request::create(new Uri('http://foo.bar/bazfoo')); $request = new ActionRequest($httpRequest); $response = new Response(); /** @var Arguments $mockArguments */ $mockArguments = $this->getMockBuilder(Arguments::class)->disableOriginalConstructor()->getMock(); $uriBuilder = new UriBuilder(); $controllerContext = new ControllerContext($request, $response, $mockArguments, $uriBuilder, $this->createMock(FlashMessageContainer::class)); return $controllerContext; }
/** * @dataProvider uriAndHostPatterns * @test */ public function requestMatchingBasicallyWorks($uri, $pattern, $expected, $message) { $request = Request::create(new Uri($uri))->createActionRequest(); $requestPattern = new Host(['hostPattern' => $pattern]); $this->assertEquals($expected, $requestPattern->matchRequest($request), $message); }