/** * Iterate over all sets of data and test the rewriting * * @param string $testDataSet The dataset to test against * * @return boolean * @throws \Exception */ public function assertionEngine($testDataSet) { // Do we know this dataset? $this->assertArrayHasKey($testDataSet, $this->rewriteDataSets); // Get our dataset $dataSet = $this->rewriteDataSets[$testDataSet]; // We will get the rules into our module by ways of the volatile rewrites $this->mockRequestContext->setModuleVar(ModuleVars::VOLATILE_REWRITES, $dataSet['rules']); // No iterate over the map which is combined with the rules in the dataset foreach ($dataSet['map'] as $input => $desiredOutput) { // We will provide the crucial information by way of server vars $this->mockRequestContext->setServerVar(ServerVars::X_REQUEST_URI, $input); // Start the processing $this->rewriteModule->process($this->request, $this->response, $this->mockRequestContext, ModuleHooks::REQUEST_POST); // If we got a redirect we have to test differently if (isset($dataSet['redirect'])) { try { // Has the header location been set at all? // If we did not match any redirect condition and will set it to the input so we get some output if (!$this->response->hasHeader(Protocol::HEADER_LOCATION)) { $this->response->addHeader(Protocol::HEADER_LOCATION, $input); } // Asserting that the header location was set correctly $this->assertSame($desiredOutput, $this->response->getHeader(Protocol::HEADER_LOCATION)); // If we got a custom status code we have to check for it if (isset($dataSet['redirectAs'])) { $this->assertSame($dataSet['redirectAs'], (int) $this->response->getStatusCode()); } } catch (\Exception $e) { // Do not forget to reset the response object we are using!! $this->response = new HttpResponse(); $this->response->init(); // Re-throw the exception throw $e; } } else { // Now check if we got the same thing here $this->assertSame($desiredOutput, $this->mockRequestContext->getServerVar(ServerVars::X_REQUEST_URI)); } } // Still here? Then we are successful return true; }
/** * Inits the connection handler by given context and params * * @param \AppserverIo\Server\Interfaces\ServerContextInterface $serverContext The server's context * @param array $params The params for connection handler * * @return void */ public function init(ServerContextInterface $serverContext, array $params = null) { // set server context $this->serverContext = $serverContext; // set params $this->errorsPageTemplate = $params["errorsPageTemplate"]; // init http request object $httpRequest = new HttpRequest(); // init http response object $httpResponse = new HttpResponse(); // set default response headers $httpResponse->setDefaultHeaders(array(Protocol::HEADER_SERVER => $this->getServerConfig()->getSoftware(), Protocol::HEADER_CONNECTION => Protocol::HEADER_CONNECTION_VALUE_CLOSE)); // setup http parser $this->parser = new HttpRequestParser($httpRequest, $httpResponse); $this->parser->injectQueryParser(new HttpQueryParser()); $this->parser->injectPart(new HttpPart()); // setup request context // get request context type $requestContextType = $this->getServerConfig()->getRequestContextType(); /** * @var \AppserverIo\Server\Interfaces\RequestContextInterface $requestContext */ // instantiate and init request context $this->requestContext = new $requestContextType(); $this->requestContext->init($this->getServerConfig()); }