RequestTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Network\Http;
  15. use Cake\Network\Http\Request;
  16. use Cake\TestSuite\TestCase;
  17. /**
  18. * HTTP request test.
  19. */
  20. class RequestTest extends TestCase {
  21. /**
  22. * test url method
  23. *
  24. * @return void
  25. */
  26. public function testUrl() {
  27. $request = new Request();
  28. $this->assertSame($request, $request->url('http://example.com'));
  29. $this->assertEquals('http://example.com', $request->url());
  30. }
  31. /**
  32. * test method method.
  33. *
  34. * @return void
  35. */
  36. public function testMethod() {
  37. $request = new Request();
  38. $this->assertSame($request, $request->method(Request::METHOD_GET));
  39. $this->assertEquals(Request::METHOD_GET, $request->method());
  40. }
  41. /**
  42. * test invalid method.
  43. *
  44. * @expectedException \Cake\Core\Exception\Exception
  45. * @return void
  46. */
  47. public function testMethodInvalid() {
  48. $request = new Request();
  49. $request->method('set on fire');
  50. }
  51. /**
  52. * test body method.
  53. *
  54. * @return void
  55. */
  56. public function testBody() {
  57. $data = '{"json":"data"}';
  58. $request = new Request();
  59. $this->assertSame($request, $request->body($data));
  60. $this->assertEquals($data, $request->body());
  61. }
  62. /**
  63. * test header method.
  64. *
  65. * @return void
  66. */
  67. public function testHeader() {
  68. $request = new Request();
  69. $type = 'application/json';
  70. $result = $request->header('Content-Type', $type);
  71. $this->assertSame($result, $request, 'Should return self');
  72. $result = $request->header('content-type');
  73. $this->assertEquals($type, $result, 'lowercase does not work');
  74. $result = $request->header('ConTent-typE');
  75. $this->assertEquals($type, $result, 'Funny casing does not work');
  76. $result = $request->header([
  77. 'Connection' => 'close',
  78. 'user-agent' => 'CakePHP'
  79. ]);
  80. $this->assertSame($result, $request, 'Should return self');
  81. $this->assertEquals('close', $request->header('connection'));
  82. $this->assertEquals('CakePHP', $request->header('USER-AGENT'));
  83. $this->assertNull($request->header('not set'));
  84. }
  85. /**
  86. * test cookie method.
  87. *
  88. * @return void
  89. */
  90. public function testCookie() {
  91. $request = new Request();
  92. $result = $request->cookie('session', '123456');
  93. $this->assertSame($result, $request, 'Should return self');
  94. $this->assertNull($request->cookie('not set'));
  95. $result = $request->cookie('session');
  96. $this->assertEquals('123456', $result);
  97. }
  98. }