def test_roundtrip_compression_with_files(simple_compressed_file): """ Roundtripping data through the compressor works correctly. """ with open(simple_compressed_file[0], 'rb') as f: uncompressed_data = f.read() assert brotlicffi.decompress( brotlicffi.compress(uncompressed_data)) == uncompressed_data
def test_brotli(): body = b"test 123" compressed_body = brotlicffi.compress(body) headers = [(b"Content-Encoding", b"br")] response = httpx.Response( 200, headers=headers, content=compressed_body, ) assert response.content == body
def test_decoding_errors(header_value): headers = [(b"Content-Encoding", header_value)] body = b"test 123" compressed_body = brotlicffi.compress(body)[3:] with pytest.raises(httpx.DecodingError): request = httpx.Request("GET", "https://example.org") httpx.Response(200, headers=headers, content=compressed_body, request=request) with pytest.raises(httpx.DecodingError): httpx.Response(200, headers=headers, content=compressed_body)
def close(self): """ All tags must have been specified. Now write the table data and directory. """ if len(self.tables) != self.numTables: raise TTLibError("wrong number of tables; expected %d, found %d" % (self.numTables, len(self.tables))) if self.sfntVersion in ("\x00\x01\x00\x00", "true"): isTrueType = True elif self.sfntVersion == "OTTO": isTrueType = False else: raise TTLibError("Not a TrueType or OpenType font (bad sfntVersion)") # The WOFF2 spec no longer requires the glyph offsets to be 4-byte aligned. # However, the reference WOFF2 implementation still fails to reconstruct # 'unpadded' glyf tables, therefore we need to 'normalise' them. # See: # https://github.com/khaledhosny/ots/issues/60 # https://github.com/google/woff2/issues/15 if ( isTrueType and "glyf" in self.flavorData.transformedTables and "glyf" in self.tables ): self._normaliseGlyfAndLoca(padding=4) self._setHeadTransformFlag() # To pass the legacy OpenType Sanitiser currently included in browsers, # we must sort the table directory and data alphabetically by tag. # See: # https://github.com/google/woff2/pull/3 # https://lists.w3.org/Archives/Public/public-webfonts-wg/2015Mar/0000.html # TODO(user): remove to match spec once browsers are on newer OTS self.tables = OrderedDict(sorted(self.tables.items())) self.totalSfntSize = self._calcSFNTChecksumsLengthsAndOffsets() fontData = self._transformTables() compressedFont = brotli.compress(fontData, mode=brotli.MODE_FONT) self.totalCompressedSize = len(compressedFont) self.length = self._calcTotalSize() self.majorVersion, self.minorVersion = self._getVersion() self.reserved = 0 directory = self._packTableDirectory() self.file.seek(0) self.file.write(pad(directory + compressedFont, size=4)) self._writeFlavorData()
def test_multi_with_identity(): body = b"test 123" compressed_body = brotlicffi.compress(body) headers = [(b"Content-Encoding", b"br, identity")] response = httpx.Response( 200, headers=headers, content=compressed_body, ) assert response.content == body headers = [(b"Content-Encoding", b"identity, br")] response = httpx.Response( 200, headers=headers, content=compressed_body, ) assert response.content == body
def _calcFlavorDataOffsetsAndSize(self, start): """Calculate offsets and lengths for any meta- and/or private data.""" offset = start data = self.flavorData if data.metaData: self.metaOrigLength = len(data.metaData) self.metaOffset = offset self.compressedMetaData = brotli.compress( data.metaData, mode=brotli.MODE_TEXT) self.metaLength = len(self.compressedMetaData) offset += self.metaLength else: self.metaOffset = self.metaLength = self.metaOrigLength = 0 self.compressedMetaData = b"" if data.privData: # make sure private data is padded to 4-byte boundary offset = (offset + 3) & ~3 self.privOffset = offset self.privLength = len(data.privData) offset += self.privLength else: self.privOffset = self.privLength = 0 return offset
def test_compressed_data_roundtrips(s): assert brotlicffi.decompress(brotlicffi.compress(s)) == s
def test_compressed_data_with_dictionaries(s, dictionary): d = brotlicffi.Decompressor(dictionary) compressed = brotlicffi.compress(s, dictionary=dictionary) uncompressed = d.decompress(compressed) assert uncompressed == s
def test_value_error_without_request(header_value): headers = [(b"Content-Encoding", header_value)] body = b"test 123" compressed_body = brotlicffi.compress(body)[3:] with pytest.raises(httpx.DecodingError): httpx.Response(200, headers=headers, content=compressed_body)