AcceptHeaderTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. namespace Tests\Unit;
  17. use AcceptHeader;
  18. use PHPUnit\Framework\TestCase;
  19. if (!defined('INSTALLDIR')) {
  20. define('INSTALLDIR', dirname(dirname(__DIR__)));
  21. }
  22. if (!defined('PUBLICDIR')) {
  23. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  24. }
  25. if (!defined('GNUSOCIAL')) {
  26. define('GNUSOCIAL', true);
  27. }
  28. if (!defined('STATUSNET')) { // Compatibility
  29. define('STATUSNET', true);
  30. }
  31. require_once INSTALLDIR . '/lib/util/common.php';
  32. final class AcceptHeaderTest extends TestCase
  33. {
  34. public function testHeader1()
  35. {
  36. $acceptHeader = new AcceptHeader('audio/*; q=0.2, audio/basic');
  37. $this->assertEquals('audio/basic', $this->_getMedia($acceptHeader[0]));
  38. $this->assertEquals('audio/*; q=0.2', $this->_getMedia($acceptHeader[1]));
  39. }
  40. public function testHeader2()
  41. {
  42. $acceptHeader = new AcceptHeader('text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5');
  43. $this->assertEquals('text/html; level=1', $this->_getMedia($acceptHeader[0]));
  44. $this->assertEquals('text/html; q=0.7', $this->_getMedia($acceptHeader[1]));
  45. $this->assertEquals('*/*; q=0.5', $this->_getMedia($acceptHeader[2]));
  46. $this->assertEquals('text/html; level=2; q=0.4', $this->_getMedia($acceptHeader[3]));
  47. $this->assertEquals('text/*; q=0.3', $this->_getMedia($acceptHeader[4]));
  48. }
  49. public function testHeader3()
  50. {
  51. $acceptHeader = new AcceptHeader('text/*, text/html, text/html;level=1, */*');
  52. $this->assertEquals('text/html; level=1', $this->_getMedia($acceptHeader[0]));
  53. $this->assertEquals('text/html', $this->_getMedia($acceptHeader[1]));
  54. $this->assertEquals('text/*', $this->_getMedia($acceptHeader[2]));
  55. $this->assertEquals('*/*', $this->_getMedia($acceptHeader[3]));
  56. }
  57. private function _getMedia(array $mediaType)
  58. {
  59. $str = $mediaType['type'] . '/' . $mediaType['subtype'];
  60. if (!empty($mediaType['params'])) {
  61. foreach ($mediaType['params'] as $k => $v) {
  62. $str .= '; ' . $k . '=' . $v;
  63. }
  64. }
  65. return $str;
  66. }
  67. }