/** * Prepare cache-busting headers for GET requests * * Invoked from the onFinish() method for GET requests to disable client-side HTTP caching. * * @param Headers $headers */ protected function disableHttpCache(Headers $headers) { $headers->addHeader(new GenericHeader('Expires', '0')); $headers->addHeader(new GenericMultiHeader('Cache-Control', 'no-store, no-cache, must-revalidate')); $headers->addHeader(new GenericMultiHeader('Cache-Control', 'post-check=0, pre-check=0')); $headers->addHeaderLine('Pragma', 'no-cache'); }
/** * {@inheritdoc} */ public function sendRequest(RequestInterface $request) { $request = $this->sanitizeRequest($request); $headers = new Headers(); foreach ($request->getHeaders() as $key => $value) { $headers->addHeader(new GenericHeader($key, $request->getHeaderLine($key))); } $zendRequest = new Request(); $zendRequest->setMethod($request->getMethod()); $zendRequest->setUri((string) $request->getUri()); $zendRequest->setHeaders($headers); $zendRequest->setContent($request->getBody()->getContents()); $options = ['httpversion' => $request->getProtocolVersion()]; if (extension_loaded('curl')) { $options['curloptions'] = [CURLOPT_HTTP_VERSION => $this->getProtocolVersion($request->getProtocolVersion())]; } $this->client->setOptions($options); if ($this->client->getAdapter() instanceof ZendClient\Adapter\Curl && $request->getMethod()) { $request = $request->withHeader('Content-Length', '0'); } try { $zendResponse = $this->client->send($zendRequest); } catch (RuntimeException $exception) { throw new NetworkException($exception->getMessage(), $request, $exception); } return $this->responseFactory->createResponse($zendResponse->getStatusCode(), $zendResponse->getReasonPhrase(), $zendResponse->getHeaders()->toArray(), $zendResponse->getContent(), $zendResponse->getVersion()); }
public function testFromResponseInCookie() { $response = new Response(); $headers = new Headers(); $header = new SetCookie("foo", "bar"); $header->setDomain("www.zend.com"); $header->setPath("/"); $headers->addHeader($header); $response->setHeaders($headers); $response = Cookies::fromResponse($response, "http://www.zend.com"); $this->assertSame($header, $response->getCookie('http://www.zend.com', 'foo')); }
/** * @param string $collectionPath * @param \SimpleXMLElement $element * @return \SimpleXMLElement | null */ public function save($collectionPath, \SimpleXMLElement $element) { $uri = new Http($collectionPath); $request = new Request(); $request->setMethod('PUT'); $headers = new Headers(); $headers->addHeader(new GenericHeader('Content-Type', 'application/xml')); $request->setHeaders($headers); $request->setContent($element->asXML()); $response = $this->transport->put($uri, $request); $body = $response->getBody(); if ($response->isClientError() || empty($body)) { return null; } return new \SimpleXMLElement($body); }
public function testRetrievingASingleValueForParameters() { $request = new Request(); $p = new \Zend\Stdlib\Parameters(array('foo' => 'bar')); $request->setQuery($p); $request->setPost($p); $request->setFiles($p); $this->assertSame('bar', $request->getQuery('foo')); $this->assertSame('bar', $request->getPost('foo')); $this->assertSame('bar', $request->getFiles('foo')); $headers = new Headers(); $h = new GenericHeader('foo', 'bar'); $headers->addHeader($h); $request->setHeaders($headers); $this->assertSame($headers, $request->getHeaders()); $this->assertSame($h, $request->getHeaders()->get('foo')); $this->assertSame($h, $request->getHeader('foo')); }
public function testLogin() { $user_model = $this->getApplicationServiceLocator()->get('Core\\Model\\User'); $authentication = $this->getApplicationServiceLocator()->get('API\\Service\\AuthenticationService'); //1 - create user record $uid = md5(time() . rand()); $id = $user_model->insert(array('username' => $uid, 'password' => md5($uid), 'email' => $uid . '@yahoo.com', 'status' => 'active', 'default_role' => 'root')); //test if user was created $this->assertTrue((bool) $id, 'User not created for authentication tests.'); //2 - send login request - incorrect $response = $this->api('login', array('username' => $uid)); $this->assertArrayHasKey('error', $response); $this->assertArrayHasKey('password', (array) $response['error']); $response = (array) $response['error']; $response = (array) $response['password']; $this->assertArrayHasKey('isEmpty', (array) $response); //2 - send login request - correct $response = $this->api('login', array('username' => $uid, 'password' => $uid)); //test if login was successful $this->assertArrayHasKey('error', $response, 'API response do not returned "error" key.'); $this->assertFalse($response['error'], '"error" key should be false.'); $this->assertArrayHasKey('response', $response, 'API response do not returned "response" key.'); $token = (array) $response['response']; $this->assertArrayHasKey('$token', $token, '"token" not returned as a key.'); $token = $token['$token']; $this->assertEquals(32, strlen($token), 'Not a proper MD5 token.'); //3 - get storage and test it with login request data $storage = $authentication->getStorage()->read(); $this->assertEquals($id, $storage->id); $this->assertEquals($uid, (string) $storage->username); $this->assertEquals($token, (string) $storage->token); $this->assertEquals('active', (string) $storage->status); //32 - test session mehod $headers = new Headers(); $headers->addHeader(Authorization::fromString('Authorization: Token ' . $token)); $this->getRequest()->setHeaders($headers); $response = $this->api('session'); $this->assertArrayHasKey('error', $response); $this->assertFalse($response['error']); $this->assertArrayHasKey('response', $response); $this->assertArrayHasKey('$user', (array) $response['response']); $response = (array) $response['response']; $this->assertEquals($id, $response['$user']->id); $this->assertEquals($uid, (string) $response['$user']->username); //4 - logout - without authentication $this->reset(); $response = $this->api('logout'); $this->assertArrayHasKey('error', $response); $this->assertEquals($response['error'], 'authentication-required'); $this->assertArrayHasKey('response', $response); $this->assertNull($response['response']); //5 - logout - with authentication $this->reset(); $headers = new Headers(); //print_r($token); $headers->addHeader(Authorization::fromString('Authorization: Token ' . $token)); $this->getRequest()->setHeaders($headers); $response = $this->api('logout'); $this->assertArrayHasKey('error', $response); $this->assertFalse($response['error']); $this->assertArrayHasKey('response', $response); $this->assertTrue($response['response']); //6 - repeat previous request $response = $this->api('logout'); $this->assertArrayHasKey('error', $response); $this->assertEquals($response['error'], 'authentication-required'); $this->assertArrayHasKey('response', $response); $this->assertNull($response['response']); //delete the user created for testing $this->assertTrue((bool) $user_model->delete(array('id' => $id)), 'Testing user was not deleted.'); }
/** * Prepare cache-busting headers for GET requests * * Invoked from the onFinish() method for GET requests to disable client-side HTTP caching. * * @param \Zend\Http\Headers $headers */ protected function disableHttpCache($headers) { $headers->addHeader(new GenericHeader('Expires', '0')); // $headers->addHeaderLine('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT'); $headers->addHeader(new GenericMultiHeader('Cache-Control', 'no-store, no-cache, must-revalidate')); $headers->addHeader(new GenericMultiHeader('Cache-Control', 'post-check=0, pre-check=0')); $headers->addHeaderLine('Pragma', 'no-cache'); }
/** * @param Headers $headers * @return self */ public function setVary(Headers $headers) { if (!empty($this->cacheConfig['vary']['value']) && (!empty($this->cacheConfig['vary']['override']) || !$headers->has('vary'))) { $vary = new Header\Vary($this->cacheConfig['vary']['value']); $headers->addHeader($vary); } return $this; }
public function testHeadersAggregatesHeaderThroughAddHeader() { $headers = new Headers(); $headers->addHeader(new Header\GenericHeader('Fake', 'bar')); $this->assertEquals(1, $headers->count()); $this->assertInstanceOf('Zend\\Http\\Header\\GenericHeader', $headers->get('Fake')); }
public function testCastingToStringReturnsAllMultiHeaderValues() { $headers = new Headers(); $cookie1 = new Header\SetCookie('foo', 'bar'); $cookie2 = new Header\SetCookie('bar', 'baz'); $headers->addHeader($cookie1); $headers->addHeader($cookie2); $string = $headers->toString(); $expected = array('Set-Cookie: ' . $cookie1->getFieldValue(), 'Set-Cookie: ' . $cookie2->getFieldValue()); $expected = implode("\r\n", $expected) . "\r\n"; $this->assertEquals($expected, $string); }