|
| 1 | +/* |
| 2 | + * Copyright (c) 2011-2026 Contributors to the Eclipse Foundation |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 7 | + * which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 10 | + */ |
| 11 | +package io.vertx.tests.http.compression; |
| 12 | + |
| 13 | +import io.netty.handler.codec.compression.*; |
| 14 | +import io.vertx.core.http.CompressionConfig; |
| 15 | +import org.junit.Assert; |
| 16 | +import org.junit.Before; |
| 17 | +import org.junit.Test; |
| 18 | + |
| 19 | +public class HttpCompressionConfigTest { |
| 20 | + |
| 21 | + private CompressionConfig cfg; |
| 22 | + |
| 23 | + @Before |
| 24 | + public void before() { |
| 25 | + cfg = new CompressionConfig(); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void testBrotli() { |
| 30 | + cfg.addBrotli(); |
| 31 | + assertEquals(StandardCompressionOptions.brotli()); |
| 32 | + cfg.addBrotli(10); |
| 33 | + assertEquals(StandardCompressionOptions.brotli(10, 22, BrotliMode.TEXT)); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testDeflate() { |
| 38 | + DeflateOptions def = StandardCompressionOptions.deflate(); |
| 39 | + cfg.addDeflate(); |
| 40 | + assertEquals(def); |
| 41 | + cfg.addDeflate(8); |
| 42 | + assertEquals(StandardCompressionOptions.deflate(8, def.windowBits(), def.memLevel())); |
| 43 | + cfg.addDeflate(8, 10, 7); |
| 44 | + assertEquals(StandardCompressionOptions.deflate(8, 10, 7)); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testGzip() { |
| 49 | + GzipOptions def = StandardCompressionOptions.gzip(); |
| 50 | + cfg.addGzip(); |
| 51 | + assertEquals(StandardCompressionOptions.gzip()); |
| 52 | + cfg.addGzip(8); |
| 53 | + assertEquals(StandardCompressionOptions.gzip(8, def.windowBits(), def.memLevel())); |
| 54 | + cfg.addGzip(8, 10, 7); |
| 55 | + assertEquals(StandardCompressionOptions.gzip(8, 10, 7)); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testSnappy() { |
| 60 | + cfg.addSnappy(); |
| 61 | + assertEquals(StandardCompressionOptions.snappy()); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testZstd() { |
| 66 | + ZstdOptions def = StandardCompressionOptions.zstd(); |
| 67 | + cfg.addZstd(); |
| 68 | + assertEquals(def); |
| 69 | + cfg.addZstd(10); |
| 70 | + assertEquals(StandardCompressionOptions.zstd(10, def.blockSize(), def.maxEncodeSize())); |
| 71 | + cfg.addZstd(10, 11, 12); |
| 72 | + assertEquals(StandardCompressionOptions.zstd(10, 11, 12)); |
| 73 | + } |
| 74 | + |
| 75 | + private void assertEquals(BrotliOptions expected) { |
| 76 | + BrotliOptions actual = (BrotliOptions) cfg.getCompressors().get(0); |
| 77 | + Assert.assertEquals(expected.parameters().lgwin(), actual.parameters().lgwin()); |
| 78 | + Assert.assertEquals(expected.parameters().mode(), actual.parameters().mode()); |
| 79 | + Assert.assertEquals(expected.parameters().quality(), actual.parameters().quality()); |
| 80 | + } |
| 81 | + |
| 82 | + private void assertEquals(DeflateOptions expected) { |
| 83 | + DeflateOptions actual = (DeflateOptions) cfg.getCompressors().get(0); |
| 84 | + Assert.assertEquals(expected.compressionLevel(), actual.compressionLevel()); |
| 85 | + Assert.assertEquals(expected.memLevel(), actual.memLevel()); |
| 86 | + Assert.assertEquals(expected.windowBits(), actual.windowBits()); |
| 87 | + } |
| 88 | + |
| 89 | + private void assertEquals(GzipOptions expected) { |
| 90 | + GzipOptions actual = (GzipOptions) cfg.getCompressors().get(0); |
| 91 | + Assert.assertEquals(expected.compressionLevel(), actual.compressionLevel()); |
| 92 | + Assert.assertEquals(expected.memLevel(), actual.memLevel()); |
| 93 | + Assert.assertEquals(expected.windowBits(), actual.windowBits()); |
| 94 | + } |
| 95 | + |
| 96 | + private void assertEquals(ZstdOptions expected) { |
| 97 | + ZstdOptions actual = (ZstdOptions) cfg.getCompressors().get(0); |
| 98 | + Assert.assertEquals(expected.compressionLevel(), actual.compressionLevel()); |
| 99 | + Assert.assertEquals(expected.maxEncodeSize(), actual.maxEncodeSize()); |
| 100 | + Assert.assertEquals(expected.blockSize(), actual.blockSize()); |
| 101 | + } |
| 102 | + |
| 103 | + private void assertEquals(SnappyOptions expected) { |
| 104 | + // No config at the moment |
| 105 | + } |
| 106 | +} |
0 commit comments