SocketTest.php 28 KB

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