Repository navigation
Add mod_brotli compression tests #840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Good morning, fellow! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod | ||
| tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim | ||
| veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea | ||
| commodo consequat. Duis aute irure dolor in reprehenderit in voluptate | ||
| velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat | ||
| cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id | ||
| est laborum. | ||
|
|
||
| Curabitur pretium tincidunt lacus. Nulla gravida orci a odio. Nullam varius, | ||
| turpis et commodo pharetra, est eros bibendum elit, nec luctus magna felis | ||
| sollicitudin mauris. Integer in mauris eu nibh euismod gravida. Duis ac | ||
| tellus et risus vulputate vehicula. Donec lobortis risus a elit. Etiam | ||
| tempor. Ut ullamcorper, ligula ut dictum pharetra, nisi nunc fringilla | ||
| magna, in commodo elit erat nec turpis. Ut pharetra augue nec augue. | ||
| Nam elit agna, endrerit sit amet, tincidunt ac, viverra sed, nulla. | ||
| Donec porta diam eu massa. Quisque diam lorem, interdum vitae, dapibus | ||
| ac, scelerisque vitae, pede. Donec eget tellus non erat lacinia fermentum. | ||
| Donec in velit vel ipsum auctor pulvinar. Class aptent taciti sociosqu ad | ||
| litora torquent per conubia nostra, per inceptos hymenaeos. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import os | ||
|
|
||
| import pytest | ||
|
|
||
| from pyhttpd.conf import HttpdConf | ||
|
|
||
|
|
||
| class TestBrotliCompression: | ||
|
|
||
| @pytest.fixture(autouse=True, scope='class') | ||
| def _class_scope(self, env): | ||
| conf = HttpdConf(env, extras={ | ||
| f"test1.{env.http_tld}": """ | ||
| <Location "/"> | ||
| AddOutputFilterByType BROTLI_COMPRESS text/plain text/html text/css | ||
| </Location> | ||
| """, | ||
| }) | ||
| conf.add_vhost_test1() | ||
| conf.install() | ||
| assert env.apache_restart() == 0 | ||
|
|
||
| # Request a small text file with Accept-Encoding: br. | ||
| # Verify Content-Encoding: br header is present. | ||
| def test_brotli_004_01_br_header_present(self, env): | ||
| url = env.mkurl("http", "test1", "/greeting.txt") | ||
| r = env.curl_get(url, options=[ | ||
| "-H", "Accept-Encoding: br", | ||
| ]) | ||
| assert r.response, "no response" | ||
| assert r.response["status"] == 200 | ||
| assert r.response["header"].get("content-encoding") == "br", \ | ||
| f"expected Content-Encoding: br, got: {r.response['header']}" | ||
| raw_body = r.response["body"] | ||
| assert len(raw_body) > 0, "compressed body should not be empty" | ||
| original = b"Good morning, fellow!" | ||
| assert len(raw_body) != len(original) or raw_body != original, \ | ||
| "body should be compressed, not plain text" | ||
|
|
||
| # Request a larger text file; verify brotli compression is applied. | ||
| def test_brotli_004_02_large_file(self, env): | ||
| url = env.mkurl("http", "test1", "/lorem.txt") | ||
| r = env.curl_get(url, options=[ | ||
| "-H", "Accept-Encoding: br", | ||
| ]) | ||
| assert r.response, "no response" | ||
| assert r.response["status"] == 200 | ||
| assert r.response["header"].get("content-encoding") == "br", \ | ||
| f"expected Content-Encoding: br, got: {r.response['header']}" | ||
| lorem_path = os.path.join(env.server_dir, "htdocs", "test1", | ||
| "lorem.txt") | ||
| orig_size = os.path.getsize(lorem_path) | ||
| assert len(r.response["body"]) < orig_size, \ | ||
| "compressed response should be smaller than the original" | ||
|
|
||
| # When the client does not advertise Accept-Encoding: br the response | ||
| # must be served uncompressed -- no Content-Encoding header. | ||
| def test_brotli_004_03_response_uncompressd(self, env): | ||
| url = env.mkurl("http", "test1", "/greeting.txt") | ||
| r = env.curl_get(url) | ||
| assert r.response, "no response" | ||
| assert r.response["status"] == 200 | ||
| assert "content-encoding" not in r.response["header"], \ | ||
| f"unexpected Content-Encoding present: " \ | ||
| f"{r.response['header'].get('content-encoding')}" | ||
| body = r.response["body"].decode() | ||
| assert body.strip() == "Good morning, fellow!" | ||
|
|
||
| # Binary / non-matching content types must not be compressed. | ||
| def test_brotli_004_04_binary_no_match_no_compression(self, env): | ||
| bin_dir = os.path.join(env.server_dir, "htdocs", "test1") | ||
| bin_path = os.path.join(bin_dir, "pixel.png") | ||
| with open(bin_path, "wb") as f: | ||
| f.write( | ||
| b'\x89PNG\r\n\x1a\n' | ||
| b'\x00\x00\x00\rIHDR\x00\x00\x00\x01' | ||
| b'\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde' | ||
| b'\x00\x00\x00\x0cIDATx' | ||
| b'\x9cc\xf8\x0f\x00\x00\x01\x01\x00\x05' | ||
| b'\x18\xd8N\x00\x00\x00\x00IEND\xaeB`\x82' | ||
| ) | ||
| url = env.mkurl("http", "test1", "/pixel.png") | ||
| r = env.curl_get(url, options=[ | ||
| "-H", "Accept-Encoding: br", | ||
| ]) | ||
| assert r.response, "no response" | ||
| assert r.response["status"] == 200 | ||
| assert r.response["header"].get("content-encoding") != "br", \ | ||
| "binary content should not be brotli-compressed" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would keep the
brotlichange present in this file too.