| 1 |
<?php |
| 2 |
|
| 3 |
namespace WebPConvert\Tests\Serve; |
| 4 |
|
| 5 |
use WebPConvert\Serve\ServeFile; |
| 6 |
use WebPConvert\Serve\MockedHeader; |
| 7 |
use WebPConvert\Exceptions\InvalidInpuException; |
| 8 |
use WebPConvert\Exceptions\InvalidInput\InvalidImageTypeException; |
| 9 |
use WebPConvert\Exceptions\InvalidInput\TargetNotFoundException; |
| 10 |
//use WebPConvert\Serve\Exceptions\ServeFailedException; |
| 11 |
|
| 12 |
use PHPUnit\Framework\TestCase; |
| 13 |
|
| 14 |
class ServeFileTest extends TestCase |
| 15 |
{ |
| 16 |
|
| 17 |
public function getImageFolder() |
| 18 |
{ |
| 19 |
return realpath(__DIR__ . '/../images'); |
| 20 |
} |
| 21 |
|
| 22 |
public function getImagePath($image) |
| 23 |
{ |
| 24 |
return $this->getImageFolder() . '/' . $image; |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
/** |
| 29 |
* Call to serve and return result or exception |
| 30 |
* |
| 31 |
* The method takes care of closing output buffer in case of exception |
| 32 |
* |
| 33 |
* @return array First item: the output, second item: Exception, if thrown |
| 34 |
*/ |
| 35 |
public static function callServe($filename, $mime, $options) |
| 36 |
{ |
| 37 |
ob_start(); |
| 38 |
try { |
| 39 |
ServeFile::serve($filename, $mime, $options); |
| 40 |
} catch (\Exception $e) { |
| 41 |
return [ob_get_clean(), $e]; |
| 42 |
} catch (\Throwable $e) { |
| 43 |
return [ob_get_clean(), $e]; |
| 44 |
} |
| 45 |
return [ob_get_clean(), null]; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Call to serve and return result or exception |
| 50 |
* |
| 51 |
* The method takes care of closing output buffer in case of exception |
| 52 |
* |
| 53 |
* @return string the output |
| 54 |
*/ |
| 55 |
public static function callServeWithThrow($filename, $mime, $options) |
| 56 |
{ |
| 57 |
ob_start(); |
| 58 |
try { |
| 59 |
ServeFile::serve($filename, $mime, $options); |
| 60 |
} catch (\Exception $e) { |
| 61 |
ob_get_clean(); |
| 62 |
throw($e); |
| 63 |
} catch (\Throwable $e) { |
| 64 |
ob_get_clean(); |
| 65 |
throw($e); |
| 66 |
} |
| 67 |
return ob_get_clean(); |
| 68 |
} |
| 69 |
|
| 70 |
public function testServeDefaultOptions() |
| 71 |
{ |
| 72 |
MockedHeader::reset(); |
| 73 |
|
| 74 |
$filename = self::getImagePath('plaintext-with-jpg-extension.jpg'); |
| 75 |
$this->assertTrue(file_exists($filename)); |
| 76 |
|
| 77 |
$result = self::callServeWithThrow($filename, 'image/webp', []); |
| 78 |
|
| 79 |
// Test that content of file was send to output |
| 80 |
$isWindows = preg_match('/^win/i', PHP_OS); |
| 81 |
if ($isWindows) { |
| 82 |
$this->assertEquals("text\r\n", $result); |
| 83 |
} else { |
| 84 |
$this->assertEquals("text\n", $result); |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
$headers = MockedHeader::getHeaders(); |
| 89 |
$this->assertGreaterThanOrEqual(1, MockedHeader::getNumHeaders()); |
| 90 |
|
| 91 |
// Test that headers were set as expected |
| 92 |
$this->assertTrue(MockedHeader::hasHeader('Content-Type: image/webp')); |
| 93 |
$this->assertFalse(MockedHeader::hasHeader('Vary: Accept')); |
| 94 |
|
| 95 |
$this->assertTrue(MockedHeader::hasHeaderContaining('Content-Length:')); |
| 96 |
|
| 97 |
//$this->assertTrue(MockedHeader::hasHeader('Last-Modified: Mon, 29 Apr 2019 12:54:37 GMT')); |
| 98 |
|
| 99 |
// TODO:The following fails on travis. WHY??? |
| 100 |
//$this->assertTrue(MockedHeader::hasHeaderContaining('Last-Modified:')); |
| 101 |
|
| 102 |
//$this->assertTrue(MockedHeader::hasHeader('Cache-Control: public, max-age=86400')); |
| 103 |
//$this->assertTrue(MockedHeader::hasHeaderContaining('Expires:')); |
| 104 |
} |
| 105 |
|
| 106 |
public function testServeVaryHeader() |
| 107 |
{ |
| 108 |
MockedHeader::reset(); |
| 109 |
|
| 110 |
$this->assertEquals(0, MockedHeader::getNumHeaders()); |
| 111 |
|
| 112 |
$filename = self::getImagePath('plaintext-with-jpg-extension.jpg'); |
| 113 |
$this->assertTrue(file_exists($filename)); |
| 114 |
|
| 115 |
$options = [ |
| 116 |
'headers' => [ |
| 117 |
'vary-accept' => true |
| 118 |
] |
| 119 |
]; |
| 120 |
|
| 121 |
$result = self::callServeWithThrow($filename, 'image/webp', $options); |
| 122 |
|
| 123 |
$this->assertTrue(MockedHeader::hasHeader('Vary: Accept')); |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
public function testServeNoHeaders() |
| 129 |
{ |
| 130 |
MockedHeader::reset(); |
| 131 |
|
| 132 |
$this->assertEquals(0, MockedHeader::getNumHeaders()); |
| 133 |
|
| 134 |
$filename = self::getImagePath('plaintext-with-jpg-extension.jpg'); |
| 135 |
$this->assertTrue(file_exists($filename)); |
| 136 |
|
| 137 |
$options = [ |
| 138 |
'headers' => [ |
| 139 |
'cache-control' => false, |
| 140 |
'content-length' => false, |
| 141 |
'content-type' => false, |
| 142 |
'expires' => false, |
| 143 |
'last-modified' => false, |
| 144 |
'vary-accept' => false |
| 145 |
], |
| 146 |
'cache-control-header' => 'private, max-age=100', |
| 147 |
]; |
| 148 |
|
| 149 |
$result = self::callServeWithThrow($filename, 'image/webp', $options); |
| 150 |
|
| 151 |
// Test that content of file was send to output |
| 152 |
$isWindows = preg_match('/^win/i', PHP_OS); |
| 153 |
if ($isWindows) { |
| 154 |
$this->assertEquals("text\r\n", $result); |
| 155 |
} else { |
| 156 |
$this->assertEquals("text\n", $result); |
| 157 |
} |
| 158 |
|
| 159 |
// Test that headers were set as expected |
| 160 |
// We actually expect that none are added. |
| 161 |
|
| 162 |
$headers = MockedHeader::getHeaders(); |
| 163 |
$this->assertEquals(0, MockedHeader::getNumHeaders()); |
| 164 |
|
| 165 |
// TODO:The following fails on travis. WHY??? |
| 166 |
//$this->assertFalse(MockedHeader::hasHeader('Content-Type: image/webp')); |
| 167 |
|
| 168 |
//$this->assertTrue(MockedHeader::hasHeader('Vary: Accept')); |
| 169 |
//$this->assertFalse(MockedHeader::hasHeader('Last-Modified: Mon, 29 Apr 2019 12:54:37 GMT')); |
| 170 |
|
| 171 |
// TODO:The following fails on travis. WHY??? |
| 172 |
//$this->assertTrue(MockedHeader::hasHeaderContaining('Last-Modified:')); |
| 173 |
|
| 174 |
|
| 175 |
$this->assertFalse(MockedHeader::hasHeader('Cache-Control: public, max-age=86400')); |
| 176 |
$this->assertFalse(MockedHeader::hasHeaderContaining('Expires:')); |
| 177 |
} |
| 178 |
|
| 179 |
public function testServeCustomCacheControl() |
| 180 |
{ |
| 181 |
MockedHeader::reset(); |
| 182 |
$filename = self::getImagePath('plaintext-with-jpg-extension.jpg'); |
| 183 |
$this->assertTrue(file_exists($filename)); |
| 184 |
|
| 185 |
$options = [ |
| 186 |
'headers' => [ |
| 187 |
'cache-control' => true, |
| 188 |
'expires' => true, |
| 189 |
], |
| 190 |
'cache-control-header' => 'private, max-age=100', |
| 191 |
]; |
| 192 |
|
| 193 |
$result = self::callServeWithThrow($filename, 'image/webp', $options); |
| 194 |
$this->assertTrue(MockedHeader::hasHeader('Cache-Control: private, max-age=100')); |
| 195 |
$this->assertTrue(MockedHeader::hasHeaderContaining('Expires:')); |
| 196 |
} |
| 197 |
|
| 198 |
public function testServeCustomCacheControlNoMaxAge() |
| 199 |
{ |
| 200 |
MockedHeader::reset(); |
| 201 |
$filename = self::getImagePath('plaintext-with-jpg-extension.jpg'); |
| 202 |
$this->assertTrue(file_exists($filename)); |
| 203 |
$options = [ |
| 204 |
'headers' => [ |
| 205 |
'cache-control' => true, |
| 206 |
], |
| 207 |
'cache-control-header' => 'private', |
| 208 |
]; |
| 209 |
$result = self::callServeWithThrow($filename, 'image/webp', $options); |
| 210 |
|
| 211 |
$this->assertTrue(MockedHeader::hasHeader('Cache-Control: private')); |
| 212 |
|
| 213 |
// When there is no max-age, there should neither be any Expires |
| 214 |
$this->assertFalse(MockedHeader::hasHeaderContaining('Expires:')); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
public function testServeNonexistantFile() |
| 219 |
{ |
| 220 |
//MockedHeader::reset(); |
| 221 |
|
| 222 |
$filename = __DIR__ . '/i-dont-exist-no'; |
| 223 |
$this->assertFalse(file_exists($filename)); |
| 224 |
|
| 225 |
//$this->expectException(TargetNotFoundException::class); |
| 226 |
list($result, $e) = self::callServe($filename, 'image/webp', []); |
| 227 |
$this->assertSame( |
| 228 |
'WebPConvert\Exceptions\InvalidInput\TargetNotFoundException', |
| 229 |
get_class($e) |
| 230 |
); |
| 231 |
$this->assertSame('', $result); |
| 232 |
//$this->assertTrue(MockedHeader::hasHeader('X-WebP-Convert-Error: Could not read file')); |
| 233 |
} |
| 234 |
|
| 235 |
} |
| 236 |
require_once('mock-header.inc'); |
| 237 |
|