Пример #1
0
 /**
  * @covers Guzzle\Http\Message\Request::changedHeader
  * @covers Guzzle\Http\Message\Request::setHeader
  */
 public function testCatchesAllHostHeaderChanges()
 {
     // Tests setting using headers
     $this->request->setHeader('Host', 'www.abc.com');
     $this->assertEquals('www.abc.com', $this->request->getHost());
     $this->assertEquals('www.abc.com:8124', $this->request->getHeader('Host'));
     $this->assertEquals(8124, $this->request->getPort());
     // Tests setting using setHost()
     $this->request->setHost('abc.com');
     $this->assertEquals('abc.com', $this->request->getHost());
     $this->assertEquals('abc.com:8124', $this->request->getHeader('Host'));
     $this->assertEquals(8124, $this->request->getPort());
     // Tests setting with a port
     $this->request->setHost('abc.com:8081');
     $this->assertEquals('abc.com', $this->request->getHost());
     $this->assertEquals('abc.com:8081', $this->request->getHeader('Host'));
     $this->assertEquals(8081, $this->request->getPort());
     // Tests setting with a port using the Host header
     $this->request->setHeader('Host', 'solr.com:8983');
     $this->assertEquals('solr.com', $this->request->getHost());
     $this->assertEquals('solr.com:8983', (string) $this->request->getHeader('Host'));
     $this->assertEquals(8983, $this->request->getPort());
     // Tests setting with an inferred 443 port using the Host header
     $this->request->setScheme('https');
     $this->request->setHeader('Host', 'solr.com');
     $this->assertEquals('solr.com', $this->request->getHost());
     $this->assertEquals('solr.com:8983', (string) $this->request->getHeader('Host'));
     $this->assertEquals(8983, $this->request->getPort());
 }
Пример #2
0
 public function testClonedRequestsUseNewInternalState()
 {
     $p = new AsyncPlugin();
     $this->request->getEventDispatcher()->addSubscriber($p);
     $h = $this->request->getHeader('Host');
     $r = clone $this->request;
     $this->assertEquals(RequestInterface::STATE_NEW, $r->getState());
     $this->assertNotSame($r->getQuery(), $this->request->getQuery());
     $this->assertNotSame($r->getCurlOptions(), $this->request->getCurlOptions());
     $this->assertNotSame($r->getEventDispatcher(), $this->request->getEventDispatcher());
     $this->assertEquals($r->getHeaders(), $this->request->getHeaders());
     $this->assertNotSame($h, $r->getHeader('Host'));
     $this->assertNotSame($r->getParams(), $this->request->getParams());
     $this->assertTrue($this->request->getEventDispatcher()->hasListeners('request.sent'));
 }
Пример #3
0
 public function getHeader($name)
 {
     return $this->_request->getHeader($name);
 }
Пример #4
0
 /**
  * @dataProvider satisfiableOnErrorProvider
  */
 public function testInjectsSatisfiableResponsesOnException($cacheResponse)
 {
     $storage = $this->getMockBuilder('Guzzle\\Plugin\\Cache\\CacheStorageInterface')->setMethods(array('fetch'))->getMockForAbstractClass();
     $storage->expects($this->exactly(2))->method('fetch')->will($this->returnValue($cacheResponse));
     $plugin = new CachePlugin(array('storage' => $storage));
     $request = new Request('GET', 'http://foo.com', array('Cache-Control' => 'max-stale'));
     $plugin->onRequestBeforeSend(new Event(array('request' => $request)));
     $plugin->onRequestException(new Event(array('request' => $request, 'response' => $request->getResponse(), 'exception' => $this->getMock('Guzzle\\Http\\Exception\\CurlException'))));
     $plugin->onRequestSent(new Event(array('request' => $request, 'response' => $response = $request->getResponse())));
     $this->assertEquals($cacheResponse->getStatusCode(), $response->getStatusCode());
     $this->assertEquals((string) $cacheResponse->getBody(), (string) $response->getBody());
     $this->assertTrue($response->hasHeader('Age'));
     if ($response->isFresh() === false) {
         $this->assertContains('110', (string) $response->getHeader('Warning'));
     }
     $this->assertSame(sprintf('%s GuzzleCache/%s', $request->getProtocolVersion(), Version::VERSION), (string) $request->getHeader('Via'));
     $this->assertSame(sprintf('%s GuzzleCache/%s', $request->getProtocolVersion(), Version::VERSION), (string) $response->getHeader('Via'));
     $this->assertTrue($request->getParams()->get('cache.lookup'));
     $this->assertSame('error', $request->getParams()->get('cache.hit'));
     $this->assertTrue($response->hasHeader('X-Cache-Lookup'));
     $this->assertTrue($response->hasHeader('X-Cache'));
     $this->assertEquals('HIT from GuzzleCache', (string) $response->getHeader('X-Cache-Lookup'));
     $this->assertEquals('HIT_ERROR from GuzzleCache', (string) $response->getHeader('X-Cache'));
 }
Пример #5
0
 /**
  * {@inheritDoc}
  */
 public function getHeader($header)
 {
     return (string) $this->request->getHeader($header);
 }
Пример #6
0
 protected function generateRequest($url, array $subProtocols, array $headers)
 {
     $headers = array_merge($this->defaultHeaders, $headers);
     $headers['Sec-WebSocket-Key'] = $this->generateKey();
     $request = new Request('GET', $url, $headers);
     $scheme = strtolower($request->getScheme());
     if (!in_array($scheme, ['ws', 'wss'])) {
         throw new \InvalidArgumentException(sprintf('Cannot connect to invalid URL (%s)', $url));
     }
     $request->setScheme('HTTP');
     if (!$request->getPort()) {
         $request->setPort('wss' === $scheme ? 443 : 80);
     } else {
         $request->setHeader('Host', $request->getHeader('Host') . ":{$request->getPort()}");
     }
     if (!$request->getHeader('Origin')) {
         $request->setHeader('Origin', str_replace('ws', 'http', $scheme) . '://' . $request->getHost());
     }
     // do protocol headers
     if (count($subProtocols) > 0) {
         $protocols = implode(',', $subProtocols);
         if ($protocols != "") {
             $request->setHeader('Sec-WebSocket-Protocol', $protocols);
         }
     }
     return $request;
 }