HeaderUtilityTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. declare(strict_types=1);
  3. namespace Cake\Test\TestCase\Http;
  4. use Cake\Http\HeaderUtility;
  5. use Cake\Http\Response;
  6. use Cake\Http\ServerRequest;
  7. use Cake\TestSuite\TestCase;
  8. class HeaderUtilityTest extends TestCase
  9. {
  10. /**
  11. * Tests getting a parsed representation of a Link header
  12. */
  13. public function testParseLinks(): void
  14. {
  15. $response = new Response();
  16. $this->assertFalse($response->hasHeader('Link'));
  17. $new = $response->withAddedLink('http://example.com');
  18. $this->assertSame('<http://example.com>', $new->getHeaderLine('Link'));
  19. $expected = [
  20. ['link' => 'http://example.com'],
  21. ];
  22. $this->assertSame($expected, HeaderUtility::parseLinks($new->getHeader('Link')));
  23. $new = $response->withAddedLink('http://example.com/苗条');
  24. $this->assertSame('<http://example.com/苗条>', $new->getHeaderLine('Link'));
  25. $expected = [
  26. ['link' => 'http://example.com/苗条'],
  27. ];
  28. $this->assertSame($expected, HeaderUtility::parseLinks($new->getHeader('Link')));
  29. $new = $response->withAddedLink('http://example.com', ['rel' => 'prev']);
  30. $this->assertSame('<http://example.com>; rel="prev"', $new->getHeaderLine('Link'));
  31. $expected = [
  32. [
  33. 'link' => 'http://example.com',
  34. 'rel' => 'prev',
  35. ],
  36. ];
  37. $this->assertSame($expected, HeaderUtility::parseLinks($new->getHeader('Link')));
  38. $new = $response->withAddedLink('http://example.com', ['rel' => 'prev', 'results' => 'true']);
  39. $this->assertSame('<http://example.com>; rel="prev"; results="true"', $new->getHeaderLine('Link'));
  40. $expected = [
  41. [
  42. 'link' => 'http://example.com',
  43. 'rel' => 'prev',
  44. 'results' => 'true',
  45. ],
  46. ];
  47. $this->assertSame($expected, HeaderUtility::parseLinks($new->getHeader('Link')));
  48. $new = $response
  49. ->withAddedLink('http://example.com/1', ['rel' => 'prev'])
  50. ->withAddedLink('http://example.com/3', ['rel' => 'next']);
  51. $this->assertSame('<http://example.com/1>; rel="prev",<http://example.com/3>; rel="next"', $new->getHeaderLine('Link'));
  52. $expected = [
  53. [
  54. 'link' => 'http://example.com/1',
  55. 'rel' => 'prev',
  56. ],
  57. [
  58. 'link' => 'http://example.com/3',
  59. 'rel' => 'next',
  60. ],
  61. ];
  62. $this->assertSame($expected, HeaderUtility::parseLinks($new->getHeader('Link')));
  63. $encodedLinkHeader = "</extended-attr-example>; rel=start; title*=UTF-8'en'%E2%91%A0%E2%93%AB%E2%85%93%E3%8F%A8%E2%99%B3%F0%9D%84%9E%CE%BB";
  64. $new = $response
  65. ->withHeader('Link', $encodedLinkHeader);
  66. $this->assertSame($encodedLinkHeader, $new->getHeaderLine('Link'));
  67. $expected = [
  68. [
  69. 'link' => '/extended-attr-example',
  70. 'rel' => 'start',
  71. 'title*' => [
  72. 'language' => 'en',
  73. 'encoding' => 'UTF-8',
  74. 'value' => '①⓫⅓㏨♳𝄞λ',
  75. ],
  76. ],
  77. ];
  78. $this->assertSame($expected, HeaderUtility::parseLinks($new->getHeader('Link')));
  79. }
  80. public function testParseAccept(): void
  81. {
  82. $request = new ServerRequest([
  83. 'url' => '/dashboard',
  84. 'environment' => [
  85. 'HTTP_ACCEPT' => 'application/json;q=0.5,application/xml;q=0.6,application/pdf;q=0.3',
  86. ],
  87. ]);
  88. $result = HeaderUtility::parseAccept($request->getHeaderLine('Accept'));
  89. $expected = [
  90. '0.6' => ['application/xml'],
  91. '0.5' => ['application/json'],
  92. '0.3' => ['application/pdf'],
  93. ];
  94. $this->assertEquals($expected, $result);
  95. }
  96. public function testParseWwwAuthenticate(): void
  97. {
  98. $result = HeaderUtility::parseWwwAuthenticate('Digest realm="The batcave",nonce="4cded326c6c51"');
  99. $expected = [
  100. 'realm' => 'The batcave',
  101. 'nonce' => '4cded326c6c51',
  102. ];
  103. $this->assertEquals($expected, $result);
  104. }
  105. public function testWwwAuthenticateWithAlgo(): void
  106. {
  107. $result = HeaderUtility::parseWwwAuthenticate('Digest qop="auth", realm="shellyplus1pm-44179393e8a8", nonce="63f8c86f", algorithm=SHA-256');
  108. $expected = [
  109. 'qop' => 'auth',
  110. 'realm' => 'shellyplus1pm-44179393e8a8',
  111. 'nonce' => '63f8c86f',
  112. 'algorithm' => 'SHA-256',
  113. ];
  114. $this->assertEquals($expected, $result);
  115. }
  116. }