HeaderTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace Qiniu\Tests;
  3. use Qiniu\Http\Header;
  4. class HeaderTest extends \PHPUnit_Framework_TestCase
  5. {
  6. protected $heads = array(
  7. ':status' => array('200'),
  8. ':x-test-1' => array('hello1'),
  9. ':x-Test-2' => array('hello2'),
  10. 'content-type' => array('application/json'),
  11. 'CONTENT-LENGTH' => array(1234),
  12. 'oRiGin' => array('https://www.qiniu.com'),
  13. 'ReFer' => array('www.qiniu.com'),
  14. 'Last-Modified' => array('Mon, 06 Sep 2021 06:44:52 GMT'),
  15. 'acCePt-ChArsEt' => array('utf-8'),
  16. 'x-test-3' => array('hello3'),
  17. 'cache-control' => array('no-cache', 'no-store'),
  18. );
  19. public function testNormalizeKey()
  20. {
  21. $except = array(
  22. ':status',
  23. ':x-test-1',
  24. ':x-Test-2',
  25. 'Content-Type',
  26. 'Content-Length',
  27. 'Origin',
  28. 'Refer',
  29. 'Last-Modified',
  30. 'Accept-Charset',
  31. 'X-Test-3',
  32. 'Cache-Control'
  33. );
  34. $actual = array_map(function ($str) {
  35. return Header::normalizeKey($str);
  36. }, array_keys($this->heads));
  37. $this->assertEquals($actual, $except);
  38. }
  39. public function testInvalidKeyName()
  40. {
  41. $except = array(
  42. 'a:x-test-1',
  43. );
  44. $actual = array_map(function ($str) {
  45. return Header::normalizeKey($str);
  46. }, $except);
  47. $this->assertEquals($except, $actual);
  48. }
  49. public function testGetRawData()
  50. {
  51. $header = new Header($this->heads);
  52. foreach ($this->heads as $k => $v) {
  53. $rawHeader = $header->getRawData();
  54. $this->assertEquals($v, $rawHeader[Header::normalizeKey($k)]);
  55. }
  56. }
  57. public function testOffsetExists()
  58. {
  59. $header = new Header($this->heads);
  60. foreach (array_keys($this->heads) as $k) {
  61. $this->assertNotNull($header[$k]);
  62. }
  63. $except = array(
  64. ':status',
  65. ':x-test-1',
  66. ':x-Test-2',
  67. 'Content-Type',
  68. 'Content-Length',
  69. 'Origin',
  70. 'Refer',
  71. 'Last-Modified',
  72. 'Accept-Charset',
  73. 'X-Test-3',
  74. 'Cache-Control'
  75. );
  76. foreach ($except as $k) {
  77. $this->assertNotNull($header[$k], $k." is null");
  78. }
  79. }
  80. public function testOffsetGet()
  81. {
  82. $header = new Header($this->heads);
  83. foreach ($this->heads as $k => $v) {
  84. $this->assertEquals($v[0], $header[$k]);
  85. }
  86. $this->assertNull($header['no-exist']);
  87. }
  88. public function testOffsetSet()
  89. {
  90. $header = new Header($this->heads);
  91. $header["X-Test-3"] = "hello";
  92. $this->assertEquals("hello", $header["X-Test-3"]);
  93. $header["x-test-3"] = "hello test3";
  94. $this->assertEquals("hello test3", $header["x-test-3"]);
  95. $header[":x-Test-2"] = "hello";
  96. $this->assertEquals("hello", $header[":x-Test-2"]);
  97. $header[":x-test-2"] = "hello test2";
  98. $this->assertEquals("hello", $header[":x-Test-2"]);
  99. }
  100. public function testOffsetUnset()
  101. {
  102. $header = new Header($this->heads);
  103. unset($header["X-Test-3"]);
  104. $this->assertFalse(isset($header["X-Test-3"]));
  105. $header = new Header($this->heads);
  106. unset($header["x-test-3"]);
  107. $this->assertFalse(isset($header["x-test-3"]));
  108. $header = new Header($this->heads);
  109. unset($header[":x-test-2"]);
  110. $this->assertTrue(isset($header[":x-Test-2"]));
  111. $header = new Header($this->heads);
  112. unset($header[":x-Test-2"]);
  113. $this->assertFalse(isset($header[":x-Test-2"]));
  114. }
  115. public function testGetIterator()
  116. {
  117. $header = new Header($this->heads);
  118. $hasException = false;
  119. try {
  120. foreach ($header as $k => $v) {
  121. $hasException = !isset($header[$k]);
  122. }
  123. } catch (\Exception $e) {
  124. $hasException = true;
  125. }
  126. $this->assertFalse($hasException);
  127. }
  128. public function testEmptyHeaderIterator()
  129. {
  130. $emptyHeader = new Header();
  131. $hasException = false;
  132. try {
  133. foreach ($emptyHeader as $k => $v) {
  134. $hasException = !isset($header[$k]);
  135. }
  136. } catch (\Exception $e) {
  137. $hasException = true;
  138. }
  139. $this->assertFalse($hasException);
  140. }
  141. public function testCount()
  142. {
  143. $header = new Header($this->heads);
  144. $this->assertEquals(count($this->heads), count($header));
  145. }
  146. public function testFromRaw()
  147. {
  148. $lines = array();
  149. foreach ($this->heads as $k => $vs) {
  150. foreach ($vs as $v) {
  151. array_push($lines, $k . ": " . $v);
  152. }
  153. }
  154. $raw = implode("\r\n", $lines);
  155. $headerFromRaw = Header::fromRawText($raw);
  156. $this->assertEquals(new Header($this->heads), $headerFromRaw);
  157. }
  158. }