RequestTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\net\http;
  9. use lithium\net\http\Request;
  10. class RequestTest extends \lithium\test\Unit {
  11. public $request = null;
  12. public function setUp() {
  13. $this->request = new Request(array('init' => false));
  14. }
  15. public function testConstruct() {
  16. $request = new Request(array(
  17. 'host' => 'localhost',
  18. 'port' => 443,
  19. 'headers' => array('Header' => 'Value'),
  20. 'body' => array('Part 1')
  21. ));
  22. $expected = 'localhost';
  23. $result = $request->host;
  24. $this->assertEqual($expected, $result);
  25. $expected = 443;
  26. $result = $request->port;
  27. $this->assertEqual($expected, $result);
  28. $expected = 'GET';
  29. $result = $request->method;
  30. $this->assertEqual($expected, $result);
  31. $expected = 'HTTP/1.1';
  32. $result = $request->protocol;
  33. $this->assertEqual($expected, $result);
  34. $expected = '1.1';
  35. $result = $request->version;
  36. $this->assertEqual($expected, $result);
  37. $expected = '/';
  38. $result = $request->path;
  39. $this->assertEqual($expected, $result);
  40. $expected = array(
  41. 'Host: localhost:443',
  42. 'Connection: Close',
  43. 'User-Agent: Mozilla/5.0',
  44. 'Header: Value'
  45. );
  46. $result = $request->headers();
  47. $this->assertEqual($expected, $result);
  48. $expected = array();
  49. $result = $request->cookies;
  50. $this->assertEqual($expected, $result);
  51. $expected = 'Part 1';
  52. $result = $request->body();
  53. $this->assertEqual($expected, $result);
  54. }
  55. public function testConstructWithPath() {
  56. $request = new Request(array(
  57. 'host' => 'localhost/base/path',
  58. 'port' => 443,
  59. 'headers' => array('Header' => 'Value'),
  60. 'body' => array('Part 1'),
  61. 'params' => array('param' => 'value')
  62. ));
  63. $expected = '/base/path';
  64. $result = $request->path;
  65. $this->assertEqual($expected, $result);
  66. }
  67. public function testQueryStringDefault() {
  68. $expected = "?param=value&param1=value1";
  69. $result = $this->request->queryString(array('param' => 'value', 'param1' => 'value1'));
  70. $this->assertEqual($expected, $result);
  71. }
  72. public function testQueryStringFormat() {
  73. $expected = "?param:value;param1:value1";
  74. $result = $this->request->queryString(
  75. array('param' => 'value', 'param1' => 'value1'), "{:key}:{:value};"
  76. );
  77. $this->assertEqual($expected, $result);
  78. }
  79. public function testQueryStringSetup() {
  80. $expected = "?param=value";
  81. $result = $this->request->queryString(array('param' => 'value'));
  82. $this->assertEqual($expected, $result);
  83. $expected = "?param=value";
  84. $this->request->query = array('param' => 'value');
  85. $result = $this->request->queryString();
  86. $this->assertEqual($expected, $result);
  87. $expected = "?param=value&param2=value2";
  88. $result = $this->request->queryString(array('param2' => 'value2'));
  89. $this->assertEqual($expected, $result);
  90. }
  91. public function testToString() {
  92. $expected = join("\r\n", array(
  93. 'GET / HTTP/1.1',
  94. 'Host: localhost',
  95. 'Connection: Close',
  96. 'User-Agent: Mozilla/5.0',
  97. '', ''
  98. ));
  99. $result = (string) $this->request;
  100. $this->assertEqual($expected, $result);
  101. $result = $this->request->to('string');
  102. $this->assertEqual($expected, $result);
  103. }
  104. public function testToStringWithAuth() {
  105. $request = new Request(array(
  106. 'auth' => 'Basic',
  107. 'username' => 'root',
  108. 'password' => 'something'
  109. ));
  110. $expected = join("\r\n", array(
  111. 'GET / HTTP/1.1',
  112. 'Host: localhost',
  113. 'Connection: Close',
  114. 'User-Agent: Mozilla/5.0',
  115. 'Authorization: Basic ' . base64_encode('root:something'),
  116. '', ''
  117. ));
  118. $result = (string) $request;
  119. $this->assertEqual($expected, $result);
  120. }
  121. public function testToContextWithAuth() {
  122. $request = new Request(array(
  123. 'auth' => 'Basic',
  124. 'username' => 'Aladdin',
  125. 'password' => 'open sesame'
  126. ));
  127. $expected = array('http' => array(
  128. 'method' => 'GET',
  129. 'header' => array(
  130. 'Host: localhost',
  131. 'Connection: Close',
  132. 'User-Agent: Mozilla/5.0',
  133. 'Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='
  134. ),
  135. 'content' => '',
  136. 'protocol_version' => '1.1',
  137. 'ignore_errors' => true,
  138. 'follow_location' => true,
  139. 'request_fulluri' => false,
  140. 'proxy' => null
  141. ));
  142. $this->assertEqual($expected, $request->to('context'));
  143. }
  144. public function testToStringWithBody() {
  145. $expected = join("\r\n", array(
  146. 'GET / HTTP/1.1',
  147. 'Host: localhost',
  148. 'Connection: Close',
  149. 'User-Agent: Mozilla/5.0',
  150. 'Content-Length: 11',
  151. '', 'status=cool'
  152. ));
  153. $this->request->body(array('status=cool'));
  154. $result = (string) $this->request;
  155. $this->assertEqual($expected, $result);
  156. }
  157. public function testToArray() {
  158. $expected = array(
  159. 'method' => 'GET',
  160. 'query' => array(),
  161. 'headers' => array(
  162. 'Host' => 'localhost',
  163. 'Connection' => 'Close',
  164. 'User-Agent' => 'Mozilla/5.0'
  165. ),
  166. 'cookies' => array(),
  167. 'protocol' => 'HTTP/1.1',
  168. 'version' => '1.1',
  169. 'body' => array(),
  170. 'scheme' => 'http',
  171. 'host' => 'localhost',
  172. 'port' => null,
  173. 'path' => '/',
  174. 'auth' => null,
  175. 'username' => null,
  176. 'password' => null
  177. );
  178. $result = $this->request->to('array');
  179. $this->assertEqual($expected, $result);
  180. }
  181. /**
  182. * Tests that creating a `Request` with a proxy configuration correctly modifies the results
  183. * of exporting the `Request` to a stream context configuration.
  184. */
  185. public function testWithProxy() {
  186. $request = new Request(array('proxy' => 'tcp://proxy.example.com:5100'));
  187. $expected = array('http' => array(
  188. 'content' => '',
  189. 'method' => 'GET',
  190. 'header' => array('Host: localhost', 'Connection: Close', 'User-Agent: Mozilla/5.0'),
  191. 'protocol_version' => '1.1',
  192. 'ignore_errors' => true,
  193. 'follow_location' => true,
  194. 'request_fulluri' => true,
  195. 'proxy' => 'tcp://proxy.example.com:5100'
  196. ));
  197. $this->assertEqual($expected, $request->to('context'));
  198. }
  199. public function testToUrl() {
  200. $expected = 'http://localhost/';
  201. $result = $this->request->to('url');
  202. $this->assertEqual($expected, $result);
  203. $this->request = new Request(array('scheme' => 'https', 'port' => 443));
  204. $expected = 'https://localhost:443/';
  205. $result = $this->request->to('url');
  206. $this->assertEqual($expected, $result);
  207. }
  208. public function testToContext() {
  209. $expected = array('http' => array(
  210. 'method' => 'GET',
  211. 'content' => '',
  212. 'header' => array(
  213. 'Host: localhost',
  214. 'Connection: Close',
  215. 'User-Agent: Mozilla/5.0'
  216. ),
  217. 'protocol_version' => '1.1',
  218. 'ignore_errors' => true,
  219. 'follow_location' => true,
  220. 'request_fulluri' => false,
  221. 'proxy' => null
  222. ));
  223. $result = $this->request->to('context');
  224. $this->assertEqual($expected, $result);
  225. }
  226. public function testQueryStringWithArrayValues() {
  227. $expected = "?param%5B0%5D=value1&param%5B1%5D=value2";
  228. $result = $this->request->queryString(array('param' => array('value1', 'value2')));
  229. $this->assertEqual($expected, $result);
  230. }
  231. public function testQueryStringWithArrayValuesCustomFormat() {
  232. $expected = "?param%5B%5D:value1/param%5B%5D:value2";
  233. $result = $this->request->queryString(
  234. array('param' => array('value1', 'value2')),
  235. "{:key}:{:value}/"
  236. );
  237. $this->assertEqual($expected, $result);
  238. }
  239. public function testDigest() {
  240. $request = new Request(array(
  241. 'path' => '/http_auth',
  242. 'auth' => array(
  243. 'realm' => 'app',
  244. 'qop' => 'auth',
  245. 'nonce' => '4bca0fbca7bd0',
  246. 'opaque' => 'd3fb67a7aa4d887ec4bf83040a820a46'
  247. ),
  248. 'username' => 'gwoo',
  249. 'password' => 'li3'
  250. ));
  251. $cnonce = md5(time());
  252. $user = md5("gwoo:app:li3");
  253. $nonce = "4bca0fbca7bd0:00000001:{$cnonce}:auth";
  254. $req = md5("GET:/http_auth");
  255. $hash = md5("{$user}:{$nonce}:{$req}");
  256. $request->to('url');
  257. preg_match('/response="(.*?)"/', $request->headers('Authorization'), $matches);
  258. list($match, $response) = $matches;
  259. $expected = $hash;
  260. $result = $response;
  261. $this->assertEqual($expected, $result);
  262. }
  263. public function testParseUrlToConfig() {
  264. $url = "http://localhost/path/one.php?param=1&param=2";
  265. $config = parse_url($url);
  266. $request = new Request($config);
  267. $expected = $url;
  268. $result = $request->to('url');
  269. $this->assertEqual($expected, $result);
  270. }
  271. public function testQueryParamsConstructed() {
  272. $url = "http://localhost/path/one.php?param=1&param=2";
  273. $config = parse_url($url);
  274. $request = new Request($config);
  275. $expected = "?param=1&param=2";
  276. $result = $request->queryString();
  277. $this->assertEqual($expected, $result);
  278. $expected = "?param=1&param=2&param3=3";
  279. $result = $request->queryString(array('param3' => 3));
  280. $this->assertEqual($expected, $result);
  281. }
  282. public function testKeepDefinedContentTypeHeaderOnPost() {
  283. $request = new Request(array(
  284. 'method' => 'POST',
  285. 'headers' => array('Content-Type' => 'text/x-test')
  286. ));
  287. $expected = 'Content-Type: text/x-test';
  288. $result = $request->headers();
  289. $message = "Expected value `{$expected}` not found in result.";
  290. $this->assertTrue(in_array($expected, $result), $message);
  291. $expected = '#Content-Type: text/x-test#';
  292. $result = $request->to('string');
  293. $this->assertPattern($expected, $result);
  294. }
  295. public function testKeepDefinedContentTypeHeaderWhenTypeIsSet() {
  296. $request = new Request(array(
  297. 'method' => 'POST',
  298. 'type' => 'json',
  299. 'headers' => array('Content-Type' => 'text/x-test')
  300. ));
  301. $expected = 'Content-Type: text/x-test';
  302. $result = $request->headers();
  303. $message = "Expected value `{$expected}` not found in result.";
  304. $this->assertTrue(in_array($expected, $result), $message);
  305. $expected = '#Content-Type: text/x-test#';
  306. $result = $request->to('string');
  307. $this->assertPattern($expected, $result);
  308. }
  309. }
  310. ?>