SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/tests/httpreqTest.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. use Sprout\Helpers\HttpReq;
  15.  
  16.  
  17. class MyHttpReq extends HttpReq
  18. {
  19. public static function buildHeadersString($headers)
  20. {
  21. return parent::buildHeadersString($headers);
  22. }
  23. }
  24.  
  25.  
  26. class httpreqTest extends PHPUnit_Framework_TestCase
  27. {
  28.  
  29. /**
  30.   * Seriously, if Google has SSL issues then someone isn't doing their job properly
  31.   **/
  32. public function testSSLVerificationOkay()
  33. {
  34. try {
  35. HttpReq::get('https://www.google.com/');
  36. } catch (Exception $ex) {
  37. $this->assertNotContains('SSL', $ex->getMessage());
  38. }
  39. $this->assertTrue(true);
  40. }
  41.  
  42. public function dataBuildHeadersString()
  43. {
  44. return [
  45. ['',''],
  46. [[],''],
  47. ['Test', 'Test'],
  48.  
  49. [
  50. ['Content-type: text/plain'],
  51. "Content-type: text/plain"
  52. ],
  53. [
  54. ['Content-type: text/plain', 'Content-length: 100'],
  55. "Content-type: text/plain\r\nContent-length: 100"
  56. ],
  57. [
  58. ['Content-type' => 'text/plain', 'Content-length' => 100],
  59. "Content-type: text/plain\r\nContent-length: 100"
  60. ],
  61. [
  62. ['X-Something-Weird' => 'test'],
  63. "X-Something-Weird: test"
  64. ],
  65. ];
  66. }
  67.  
  68. /**
  69.   * @dataProvider dataBuildHeadersString
  70.   **/
  71. public function testBuildHeadersString($input, $expected)
  72. {
  73. $this->assertEquals($expected, MyHttpReq::buildHeadersString($input));
  74. }
  75.  
  76.  
  77.  
  78. public function dataBuildHeadersStringInvalid()
  79. {
  80. $bad = [
  81. '',
  82. "\r\n", "\n\r", "\r", "\n",
  83. "\t", "\v", "\e", "\f",
  84. "\0",
  85. "\x01",
  86. ];
  87.  
  88. $out = [];
  89. foreach ($bad as $b) {
  90. $out[] = [['X-Something' => $b]];
  91. }
  92. foreach ($bad as $b) {
  93. $out[] = [[$b => 'Test']];
  94. }
  95. foreach ($bad as $b) {
  96. foreach ($bad as $c) {
  97. $out[] = [[$b => $c]];
  98. }
  99. }
  100. return $out;
  101. }
  102.  
  103. /**
  104.   * @dataProvider dataBuildHeadersStringInvalid
  105.   * @expectedException InvalidArgumentException
  106.   **/
  107. public function testBuildHeadersStringInvalid($input)
  108. {
  109. MyHttpReq::buildHeadersString($input);
  110. }
  111.  
  112. }
  113.