|
| 1 | +// Copyright 2025 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import * as Common from './common.js'; |
| 6 | + |
| 7 | +describe('Gzip', () => { |
| 8 | + it('can compress and decompress a string', async () => { |
| 9 | + const text = 'Hello, world!'; |
| 10 | + const compressed = await Common.Gzip.compress(text); |
| 11 | + const decompressed = await Common.Gzip.decompress(compressed); |
| 12 | + assert.strictEqual(decompressed, text); |
| 13 | + }); |
| 14 | + |
| 15 | + it('can compress and decompress a stream', async () => { |
| 16 | + const text = 'Hello, world! This is a stream test.'; |
| 17 | + const textEncoder = new TextEncoder(); |
| 18 | + const inputStream = new ReadableStream({ |
| 19 | + start(controller) { |
| 20 | + controller.enqueue(textEncoder.encode(text)); |
| 21 | + controller.close(); |
| 22 | + }, |
| 23 | + }); |
| 24 | + |
| 25 | + const compressedStream = Common.Gzip.compressStream(inputStream); |
| 26 | + const decompressedStream = Common.Gzip.decompressStream(compressedStream); |
| 27 | + |
| 28 | + const buffer = await new Response(decompressedStream).arrayBuffer(); |
| 29 | + const decodedText = new TextDecoder().decode(buffer); |
| 30 | + |
| 31 | + assert.strictEqual(decodedText, text); |
| 32 | + }); |
| 33 | +}); |
| 34 | + |
| 35 | +describe('arrayBufferToString', () => { |
| 36 | + it('can decompress a gzipped buffer', async () => { |
| 37 | + const text = 'Hello, world!'; |
| 38 | + const compressed = await Common.Gzip.compress(text); |
| 39 | + const result = await Common.Gzip.arrayBufferToString(compressed); |
| 40 | + assert.strictEqual(result, text); |
| 41 | + }); |
| 42 | + it('can decode a plaintext buffer', async () => { |
| 43 | + const text = 'Hello, buddy!'; |
| 44 | + const buffer = new TextEncoder().encode(text).buffer as ArrayBuffer; |
| 45 | + const result = await Common.Gzip.arrayBufferToString(buffer); |
| 46 | + assert.strictEqual(result, text); |
| 47 | + }); |
| 48 | +}); |
| 49 | + |
| 50 | +describe('fileToString', () => { |
| 51 | + it('can decompress a gzipped file', async () => { |
| 52 | + const text = '{"key": "value"}'; |
| 53 | + const compressed = await Common.Gzip.compress(text); |
| 54 | + const result = await Common.Gzip.fileToString(new File([compressed], 'file.json.gz', {type: 'application/gzip'})); |
| 55 | + assert.strictEqual(result, text); |
| 56 | + }); |
| 57 | + it('can decode a plaintext file', async () => { |
| 58 | + const text = 'Hello, buddy!'; |
| 59 | + const file = new File([text], 'test.txt'); |
| 60 | + const result = await Common.Gzip.fileToString(file); |
| 61 | + assert.strictEqual(result, text); |
| 62 | + }); |
| 63 | +}); |
0 commit comments