protected function setUp() { $this->markTestSkipped('Remove it when MAGETWO-33495 is done.'); $this->_response = $this->getMockBuilder('\\Magento\\Framework\\App\\Response\\Http')->setMethods(['sendHeaders'])->disableOriginalConstructor()->getMock(); $this->_request = $this->getMock('\\Magento\\Framework\\App\\Request\\Http', ['getHeader'], [], '', false); $header = \Zend\Http\Header\GenericHeader::fromString('User-Agent: Mozilla/5.0 FirePHP/1.6'); $this->_request->expects($this->any())->method('getHeader')->with('User-Agent')->will($this->returnValue($header)); $this->_output = new \Magento\Framework\Profiler\Driver\Standard\Output\Firebug(); $this->_output->setResponse($this->_response); $this->_output->setRequest($this->_request); }
public function testClearHeaderAndHeaderNotExists() { $response = $this->response = $this->getMock('Magento\\Framework\\HTTP\\PhpEnvironment\\Response', ['getHeaders', 'send']); $this->headers->addHeaderLine('Header-name: header-value'); $header = \Zend\Http\Header\GenericHeader::fromString('Header-name: header-value'); $this->headers->expects($this->once())->method('has')->with('Header-name')->will($this->returnValue(false)); $this->headers->expects($this->never())->method('get')->with('Header-name')->will($this->returnValue($header)); $this->headers->expects($this->never())->method('removeHeader')->with($header); $response->expects($this->once())->method('getHeaders')->will($this->returnValue($this->headers)); $response->clearHeader('Header-name'); }
/** * Create date-based header from string * * @param string $headerLine * @return AbstractDate * @throws Exception\InvalidArgumentException */ public static function fromString($headerLine) { $dateHeader = new static(); list($name, $date) = GenericHeader::splitHeaderLine($headerLine); // check to ensure proper header type for this factory if (strtolower($name) !== strtolower($dateHeader->getFieldName())) { throw new Exception\InvalidArgumentException('Invalid header line for "' . $dateHeader->getFieldName() . '" header string'); } $dateHeader->setDate($date); return $dateHeader; }
public function testSchemeFail() { $accept = new Accept(); $accept->addMediaType('application/json'); $this->getRequest()->setMethod(Request::METHOD_GET)->getHeaders()->addHeaders([$accept, GenericHeader::fromString('Authorization: Basic ' . base64_encode('toby:password1'))]); $this->dispatch('http://test.com/test'); $response = $this->getResponse(); $this->assertResponseStatusCode(403); $content = $response->getContent(); $this->assertEquals(0, strlen($content)); }
/** * Create location-based header from string * * @param string $headerLine * @return AbstractLocation * @throws Exception\InvalidArgumentException */ public static function fromString($headerLine) { $locationHeader = new static(); // ZF-5520 - IIS bug, no space after colon list($name, $uri) = GenericHeader::splitHeaderLine($headerLine); // check to ensure proper header type for this factory if (strtolower($name) !== strtolower($locationHeader->getFieldName())) { throw new Exception\InvalidArgumentException('Invalid header line for "' . $locationHeader->getFieldName() . '" header string'); } $locationHeader->setUri(trim($uri)); return $locationHeader; }
public function testSchemeFail() { $this->getRequest()->setMethod(Request::METHOD_GET)->getHeaders()->addHeader(GenericHeader::fromString('Authorization: Basic ' . base64_encode('toby:password'))); $this->dispatch('http://test.com/test'); $response = $this->getResponse(); $this->assertResponseStatusCode(403); $this->assertEquals('false', $response->getContent()); // $this->request->setUri('http://test.local'); // $this->request->getHeaders()->addHeader(GenericHeader::fromString('Authorization: Basic ' . base64_encode('toby:password'))); // // $this->assertFalse($this->authenticationService->hasIdentity()); }
/** * Create Allow header from header line * * @param string $headerLine * @return Allow * @throws Exception\InvalidArgumentException */ public static function fromString($headerLine) { list($name, $value) = GenericHeader::splitHeaderLine($headerLine); // check to ensure proper header type for this factory if (strtolower($name) !== 'allow') { throw new Exception\InvalidArgumentException('Invalid header line for Allow string: "' . $name . '"'); } $header = new static(); $header->disallowMethods(array_keys($header->getAllMethods())); $header->allowMethods(explode(',', $value)); return $header; }
/** * Parse a full header line or just the field value part. * * @param string $headerLine */ public function parseHeaderLine($headerLine) { if (strpos($headerLine, ':') !== false) { list($name, $value) = GenericHeader::splitHeaderLine($headerLine); if (strtolower($name) !== strtolower($this->getFieldName())) { $value = $headerLine; // This is just for preserve the BC. } } else { $value = $headerLine; } foreach ($this->getFieldValuePartsFromHeaderLine($value) as $value) { $this->addFieldValuePartToQueue($value); } }
/** * Create Allow header from header line * * @param string $headerLine * @return Allow * @throws Exception\InvalidArgumentException */ public static function fromString($headerLine) { $header = new static(); list($name, $value) = GenericHeader::splitHeaderLine($headerLine); // check to ensure proper header type for this factory if (strtolower($name) !== 'allow') { throw new Exception\InvalidArgumentException('Invalid header line for Allow string: "' . $name . '"'); } // reset list of methods $header->methods = array_fill_keys(array_keys($header->methods), false); // allow methods from header line foreach (explode(',', $value) as $method) { $method = trim(strtoupper($method)); $header->methods[$method] = true; } return $header; }
/** * @dataProvider dataProvider */ public function testAroundDispatchProcessIfCacheMissed($state) { $header = \Zend\Http\Header\GenericHeader::fromString('Cache-Control: no-cache'); $this->configMock->expects($this->once())->method('getType')->will($this->returnValue(\Magento\PageCache\Model\Config::BUILT_IN)); $this->configMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true)); $this->versionMock->expects($this->once())->method('process'); $this->kernelMock->expects($this->once())->method('load')->will($this->returnValue(false)); $this->stateMock->expects($this->any())->method('getMode')->will($this->returnValue($state)); if ($state == \Magento\Framework\App\State::MODE_DEVELOPER) { $this->responseMock->expects($this->at(1))->method('setHeader')->with('X-Magento-Cache-Control'); $this->responseMock->expects($this->at(2))->method('setHeader')->with('X-Magento-Cache-Debug', 'MISS', true); } else { $this->responseMock->expects($this->never())->method('setHeader'); } $this->responseMock->expects($this->once())->method('getHeader')->with('Cache-Control')->will($this->returnValue($header)); $this->kernelMock->expects($this->once())->method('process')->with($this->responseMock); $this->assertSame($this->responseMock, $this->plugin->aroundDispatch($this->frontControllerMock, $this->closure, $this->requestMock)); }
public static function fromString($headerLine) { $header = new static(); list($name, $value) = GenericHeader::splitHeaderLine($headerLine); // check to ensure proper header type for this factory if (strtolower($name) !== 'cookie') { throw new Exception\InvalidArgumentException('Invalid header line for Server string: "' . $name . '"'); } $nvPairs = preg_split('#;\\s*#', $value); $arrayInfo = array(); foreach ($nvPairs as $nvPair) { $parts = explode('=', $nvPair, 2); if (count($parts) != 2) { throw new Exception\RuntimeException('Malformed Cookie header found'); } list($name, $value) = $parts; $arrayInfo[$name] = urldecode($value); } $header->exchangeArray($arrayInfo); return $header; }
/** * Factory method: create an object from a string representation * * @param string $headerLine * @return self */ public static function fromString($headerLine) { list($name, $value) = GenericHeader::splitHeaderLine($headerLine); // check to ensure proper header type for this factory if (strtolower($name) !== 'content-type') { throw new Exception\InvalidArgumentException(sprintf('Invalid header line for Content-Type string: "%s"', $name)); } $parts = explode(';', $value); $mediaType = array_shift($parts); $header = new static($value, trim($mediaType)); if (count($parts) > 0) { $parameters = array(); foreach ($parts as $parameter) { $parameter = trim($parameter); if (!preg_match('/^(?P<key>[^\\s\\=]+)\\="?(?P<value>[^\\s\\"]*)"?$/', $parameter, $matches)) { continue; } $parameters[$matches['key']] = $matches['value']; } $header->setParameters($parameters); } return $header; }
public function testUpdateIdentityCredentialFail() { $this->setExpectedException('Sds\\IdentityModule\\Exception\\InvalidArgumentException'); $this->routeMatch->setParam('id', 'lucy'); $this->request->setMethod(Request::METHOD_PUT); $this->request->getHeaders()->addHeader(GenericHeader::fromString('Content-type: application/json')); $this->request->setContent('{ "credential":"password2" }'); $result = $this->getController()->dispatch($this->request, $this->response); $result->getVariables(); }
/** * @static * @throws Exception\InvalidArgumentException * @param $headerLine * @param bool $bypassHeaderFieldName * @return array|SetCookie */ public static function fromString($headerLine, $bypassHeaderFieldName = false) { static $setCookieProcessor = null; if ($setCookieProcessor === null) { $setCookieClass = get_called_class(); $setCookieProcessor = function ($headerLine) use($setCookieClass) { $header = new $setCookieClass(); $keyValuePairs = preg_split('#;\\s*#', $headerLine); foreach ($keyValuePairs as $keyValue) { if (preg_match('#^(?P<headerKey>[^=]+)=\\s*("?)(?P<headerValue>[^"]*)\\2#', $keyValue, $matches)) { $headerKey = $matches['headerKey']; $headerValue = $matches['headerValue']; } else { $headerKey = $keyValue; $headerValue = null; } // First K=V pair is always the cookie name and value if ($header->getName() === NULL) { $header->setName($headerKey); $header->setValue(urldecode($headerValue)); continue; } // Process the remaining elements switch (str_replace(array('-', '_'), '', strtolower($headerKey))) { case 'expires': $header->setExpires($headerValue); break; case 'domain': $header->setDomain($headerValue); break; case 'path': $header->setPath($headerValue); break; case 'secure': $header->setSecure(true); break; case 'httponly': $header->setHttponly(true); break; case 'version': $header->setVersion((int) $headerValue); break; case 'maxage': $header->setMaxAge((int) $headerValue); break; default: // Intentionally omitted } } return $header; }; } list($name, $value) = GenericHeader::splitHeaderLine($headerLine); // some sites return set-cookie::value, this is to get rid of the second : $name = strtolower($name) == 'set-cookie:' ? 'set-cookie' : $name; // check to ensure proper header type for this factory if (strtolower($name) !== 'set-cookie') { throw new Exception\InvalidArgumentException('Invalid header line for Set-Cookie string: "' . $name . '"'); } $multipleHeaders = preg_split('#(?<!Sun|Mon|Tue|Wed|Thu|Fri|Sat),\\s*#', $value); if (count($multipleHeaders) <= 1) { return $setCookieProcessor(array_pop($multipleHeaders)); } else { $headers = array(); foreach ($multipleHeaders as $headerLine) { $headers[] = $setCookieProcessor($headerLine); } return $headers; } }
protected function getSubRequest($batchRequest, array $requestData) { $request = new Request(); $request->setMethod($requestData['method']); $request->setUri($requestData['uri']); $queryString = $request->getUri()->getQuery(); if ($queryString) { $query = []; parse_str($queryString, $query); $request->setQuery(new Parameters($query)); } $requestHeaders = [$batchRequest->getHeaders()->get('Accept'), $batchRequest->getHeaders()->get('Content-Type')]; if (isset($requestData['headers'])) { foreach ($requestData['headers'] as $name => $value) { $requestHeaders[] = GenericHeader::fromString($name . ': ' . $value); } } $request->getHeaders()->addHeaders($requestHeaders); if (isset($requestData['content'])) { $request->setContent(json_encode($requestData['content'])); } return $request; }