NegotiateTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\HTTP;
  4. class NegotiateTest extends \PHPUnit\Framework\TestCase
  5. {
  6. /**
  7. * @dataProvider negotiateData
  8. */
  9. public function testNegotiate($acceptHeader, $available, $expected)
  10. {
  11. $this->assertEquals(
  12. $expected,
  13. negotiateContentType($acceptHeader, $available)
  14. );
  15. }
  16. public function negotiateData()
  17. {
  18. return [
  19. [ // simple
  20. 'application/xml',
  21. ['application/xml'],
  22. 'application/xml',
  23. ],
  24. [ // no header
  25. null,
  26. ['application/xml'],
  27. 'application/xml',
  28. ],
  29. [ // 2 options
  30. 'application/json',
  31. ['application/xml', 'application/json'],
  32. 'application/json',
  33. ],
  34. [ // 2 choices
  35. 'application/json, application/xml',
  36. ['application/xml'],
  37. 'application/xml',
  38. ],
  39. [ // quality
  40. 'application/xml;q=0.2, application/json',
  41. ['application/xml', 'application/json'],
  42. 'application/json',
  43. ],
  44. [ // wildcard
  45. 'image/jpeg, image/png, */*',
  46. ['application/xml', 'application/json'],
  47. 'application/xml',
  48. ],
  49. [ // wildcard + quality
  50. 'image/jpeg, image/png; q=0.5, */*',
  51. ['application/xml', 'application/json', 'image/png'],
  52. 'application/xml',
  53. ],
  54. [ // no match
  55. 'image/jpeg',
  56. ['application/xml'],
  57. null,
  58. ],
  59. [ // This is used in sabre/dav
  60. 'text/vcard; version=4.0',
  61. [
  62. // Most often used mime-type. Version 3
  63. 'text/x-vcard',
  64. // The correct standard mime-type. Defaults to version 3 as
  65. // well.
  66. 'text/vcard',
  67. // vCard 4
  68. 'text/vcard; version=4.0',
  69. // vCard 3
  70. 'text/vcard; version=3.0',
  71. // jCard
  72. 'application/vcard+json',
  73. ],
  74. 'text/vcard; version=4.0',
  75. ],
  76. [ // rfc7231 example 1
  77. 'audio/*; q=0.2, audio/basic',
  78. [
  79. 'audio/pcm',
  80. 'audio/basic',
  81. ],
  82. 'audio/basic',
  83. ],
  84. [ // Lower quality after
  85. 'audio/pcm; q=0.2, audio/basic; q=0.1',
  86. [
  87. 'audio/pcm',
  88. 'audio/basic',
  89. ],
  90. 'audio/pcm',
  91. ],
  92. [ // Random parameter, should be ignored
  93. 'audio/pcm; hello; q=0.2, audio/basic; q=0.1',
  94. [
  95. 'audio/pcm',
  96. 'audio/basic',
  97. ],
  98. 'audio/pcm',
  99. ],
  100. [ // No whitespace after type, should pick the one that is the most specific.
  101. 'text/vcard;version=3.0, text/vcard',
  102. [
  103. 'text/vcard',
  104. 'text/vcard; version=3.0',
  105. ],
  106. 'text/vcard; version=3.0',
  107. ],
  108. [ // Same as last one, but order is different
  109. 'text/vcard, text/vcard;version=3.0',
  110. [
  111. 'text/vcard; version=3.0',
  112. 'text/vcard',
  113. ],
  114. 'text/vcard; version=3.0',
  115. ],
  116. [ // Charset should be ignored here.
  117. 'text/vcard; charset=utf-8; version=3.0, text/vcard',
  118. [
  119. 'text/vcard',
  120. 'text/vcard; version=3.0',
  121. ],
  122. 'text/vcard; version=3.0',
  123. ],
  124. [ // Undefined offset issue.
  125. 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2',
  126. ['application/xml', 'application/json', 'image/png'],
  127. 'application/xml',
  128. ],
  129. ];
  130. }
  131. }