/** * This test launches a dummy Guzzle\Http\Server\Server object that listens * for incoming requests. The server allows us to test how cURL sends * requests and receives responses. We can validate the request structure * and whether or not the response was interpreted correctly. * * @covers Guzzle\Http\Message\Request */ public function testRequestCanBeSentUsingCurl() { $this->getServer()->flush(); $this->getServer()->enqueue(array("HTTP/1.1 200 OK\r\nContent-Length: 4\r\nExpires: Thu, 01 Dec 1994 16:00:00 GMT\r\nConnection: close\r\n\r\ndata", "HTTP/1.1 200 OK\r\nContent-Length: 4\r\nExpires: Thu, 01 Dec 1994 16:00:00 GMT\r\nConnection: close\r\n\r\ndata", "HTTP/1.1 404 Not Found\r\nContent-Encoding: application/xml\r\nContent-Length: 48\r\n\r\n<error><mesage>File not found</message></error>")); $request = RequestFactory::getInstance()->create('GET', $this->getServer()->getUrl()); $request->setClient($this->client); $response = $request->send(); $this->assertEquals('data', $response->getBody(true)); $this->assertEquals(200, (int) $response->getStatusCode()); $this->assertEquals('OK', $response->getReasonPhrase()); $this->assertEquals(4, $response->getContentLength()); $this->assertEquals('Thu, 01 Dec 1994 16:00:00 GMT', $response->getExpires()); // Test that the same handle can be sent twice without setting state to new $response2 = $request->send(); $this->assertNotSame($response, $response2); try { $request = RequestFactory::getInstance()->create('GET', $this->getServer()->getUrl() . 'index.html'); $request->setClient($this->client); $response = $request->send(); $this->fail('Request did not receive a 404 response'); } catch (BadResponseException $e) { } $requests = $this->getServer()->getReceivedRequests(true); $messages = $this->getServer()->getReceivedRequests(false); $port = $this->getServer()->getPort(); $userAgent = $this->client->getDefaultUserAgent(); $this->assertEquals('127.0.0.1:' . $port, $requests[0]->getHeader('Host')); $this->assertEquals('127.0.0.1:' . $port, $requests[1]->getHeader('Host')); $this->assertEquals('127.0.0.1:' . $port, $requests[2]->getHeader('Host')); $this->assertEquals('/', $requests[0]->getPath()); $this->assertEquals('/', $requests[1]->getPath()); $this->assertEquals('/index.html', $requests[2]->getPath()); $parts = explode("\r\n", $messages[0]); $this->assertEquals('GET / HTTP/1.1', $parts[0]); $parts = explode("\r\n", $messages[1]); $this->assertEquals('GET / HTTP/1.1', $parts[0]); $parts = explode("\r\n", $messages[2]); $this->assertEquals('GET /index.html HTTP/1.1', $parts[0]); }
/** * Data provider for factory tests * * @return array */ public function dataProvider() { $testFile = __DIR__ . '/../../../../../phpunit.xml.dist'; $postBody = new QueryString(array('file' => '@' . $testFile)); $qs = new QueryString(array('x' => 'y', 'z' => 'a')); $client = new Client(); $userAgent = $client->getDefaultUserAgent(); $auth = base64_encode('michael:123'); $testFileSize = filesize($testFile); $tests = array(array('GET', 'http://www.google.com/', null, null, array(CURLOPT_RETURNTRANSFER => 0, CURLOPT_HEADER => 0, CURLOPT_CONNECTTIMEOUT => 150, CURLOPT_WRITEFUNCTION => 'callback', CURLOPT_HEADERFUNCTION => 'callback', CURLOPT_HTTPHEADER => array('Accept:', 'Host: www.google.com', 'User-Agent: ' . $userAgent))), array('TRACE', 'http://www.google.com/', null, null, array(CURLOPT_CUSTOMREQUEST => 'TRACE')), array('GET', 'http://127.0.0.1:8080', null, null, array(CURLOPT_RETURNTRANSFER => 0, CURLOPT_HEADER => 0, CURLOPT_CONNECTTIMEOUT => 150, CURLOPT_WRITEFUNCTION => 'callback', CURLOPT_HEADERFUNCTION => 'callback', CURLOPT_PORT => 8080, CURLOPT_HTTPHEADER => array('Accept:', 'Host: 127.0.0.1:8080', 'User-Agent: ' . $userAgent))), array('HEAD', 'http://www.google.com/', null, null, array(CURLOPT_RETURNTRANSFER => 0, CURLOPT_HEADER => 0, CURLOPT_CONNECTTIMEOUT => 150, CURLOPT_HEADERFUNCTION => 'callback', CURLOPT_HTTPHEADER => array('Accept:', 'Host: www.google.com', 'User-Agent: ' . $userAgent), CURLOPT_NOBODY => 1)), array('GET', 'https://*****:*****@localhost/index.html?q=2', null, null, array(CURLOPT_RETURNTRANSFER => 0, CURLOPT_HEADER => 0, CURLOPT_CONNECTTIMEOUT => 150, CURLOPT_WRITEFUNCTION => 'callback', CURLOPT_HEADERFUNCTION => 'callback', CURLOPT_HTTPHEADER => array('Accept:', 'Host: localhost', 'Authorization: Basic ' . $auth, 'User-Agent: ' . $userAgent), CURLOPT_PORT => 443)), array('GET', 'http://*****:*****@' . $testFile . ';filename=phpunit.xml.dist;type=application/octet-stream'), CURLOPT_HTTPHEADER => array('Accept:', 'Host: localhost:8124', 'Content-Type: multipart/form-data', 'Expect: 100-Continue', 'User-Agent: ' . $userAgent)), array('Host' => '*', 'User-Agent' => '*', 'Content-Length' => '*', 'Expect' => '100-Continue', 'Content-Type' => 'multipart/form-data; boundary=*', '!Transfer-Encoding' => null)); if (version_compare(phpversion(), '5.5.0', '>=')) { $postTest[4][CURLOPT_POSTFIELDS] = array('file' => new \CurlFile($testFile, 'application/octet-stream', 'phpunit.xml.dist')); } $tests[] = $postTest; return $tests; }
public function getDefaultUserAgent() { return 'Cometdocs/' . self::VERSION . ' ' . parent::getDefaultUserAgent(); }