1<?php 2 3declare(strict_types=1); 4 5namespace Sabre\HTTP; 6 7class FunctionsTest extends \PHPUnit\Framework\TestCase 8{ 9 /** 10 * @dataProvider getHeaderValuesData 11 */ 12 public function testGetHeaderValues($input, $output) 13 { 14 $this->assertEquals( 15 $output, 16 getHeaderValues($input) 17 ); 18 } 19 20 public function getHeaderValuesData() 21 { 22 return [ 23 [ 24 'a', 25 ['a'], 26 ], 27 [ 28 'a,b', 29 ['a', 'b'], 30 ], 31 [ 32 'a, b', 33 ['a', 'b'], 34 ], 35 [ 36 ['a, b'], 37 ['a', 'b'], 38 ], 39 [ 40 ['a, b', 'c', 'd,e'], 41 ['a', 'b', 'c', 'd', 'e'], 42 ], 43 ]; 44 } 45 46 /** 47 * @dataProvider preferData 48 */ 49 public function testPrefer($input, $output) 50 { 51 $this->assertEquals( 52 $output, 53 parsePrefer($input) 54 ); 55 } 56 57 public function preferData() 58 { 59 return [ 60 [ 61 'foo; bar', 62 ['foo' => true], 63 ], 64 [ 65 'foo; bar=""', 66 ['foo' => true], 67 ], 68 [ 69 'foo=""; bar', 70 ['foo' => true], 71 ], 72 [ 73 'FOO', 74 ['foo' => true], 75 ], 76 [ 77 'respond-async', 78 ['respond-async' => true], 79 ], 80 [ 81 ['respond-async, wait=100', 'handling=lenient'], 82 ['respond-async' => true, 'wait' => 100, 'handling' => 'lenient'], 83 ], 84 [ 85 ['respond-async, wait=100, handling=lenient'], 86 ['respond-async' => true, 'wait' => 100, 'handling' => 'lenient'], 87 ], 88 // Old values 89 [ 90 'return-asynch, return-representation', 91 ['respond-async' => true, 'return' => 'representation'], 92 ], 93 [ 94 'return-minimal', 95 ['return' => 'minimal'], 96 ], 97 [ 98 'strict', 99 ['handling' => 'strict'], 100 ], 101 [ 102 'lenient', 103 ['handling' => 'lenient'], 104 ], 105 // Invalid token 106 [ 107 ['foo=%bar%'], 108 [], 109 ], 110 ]; 111 } 112 113 public function testParseHTTPDate() 114 { 115 $times = [ 116 'Wed, 13 Oct 2010 10:26:00 GMT', 117 'Wednesday, 13-Oct-10 10:26:00 GMT', 118 'Wed Oct 13 10:26:00 2010', 119 ]; 120 121 $expected = 1286965560; 122 123 foreach ($times as $time) { 124 $result = parseDate($time); 125 $this->assertEquals($expected, $result->format('U')); 126 } 127 128 $result = parseDate('Wed Oct 6 10:26:00 2010'); 129 $this->assertEquals(1286360760, $result->format('U')); 130 } 131 132 public function testParseHTTPDateFail() 133 { 134 $times = [ 135 //random string 136 'NOW', 137 // not-GMT timezone 138 'Wednesday, 13-Oct-10 10:26:00 UTC', 139 // No space before the 6 140 'Wed Oct 6 10:26:00 2010', 141 // Invalid day 142 'Wed Oct 0 10:26:00 2010', 143 'Wed Oct 32 10:26:00 2010', 144 'Wed, 0 Oct 2010 10:26:00 GMT', 145 'Wed, 32 Oct 2010 10:26:00 GMT', 146 'Wednesday, 32-Oct-10 10:26:00 GMT', 147 // Invalid hour 148 'Wed, 13 Oct 2010 24:26:00 GMT', 149 'Wednesday, 13-Oct-10 24:26:00 GMT', 150 'Wed Oct 13 24:26:00 2010', 151 ]; 152 153 foreach ($times as $time) { 154 $this->assertFalse(parseDate($time), 'We used the string: '.$time); 155 } 156 } 157 158 public function testTimezones() 159 { 160 $default = date_default_timezone_get(); 161 date_default_timezone_set('Europe/Amsterdam'); 162 163 $this->testParseHTTPDate(); 164 165 date_default_timezone_set($default); 166 } 167 168 public function testToHTTPDate() 169 { 170 $dt = new \DateTime('2011-12-10 12:00:00 +0200'); 171 172 $this->assertEquals( 173 'Sat, 10 Dec 2011 10:00:00 GMT', 174 toDate($dt) 175 ); 176 } 177} 178