2
0

CommonHttpTests.php 26 KB

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