CommonHttpTests.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. <?php
  2. // Read local configuration
  3. if (! defined('TESTS_ZEND_HTTP_CLIENT_BASEURI') &&
  4. is_readable('TestConfiguration.php')) {
  5. require_once 'TestConfiguration.php';
  6. }
  7. require_once realpath(dirname(__FILE__) . '/../../../') . '/TestHelper.php';
  8. require_once 'Zend/Http/Client.php';
  9. require_once 'Zend/Uri/Http.php';
  10. /**
  11. * This Testsuite includes all Zend_Http_Client that require a working web
  12. * server to perform. It was designed to be extendable, so that several
  13. * test suites could be run against several servers, with different client
  14. * adapters and configurations.
  15. *
  16. * Note that $this->baseuri must point to a directory on a web server
  17. * containing all the files under the _files directory. You should symlink
  18. * or copy these files and set 'baseuri' properly.
  19. *
  20. * You can also set the proper constant in your test configuration file to
  21. * point to the right place.
  22. *
  23. * @category Zend
  24. * @package Zend_Http_Client
  25. * @subpackage UnitTests
  26. * @version $Id$
  27. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. */
  30. abstract class Zend_Http_Client_CommonHttpTests extends PHPUnit_Framework_TestCase
  31. {
  32. /**
  33. * The bast URI for this test, containing all files in the _files directory
  34. * Should be set in TestConfiguration.php or TestConfiguration.php.dist
  35. *
  36. * @var string
  37. */
  38. protected $baseuri;
  39. /**
  40. * Common HTTP client
  41. *
  42. * @var Zend_Http_Client
  43. */
  44. protected $client = null;
  45. /**
  46. * Common HTTP client adapter
  47. *
  48. * @var Zend_Http_Client_Adapter_Interface
  49. */
  50. protected $_adapter = null;
  51. /**
  52. * Configuration array
  53. *
  54. * @var array
  55. */
  56. protected $config = array(
  57. 'adapter' => 'Zend_Http_Client_Adapter_Socket'
  58. );
  59. /**
  60. * Set up the test case
  61. *
  62. */
  63. protected function setUp()
  64. {
  65. if (defined('TESTS_ZEND_HTTP_CLIENT_BASEURI') &&
  66. Zend_Uri_Http::check(TESTS_ZEND_HTTP_CLIENT_BASEURI)) {
  67. $this->baseuri = TESTS_ZEND_HTTP_CLIENT_BASEURI;
  68. if (substr($this->baseuri, -1) != '/') $this->baseuri .= '/';
  69. $name = $this->getName();
  70. if (($pos = strpos($name, ' ')) !== false) {
  71. $name = substr($name, 0, $pos);
  72. }
  73. $uri = $this->baseuri . $name . '.php';
  74. $this->_adapter = new $this->config['adapter'];
  75. $this->client = new Zend_Http_Client($uri, $this->config);
  76. $this->client->setAdapter($this->_adapter);
  77. } else {
  78. // Skip tests
  79. $this->markTestSkipped("Zend_Http_Client dynamic tests are not enabled in TestConfiguration.php");
  80. }
  81. }
  82. /**
  83. * Clean up the test environment
  84. *
  85. */
  86. protected function tearDown()
  87. {
  88. $this->client = null;
  89. $this->_adapter = null;
  90. }
  91. /**
  92. * Simple request tests
  93. */
  94. /**
  95. * Test simple requests
  96. *
  97. */
  98. public function testSimpleRequests()
  99. {
  100. $methods = array('GET', 'POST', 'OPTIONS', 'PUT', 'DELETE');
  101. foreach ($methods as $method) {
  102. $res = $this->client->request($method);
  103. $this->assertEquals('Success', $res->getBody(), "HTTP {$method} request failed.");
  104. }
  105. }
  106. /**
  107. * Test we can get the last request as string
  108. *
  109. */
  110. public function testGetLastRequest()
  111. {
  112. $this->client->setUri($this->baseuri . 'testHeaders.php');
  113. $this->client->setParameterGet('someinput', 'somevalue');
  114. $this->client->setHeaders(array(
  115. 'X-Powered-By' => 'My Glorious Golden Ass',
  116. ));
  117. $res = $this->client->request(Zend_Http_Client::TRACE);
  118. if ($res->getStatus() == 405 || $res->getStatus() == 501) {
  119. $this->markTestSkipped("Server does not allow the TRACE method");
  120. }
  121. $this->assertEquals($this->client->getLastRequest(), $res->getBody(), 'Response body should be exactly like the last request');
  122. }
  123. /**
  124. * GET and POST parameters tests
  125. */
  126. /**
  127. * Test we can properly send GET parameters
  128. *
  129. * @dataProvider parameterArrayProvider
  130. */
  131. public function testGetData($params)
  132. {
  133. $this->client->setUri($this->client->getUri(true) . '?name=Arthur');
  134. $this->client->setParameterGet($params);
  135. $res = $this->client->request('GET');
  136. $this->assertEquals(serialize(array_merge(array('name' => 'Arthur'), $params)), $res->getBody());
  137. }
  138. /**
  139. * Test we can properly send POST parameters with
  140. * application/x-www-form-urlencoded content type
  141. *
  142. * @dataProvider parameterArrayProvider
  143. */
  144. public function testPostDataUrlEncoded($params)
  145. {
  146. $this->client->setUri($this->baseuri . 'testPostData.php');
  147. $this->client->setEncType(Zend_Http_Client::ENC_URLENCODED);
  148. $this->client->setParameterPost($params);
  149. $res = $this->client->request('POST');
  150. $this->assertEquals(serialize($params), $res->getBody(), "POST data integrity test failed");
  151. }
  152. /**
  153. * Test we can properly send POST parameters with
  154. * multipart/form-data content type
  155. *
  156. * @dataProvider parameterArrayProvider
  157. */
  158. public function testPostDataMultipart($params)
  159. {
  160. $this->client->setUri($this->baseuri . 'testPostData.php');
  161. $this->client->setEncType(Zend_Http_Client::ENC_FORMDATA);
  162. $this->client->setParameterPost($params);
  163. $res = $this->client->request('POST');
  164. $this->assertEquals(serialize($params), $res->getBody(), "POST data integrity test failed");
  165. }
  166. /**
  167. * Test using raw HTTP POST data
  168. *
  169. */
  170. public function testRawPostData()
  171. {
  172. $data = "Chuck Norris never wet his bed as a child. The bed wet itself out of fear.";
  173. $res = $this->client->setRawData($data, 'text/html')->request('POST');
  174. $this->assertEquals($data, $res->getBody(), 'Response body does not contain the expected data');
  175. }
  176. /**
  177. * Make sure we can reset the parameters between consecutive requests
  178. *
  179. */
  180. public function testResetParameters()
  181. {
  182. $params = array(
  183. 'quest' => 'To seek the holy grail',
  184. 'YourMother' => 'Was a hamster',
  185. 'specialChars' => '<>$+ &?=[]^%',
  186. 'array' => array('firstItem', 'secondItem', '3rdItem')
  187. );
  188. $this->client->setParameterPost($params);
  189. $this->client->setParameterGet($params);
  190. $res = $this->client->request('POST');
  191. $this->assertContains(serialize($params) . "\n" . serialize($params),
  192. $res->getBody(), "returned body does not contain all GET and POST parameters (it should!)");
  193. $this->client->resetParameters();
  194. $res = $this->client->request('POST');
  195. $this->assertNotContains(serialize($params), $res->getBody(),
  196. "returned body contains GET or POST parameters (it shouldn't!)");
  197. }
  198. /**
  199. * Test parameters get reset when we unset them
  200. *
  201. */
  202. public function testParameterUnset()
  203. {
  204. $this->client->setUri($this->baseuri . 'testResetParameters.php');
  205. $gparams = array (
  206. 'cheese' => 'camambert',
  207. 'beer' => 'jever pilnsen',
  208. );
  209. $pparams = array (
  210. 'from' => 'bob',
  211. 'to' => 'alice'
  212. );
  213. $this->client->setParameterGet($gparams)->setParameterPost($pparams);
  214. // Remove some parameters
  215. $this->client->setParameterGet('cheese', null)->setParameterPost('to', null);
  216. $res = $this->client->request('POST');
  217. $this->assertNotContains('cheese', $res->getBody(), 'The "cheese" GET parameter was expected to be unset');
  218. $this->assertNotContains('alice', $res->getBody(), 'The "to" POST parameter was expected to be unset');
  219. }
  220. /**
  221. * Header Tests
  222. */
  223. /**
  224. * Make sure we can set a single header
  225. *
  226. */
  227. public function testHeadersSingle()
  228. {
  229. $this->client->setUri($this->baseuri . 'testHeaders.php');
  230. $headers = array(
  231. 'Accept-encoding' => 'gzip,deflate',
  232. 'X-baz' => 'Foo',
  233. 'X-powered-by' => 'A large wooden badger'
  234. );
  235. foreach ($headers as $key => $val) {
  236. $this->client->setHeaders($key, $val);
  237. }
  238. $acceptHeader = "Accept: text/xml,text/html,*/*";
  239. $this->client->setHeaders($acceptHeader);
  240. $res = $this->client->request('TRACE');
  241. if ($res->getStatus() == 405 || $res->getStatus() == 501) {
  242. $this->markTestSkipped("Server does not allow the TRACE method");
  243. }
  244. $body = strtolower($res->getBody());
  245. foreach ($headers as $key => $val)
  246. $this->assertContains(strtolower("$key: $val"), $body);
  247. $this->assertContains(strtolower($acceptHeader), $body);
  248. }
  249. /**
  250. * Test we can set an array of headers
  251. *
  252. */
  253. public function testHeadersArray()
  254. {
  255. $this->client->setUri($this->baseuri . 'testHeaders.php');
  256. $headers = array(
  257. 'Accept-encoding' => 'gzip,deflate',
  258. 'X-baz' => 'Foo',
  259. 'X-powered-by' => 'A large wooden badger',
  260. 'Accept: text/xml,text/html,*/*'
  261. );
  262. $this->client->setHeaders($headers);
  263. $res = $this->client->request('TRACE');
  264. if ($res->getStatus() == 405 || $res->getStatus() == 501) {
  265. $this->markTestSkipped("Server does not allow the TRACE method");
  266. }
  267. $body = strtolower($res->getBody());
  268. foreach ($headers as $key => $val) {
  269. if (is_string($key)) {
  270. $this->assertContains(strtolower("$key: $val"), $body);
  271. } else {
  272. $this->assertContains(strtolower($val), $body);
  273. }
  274. }
  275. }
  276. /**
  277. * Test we can set a set of values for one header
  278. *
  279. */
  280. public function testMultipleHeader()
  281. {
  282. $this->client->setUri($this->baseuri . 'testHeaders.php');
  283. $headers = array(
  284. 'Accept-encoding' => 'gzip,deflate',
  285. 'X-baz' => 'Foo',
  286. 'X-powered-by' => array(
  287. 'A large wooden badger',
  288. 'My Shiny Metal Ass',
  289. 'Dark Matter'
  290. ),
  291. 'Cookie' => array(
  292. 'foo=bar',
  293. 'baz=waka'
  294. )
  295. );
  296. $this->client->setHeaders($headers);
  297. $res = $this->client->request('TRACE');
  298. if ($res->getStatus() == 405 || $res->getStatus() == 501) {
  299. $this->markTestSkipped("Server does not allow the TRACE method");
  300. }
  301. $body = strtolower($res->getBody());
  302. foreach ($headers as $key => $val) {
  303. if (is_array($val))
  304. $val = implode(', ', $val);
  305. $this->assertContains(strtolower("$key: $val"), $body);
  306. }
  307. }
  308. /**
  309. * Redirection tests
  310. */
  311. /**
  312. * Test the client properly redirects in default mode
  313. *
  314. */
  315. public function testRedirectDefault()
  316. {
  317. $this->client->setUri($this->baseuri . 'testRedirections.php');
  318. // Set some parameters
  319. $this->client->setParameterGet('swallow', 'african');
  320. $this->client->setParameterPost('Camelot', 'A silly place');
  321. // Request
  322. $res = $this->client->request('POST');
  323. $this->assertEquals(3, $this->client->getRedirectionsCount(), 'Redirection counter is not as expected');
  324. // Make sure the body does *not* contain the set parameters
  325. $this->assertNotContains('swallow', $res->getBody());
  326. $this->assertNotContains('Camelot', $res->getBody());
  327. }
  328. /**
  329. * Make sure the client properly redirects in strict mode
  330. *
  331. */
  332. public function testRedirectStrict()
  333. {
  334. $this->client->setUri($this->baseuri . 'testRedirections.php');
  335. // Set some parameters
  336. $this->client->setParameterGet('swallow', 'african');
  337. $this->client->setParameterPost('Camelot', 'A silly place');
  338. // Set strict redirections
  339. $this->client->setConfig(array('strictredirects' => true));
  340. // Request
  341. $res = $this->client->request('POST');
  342. $this->assertEquals(3, $this->client->getRedirectionsCount(), 'Redirection counter is not as expected');
  343. // Make sure the body *does* contain the set parameters
  344. $this->assertContains('swallow', $res->getBody());
  345. $this->assertContains('Camelot', $res->getBody());
  346. }
  347. /**
  348. * Make sure redirections stop when limit is exceeded
  349. *
  350. */
  351. public function testMaxRedirectsExceeded()
  352. {
  353. $this->client->setUri($this->baseuri . 'testRedirections.php');
  354. // Set some parameters
  355. $this->client->setParameterGet('swallow', 'african');
  356. $this->client->setParameterPost('Camelot', 'A silly place');
  357. // Set lower max redirections
  358. // Try with strict redirections first
  359. $this->client->setConfig(array('strictredirects' => true, 'maxredirects' => 2));
  360. $res = $this->client->request('POST');
  361. $this->assertTrue($res->isRedirect(),
  362. "Last response was not a redirection as expected. Response code: {$res->getStatus()}. Redirections counter: {$this->client->getRedirectionsCount()} (when strict redirects are on)");
  363. // Then try with normal redirections
  364. $this->client->setParameterGet('redirection', '0');
  365. $this->client->setConfig(array('strictredirects' => false));
  366. $res = $this->client->request('POST');
  367. $this->assertTrue($res->isRedirect(),
  368. "Last response was not a redirection as expected. Response code: {$res->getStatus()}. Redirections counter: {$this->client->getRedirectionsCount()} (when strict redirects are off)");
  369. }
  370. /**
  371. * Test we can properly redirect to an absolute path (not full URI)
  372. *
  373. */
  374. public function testAbsolutePathRedirect()
  375. {
  376. $this->client->setUri($this->baseuri . 'testRelativeRedirections.php');
  377. $this->client->setParameterGet('redirect', 'abpath');
  378. $this->client->setConfig(array('maxredirects' => 1));
  379. // Get the host and port part of our baseuri
  380. $uri = $this->client->getUri()->getScheme() . '://' . $this->client->getUri()->getHost() . ':' .
  381. $this->client->getUri()->getPort();
  382. $res = $this->client->request('GET');
  383. $this->assertEquals("{$uri}/path/to/fake/file.ext?redirect=abpath", $this->client->getUri(true),
  384. "The new location is not as expected: {$this->client->getUri(true)}");
  385. }
  386. /**
  387. * Test we can properly redirect to a relative path
  388. *
  389. */
  390. public function testRelativePathRedirect()
  391. {
  392. $this->client->setUri($this->baseuri . 'testRelativeRedirections.php');
  393. $this->client->setParameterGet('redirect', 'relpath');
  394. $this->client->setConfig(array('maxredirects' => 1));
  395. // Set the new expected URI
  396. $uri = clone $this->client->getUri();
  397. $uri->setPath(dirname($uri->getPath()) . '/path/to/fake/file.ext');
  398. $uri = $uri->__toString();
  399. $res = $this->client->request('GET');
  400. $this->assertEquals("{$uri}?redirect=relpath", $this->client->getUri(true),
  401. "The new location is not as expected: {$this->client->getUri(true)}");
  402. }
  403. /**
  404. * HTTP Authentication Tests
  405. *
  406. */
  407. /**
  408. * Test we can properly use Basic HTTP authentication
  409. *
  410. */
  411. public function testHttpAuthBasic()
  412. {
  413. $this->client->setUri($this->baseuri. 'testHttpAuth.php');
  414. $this->client->setParameterGet(array(
  415. 'user' => 'alice',
  416. 'pass' => 'secret',
  417. 'method' => 'Basic'
  418. ));
  419. // First - fail password
  420. $this->client->setAuth('alice', 'wrong');
  421. $res = $this->client->request();
  422. $this->assertEquals(401, $res->getStatus(), 'Expected HTTP 401 response was not recieved');
  423. // Now use good password
  424. $this->client->setAuth('alice', 'secret');
  425. $res = $this->client->request();
  426. $this->assertEquals(200, $res->getStatus(), 'Expected HTTP 200 response was not recieved');
  427. }
  428. /**
  429. * Test we can unset HTTP authentication
  430. *
  431. */
  432. public function testCancelAuth()
  433. {
  434. $this->client->setUri($this->baseuri. 'testHttpAuth.php');
  435. // Set auth and cancel it
  436. $this->client->setAuth('alice', 'secret');
  437. $this->client->setAuth(false);
  438. $res = $this->client->request();
  439. $this->assertEquals(401, $res->getStatus(), 'Expected HTTP 401 response was not recieved');
  440. $this->assertNotContains('alice', $res->getBody(), "Body contains the user name, but it shouldn't");
  441. $this->assertNotContains('secret', $res->getBody(), "Body contains the password, but it shouldn't");
  442. }
  443. /**
  444. * Cookie and CookieJar Tests
  445. *
  446. */
  447. /**
  448. * Test we can set string cookies with no jar
  449. *
  450. */
  451. public function testCookiesStringNoJar()
  452. {
  453. $this->client->setUri($this->baseuri. 'testCookies.php');
  454. $cookies = array(
  455. 'name' => 'value',
  456. 'cookie' => 'crumble'
  457. );
  458. foreach ($cookies as $k => $v) {
  459. $this->client->setCookie($k, $v);
  460. }
  461. $res = $this->client->request();
  462. $this->assertEquals($res->getBody(), serialize($cookies), 'Response body does not contain the expected cookies');
  463. }
  464. /**
  465. * Make sure we can set object cookies with no jar
  466. *
  467. */
  468. public function testSetCookieObjectNoJar()
  469. {
  470. $this->client->setUri($this->baseuri. 'testCookies.php');
  471. $refuri = $this->client->getUri();
  472. $cookies = array(
  473. Zend_Http_Cookie::fromString('chocolate=chips', $refuri),
  474. Zend_Http_Cookie::fromString('crumble=apple', $refuri)
  475. );
  476. $strcookies = array();
  477. foreach ($cookies as $c) {
  478. $this->client->setCookie($c);
  479. $strcookies[$c->getName()] = $c->getValue();
  480. }
  481. $res = $this->client->request();
  482. $this->assertEquals($res->getBody(), serialize($strcookies), 'Response body does not contain the expected cookies');
  483. }
  484. /**
  485. * Make sure we can set an array of object cookies
  486. *
  487. */
  488. public function testSetCookieObjectArray()
  489. {
  490. $this->client->setUri($this->baseuri. 'testCookies.php');
  491. $refuri = $this->client->getUri();
  492. $cookies = array(
  493. Zend_Http_Cookie::fromString('chocolate=chips', $refuri),
  494. Zend_Http_Cookie::fromString('crumble=apple', $refuri),
  495. Zend_Http_Cookie::fromString('another=cookie', $refuri)
  496. );
  497. $this->client->setCookie($cookies);
  498. $strcookies = array();
  499. foreach ($cookies as $c) {
  500. $strcookies[$c->getName()] = $c->getValue();
  501. }
  502. $res = $this->client->request();
  503. $this->assertEquals($res->getBody(), serialize($strcookies), 'Response body does not contain the expected cookies');
  504. }
  505. /**
  506. * Make sure we can set an array of string cookies
  507. *
  508. */
  509. public function testSetCookieStringArray()
  510. {
  511. $this->client->setUri($this->baseuri. 'testCookies.php');
  512. $cookies = array(
  513. 'chocolate' => 'chips',
  514. 'crumble' => 'apple',
  515. 'another' => 'cookie'
  516. );
  517. $this->client->setCookie($cookies);
  518. $res = $this->client->request();
  519. $this->assertEquals($res->getBody(), serialize($cookies), 'Response body does not contain the expected cookies');
  520. }
  521. /**
  522. * Make sure we can set cookie objects with a jar
  523. *
  524. */
  525. public function testSetCookieObjectJar()
  526. {
  527. $this->client->setUri($this->baseuri. 'testCookies.php');
  528. $this->client->setCookieJar();
  529. $refuri = $this->client->getUri();
  530. $cookies = array(
  531. Zend_Http_Cookie::fromString('chocolate=chips', $refuri),
  532. Zend_Http_Cookie::fromString('crumble=apple', $refuri)
  533. );
  534. $strcookies = array();
  535. foreach ($cookies as $c) {
  536. $this->client->setCookie($c);
  537. $strcookies[$c->getName()] = $c->getValue();
  538. }
  539. $res = $this->client->request();
  540. $this->assertEquals($res->getBody(), serialize($strcookies), 'Response body does not contain the expected cookies');
  541. }
  542. /**
  543. * File Upload Tests
  544. *
  545. */
  546. /**
  547. * Test we can upload raw data as a file
  548. *
  549. */
  550. public function testUploadRawData()
  551. {
  552. $this->client->setUri($this->baseuri. 'testUploads.php');
  553. $rawdata = file_get_contents(__FILE__);
  554. $this->client->setFileUpload('myfile.txt', 'uploadfile', $rawdata, 'text/plain');
  555. $res = $this->client->request('POST');
  556. $body = 'uploadfile myfile.txt text/plain ' . strlen($rawdata) . "\n";
  557. $this->assertEquals($body, $res->getBody(), 'Response body does not include expected upload parameters');
  558. }
  559. /**
  560. * Test we can upload an existing file
  561. *
  562. */
  563. public function testUploadLocalFile()
  564. {
  565. $this->client->setUri($this->baseuri. 'testUploads.php');
  566. $this->client->setFileUpload(__FILE__, 'uploadfile', null, 'text/x-foo-bar');
  567. $res = $this->client->request('POST');
  568. $size = filesize(__FILE__);
  569. $body = "uploadfile " . basename(__FILE__) . " text/x-foo-bar $size\n";
  570. $this->assertEquals($body, $res->getBody(), 'Response body does not include expected upload parameters');
  571. }
  572. public function testUploadLocalDetectMime()
  573. {
  574. $detect = null;
  575. if (function_exists('finfo_file')) {
  576. $f = @finfo_open(FILEINFO_MIME);
  577. if ($f) $detect = 'finfo';
  578. } elseif (function_exists('mime_content_type')) {
  579. if (mime_content_type(__FILE__)) {
  580. $detect = 'mime_magic';
  581. }
  582. }
  583. if (! $detect) {
  584. $this->markTestSkipped('No MIME type detection capability (fileinfo or mime_magic extensions) is available');
  585. }
  586. $file = dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'staticFile.jpg';
  587. $this->client->setUri($this->baseuri. 'testUploads.php');
  588. $this->client->setFileUpload($file, 'uploadfile');
  589. $res = $this->client->request('POST');
  590. $size = filesize($file);
  591. $body = "uploadfile " . basename($file) . " image/jpeg $size\n";
  592. $this->assertEquals($body, $res->getBody(), 'Response body does not include expected upload parameters (detect: ' . $detect . ')');
  593. }
  594. public function testUploadNameWithSpecialChars()
  595. {
  596. $this->client->setUri($this->baseuri. 'testUploads.php');
  597. $rawdata = file_get_contents(__FILE__);
  598. $this->client->setFileUpload('/some strage/path%/with[!@#$&]/myfile.txt', 'uploadfile', $rawdata, 'text/plain');
  599. $res = $this->client->request('POST');
  600. $body = 'uploadfile myfile.txt text/plain ' . strlen($rawdata) . "\n";
  601. $this->assertEquals($body, $res->getBody(), 'Response body does not include expected upload parameters');
  602. }
  603. public function testStaticLargeFileDownload()
  604. {
  605. $this->client->setUri($this->baseuri . 'staticFile.jpg');
  606. $got = $this->client->request()->getBody();
  607. $expected = $this->_getTestFileContents('staticFile.jpg');
  608. $this->assertEquals($expected, $got, 'Downloaded file does not seem to match!');
  609. }
  610. /**
  611. * Test that one can upload multiple files with the same form name, as an
  612. * array
  613. *
  614. * @link http://framework.zend.com/issues/browse/ZF-5744
  615. */
  616. public function testMutipleFilesWithSameFormNameZF5744()
  617. {
  618. $rawData = 'Some test raw data here...';
  619. $this->client->setUri($this->baseuri . 'testUploads.php');
  620. $files = array('file1.txt', 'file2.txt', 'someotherfile.foo');
  621. $expectedBody = '';
  622. foreach($files as $filename) {
  623. $this->client->setFileUpload($filename, 'uploadfile[]', $rawData, 'text/plain');
  624. $expectedBody .= "uploadfile $filename text/plain " . strlen($rawData) . "\n";
  625. }
  626. $res = $this->client->request('POST');
  627. $this->assertEquals($expectedBody, $res->getBody(), 'Response body does not include expected upload parameters');
  628. }
  629. /**
  630. * Test that lines that might be evaluated as boolean false do not break
  631. * the reading prematurely.
  632. *
  633. * @see http://framework.zend.com/issues/browse/ZF-4238
  634. */
  635. public function testZF4238FalseLinesInResponse()
  636. {
  637. $this->client->setUri($this->baseuri . 'ZF4238-zerolineresponse.txt');
  638. $got = $this->client->request()->getBody();
  639. $expected = $this->_getTestFileContents('ZF4238-zerolineresponse.txt');
  640. $this->assertEquals($expected, $got);
  641. }
  642. /**
  643. * Internal helpder function to get the contents of test files
  644. *
  645. * @param string $file
  646. * @return string
  647. */
  648. protected function _getTestFileContents($file)
  649. {
  650. return file_get_contents(dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR .
  651. '_files' . DIRECTORY_SEPARATOR . $file);
  652. }
  653. /**
  654. * Data provider for complex, nesting parameter arrays
  655. *
  656. * @return array
  657. */
  658. static public function parameterArrayProvider()
  659. {
  660. return array(
  661. array(
  662. array(
  663. 'quest' => 'To seek the holy grail',
  664. 'YourMother' => 'Was a hamster',
  665. 'specialChars' => '<>$+ &?=[]^%',
  666. 'array' => array('firstItem', 'secondItem', '3rdItem')
  667. )
  668. ),
  669. array(
  670. array(
  671. 'someData' => array(
  672. "1",
  673. "2",
  674. 'key' => 'value',
  675. 'nesting' => array(
  676. 'a' => 'AAA',
  677. 'b' => 'BBB'
  678. )
  679. ),
  680. 'someOtherData' => array('foo', 'bar')
  681. )
  682. ),
  683. array(
  684. array(
  685. 'foo1' => 'bar',
  686. 'foo2' => array('baz', 'w00t')
  687. )
  688. )
  689. );
  690. }
  691. /**
  692. * Data provider for invalid configuration containers
  693. *
  694. * @return array
  695. */
  696. static public function invalidConfigProvider()
  697. {
  698. return array(
  699. array(false),
  700. array('foo => bar'),
  701. array(null),
  702. array(new stdClass),
  703. array(55)
  704. );
  705. }
  706. }