AcceptHeaderTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. /**
  17. * ActivityPub implementation for GNU social
  18. *
  19. * @package GNUsocial
  20. * @author Diogo Cordeiro <diogo@fc.up.pt>
  21. * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
  22. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  23. * @link http://www.gnu.org/software/social/
  24. */
  25. require 'AcceptHeader.php';
  26. class ContainerTest extends \PHPUnit_Framework_TestCase
  27. {
  28. public function testHeader1()
  29. {
  30. $acceptHeader = new AcceptHeader('audio/*; q=0.2, audio/basic');
  31. $this->assertEquals('audio/basic', $this->_getMedia($acceptHeader[0]));
  32. $this->assertEquals('audio/*; q=0.2', $this->_getMedia($acceptHeader[1]));
  33. }
  34. public function testHeader2()
  35. {
  36. $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');
  37. $this->assertEquals('text/html; level=1', $this->_getMedia($acceptHeader[0]));
  38. $this->assertEquals('text/html; q=0.7', $this->_getMedia($acceptHeader[1]));
  39. $this->assertEquals('*/*; q=0.5', $this->_getMedia($acceptHeader[2]));
  40. $this->assertEquals('text/html; level=2; q=0.4', $this->_getMedia($acceptHeader[3]));
  41. $this->assertEquals('text/*; q=0.3', $this->_getMedia($acceptHeader[4]));
  42. }
  43. public function testHeader3()
  44. {
  45. $acceptHeader = new AcceptHeader('text/*, text/html, text/html;level=1, */*');
  46. $this->assertEquals('text/html; level=1', $this->_getMedia($acceptHeader[0]));
  47. $this->assertEquals('text/html', $this->_getMedia($acceptHeader[1]));
  48. $this->assertEquals('text/*', $this->_getMedia($acceptHeader[2]));
  49. $this->assertEquals('*/*', $this->_getMedia($acceptHeader[3]));
  50. }
  51. private function _getMedia(array $mediaType)
  52. {
  53. $str = $mediaType['type'] . '/' . $mediaType['subtype'];
  54. if (!empty($mediaType['params'])) {
  55. foreach ($mediaType['params'] as $k => $v) {
  56. $str .= '; ' . $k . '=' . $v;
  57. }
  58. }
  59. return $str;
  60. }
  61. }