CommonHttpTests.php 27 KB

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