HeaderTest.php 4.9 KB

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