1010
1111namespace chillerlan \HTTPTest \Utils ;
1212
13- use TypeError ;
13+ use RuntimeException , TypeError ;
1414
1515use function chillerlan \HTTP \Utils \{
1616 decompress_content , get_json , get_xml , message_to_string , parseUrl , r_rawurlencode ,
1717 uriIsAbsolute , uriIsAbsolutePathReference , uriIsDefaultPort , uriIsNetworkPathReference ,
1818 uriIsRelativePathReference , uriWithoutQueryValue , uriWithQueryValue
1919};
2020
21+ use function extension_loaded , function_exists ;
2122use const chillerlan \HTTP \Utils \URI_DEFAULT_PORTS ;
2223
2324class MessageHelpersTest extends TestAbstract{
@@ -106,6 +107,7 @@ public function testMessageToString():void{
106107
107108 public function decompressDataProvider ():array {
108109 return [
110+ 'br ' => ['brotli_compress ' , 'br ' ],
109111 'compress ' => ['gzcompress ' , 'compress ' ],
110112 'deflate ' => ['gzdeflate ' , 'deflate ' ],
111113 'gzip ' => ['gzencode ' , 'gzip ' ],
@@ -117,7 +119,14 @@ public function decompressDataProvider():array{
117119 * @dataProvider decompressDataProvider
118120 */
119121 public function testDecompressContent (string $ fn , string $ encoding ):void {
120- $ data = $ expected = str_repeat ('compressed string ' , 100 );
122+
123+ // https://github.com/kjdev/php-ext-brotli
124+ if ($ encoding === 'br ' && (!extension_loaded ('brotli ' ) || !function_exists ('brotli_compress ' ))){
125+ $ this ::markTestSkipped ('N/A (ext-brotli not isntalled) ' );
126+ }
127+
128+ $ data = str_repeat ('compressed string ' , 100 );
129+ $ expected = $ data ;
121130 $ response = $ this ->responseFactory ->createResponse ();
122131
123132 if ($ fn ){
@@ -130,6 +139,22 @@ public function testDecompressContent(string $fn, string $encoding):void{
130139 $ this ::assertSame ($ expected , decompress_content ($ response ));
131140 }
132141
142+ public function testDecompressContentUnableToDecompressBrotliException ():void {
143+
144+ if (extension_loaded ('brotli ' ) && function_exists ('brotli_uncompress ' )){
145+ $ this ::markTestSkipped ('N/A (ext-brotli isntalled) ' );
146+ }
147+
148+ $ this ->expectException (RuntimeException::class);
149+ $ this ->expectExceptionMessage ('cannot decompress brotli compressed message body ' );
150+
151+ $ response = $ this ->responseFactory
152+ ->createResponse ()
153+ ->withHeader ('Content-Encoding ' , 'br ' );
154+
155+ decompress_content ($ response );
156+ }
157+
133158 public function testUriIsAbsolute ():void {
134159 $ this ::assertTrue (uriIsAbsolute ($ this ->uriFactory ->createUri ('http://example.org ' )));
135160 $ this ::assertFalse (uriIsAbsolute ($ this ->uriFactory ->createUri ('//example.org ' )));
0 commit comments