Example #1
0
 /**
  * @covers ::createPOST
  * @covers ::_addHeaders
  * @covers ::_addMultipartFields
  * @covers ::_addUrlEncodedFields
  */
 public function test_createPOST()
 {
     # Valid arguments
     $URI = new GenericURI('http://example.com');
     $Image = new GenericFile(dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR . '1x1.png');
     $Data = array('int' => 1, 'float' => 1.1, 'string' => 'foo', 'array' => array(1, 2), 'object1' => new \stdClass(), 'object2' => new \SplFileInfo(sys_get_temp_dir()), 'file' => $Image, 'FormField' => new FormField('field', 'text/plain', 'foo'), 'FormFile' => new FormFile('FormFile', $Image));
     # File multipart/form-data
     $Request = $this->Factory->createPOST($URI, $URI, $Data, $this->Headers);
     $File = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR . 'request-multipart.form-data.txt';
     $Boundary = preg_match('!boundary\\s*=\\s*"(.*)"!', $Request->Header['Content-Type'], $m) ? $m[1] : '';
     $Expected = sprintf(preg_replace('!\\r*\\n!', "\r\n", file_get_contents($File)), $Boundary, sys_get_temp_dir(), file_get_contents($Image));
     // file_put_contents($File, str_replace(array(sys_get_temp_dir(), file_get_contents($Image), $Boundary), array('%2$s', '%3$s', '%1$s'), strval($Request)));
     $this->assertEquals($URI, $Request->URI, 'IFactory::createPOST() Failed to set request URI');
     $this->assertEquals(IRequest::POST, $Request->Type, 'IFactory::createPOST() Failed to set request Type');
     $this->assertEquals($Expected, strval($Request), 'IFactory::createPOST() Produced an invalid request');
     # No file application/x-www-form-urlencoded
     unset($Data['file']);
     unset($Data['FormFile']);
     $Request = $this->Factory->createPOST($URI, $URI, $Data, $this->Headers);
     $File = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR . 'request-application.x-www-form-urlencoded.txt';
     $Boundary = preg_match('!boundary\\s*=\\s*"(.*)"!', $Request->Header['Content-Type'], $m) ? $m[1] : '';
     $Expected = file_get_contents($File);
     // file_put_contents($File, str_replace($Boundary, '%1$s', strval($Request)));
     # Invalid Arguments
     for ($args = $this->generateInvalidPOST(); list($k, list($URI, $BaseURI, $Data, $Headers)) = each($args);) {
         try {
             $this->Factory->createPOST($URI, $BaseURI, $Data, $Headers);
             $this->fail('Failed to generate exception with invalid arguments');
         } catch (InvalidArgumentException $e) {
         }
     }
 }
Example #2
0
 /**
  * @covers ::translate
  * @covers ::_translateHeaders
  * @covers ::_translateURI
  * @covers ::_translateReferer
  */
 public function test_translate()
 {
     $Expected = array(CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CONNECTTIMEOUT => 30, CURLOPT_MAXREDIRS => 10, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_HTTPHEADER => array('Accept: text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8', 'Accept-Language: en-us, en;q=0.5', 'Cache-Control: max-age=0', 'Content-Type: application/x-www-form-urlencoded'), CURLOPT_ENCODING => '', CURLOPT_AUTOREFERER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_SSL_VERIFYPEER => 1, CURLOPT_SSL_VERIFYHOST => 2, CURLOPT_URL => 'http://example.com', CURLOPT_PORT => 1000, CURLOPT_USERPWD => 'user:pass', CURLOPT_REFERER => 'about:nothing', CURLOPT_USERAGENT => 'Mozilla/5.0 (Linux; Android 4.3; Nexus 7 Build/JSS15Q) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.72 Safari/537.36', CURLOPT_POSTFIELDS => 'foo=bar', CURLOPT_COOKIEFILE => $this->Client->createCookieFile()->getPathname(), CURLOPT_COOKIEJAR => $this->Client->createCookieFile()->getPathname());
     $Factory = new RequestFactory();
     $Browser = new Nexus($this->Client, new Config(array('MaxHistory' => 10)));
     $Request = $Factory->createPOST(new GenericURI('http://*****:*****@example.com:1000'), new GenericURI('about:nothing'), array('foo' => 'bar'), $Browser->createHeaders());
     $this->Client->send($Request);
     $this->assertEquals($Expected, $this->Client->translate($Request), 'cURL::translate() Returned an invalid value');
     unset($Request->Referer);
     $Request->getHeader()->offsetSet('Referer', new Referer(new GenericURI('about:nothing')));
     $this->assertEquals($Expected, $this->Client->translate($Request), 'cURL::translate() Returned an invalid value');
     $Request->getHeader()->offsetUnset('Referer');
     unset($Expected[CURLOPT_REFERER]);
     $this->assertEquals($Expected, $this->Client->translate($Request), 'cURL::translate() Returned an invalid value');
     # Inalid request
     $this->assertSame(array(), $this->Client->translate($Factory->createGET(new GenericURI('about:nothing'), new GenericURI('about:nothing'))), 'cURL::translate() Returned an invalid value');
 }