Skip to content
Myr

test_decoders.py

This file

Path

tests/test_decoders.py

88 lines · 2.7 KiB · Python

Last changed by

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

History of this file

Take it

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 1–5why, one step back
1import zlib
23import brotli4import pytest5

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 6why, one step back
6import httpx

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 7–14why, one step back
7
89def test_deflate():10 body = b"test 123"11 compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)12 compressed_body = compressor.compress(body) + compressor.flush()1314 headers = [(b"Content-Encoding", b"deflate")]

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 15why, one step back
15 response = httpx.Response(200, headers=headers, content=compressed_body)

Response.contentTom Christiebefore adoption4c55113

line 16why, one step back
16 assert response.content == body

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 17–24why, one step back
17
1819def test_gzip():20 body = b"test 123"21 compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)22 compressed_body = compressor.compress(body) + compressor.flush()2324 headers = [(b"Content-Encoding", b"gzip")]

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 25why, one step back
25 response = httpx.Response(200, headers=headers, content=compressed_body)

Response.contentTom Christiebefore adoption4c55113

line 26why, one step back
26 assert response.content == body

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 27–33why, one step back
27
2829def test_brotli():30 body = b"test 123"31 compressed_body = brotli.compress(body)3233 headers = [(b"Content-Encoding", b"br")]

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 34why, one step back
34 response = httpx.Response(200, headers=headers, content=compressed_body)

Response.contentTom Christiebefore adoption4c55113

line 35why, one step back
35 assert response.content == body

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 36–49why, one step back
36
3738def test_multi():39 body = b"test 123"4041 deflate_compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)42 compressed_body = deflate_compressor.compress(body) + deflate_compressor.flush()4344 gzip_compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)45 compressed_body = (46 gzip_compressor.compress(compressed_body) + gzip_compressor.flush()47 )4849 headers = [(b"Content-Encoding", b"deflate, gzip")]

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 50why, one step back
50 response = httpx.Response(200, headers=headers, content=compressed_body)

Response.contentTom Christiebefore adoption4c55113

line 51why, one step back
51 assert response.content == body

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 52–58why, one step back
52
5354def test_multi_with_identity():55 body = b"test 123"56 compressed_body = brotli.compress(body)5758 headers = [(b"Content-Encoding", b"br, identity")]

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 59why, one step back
59 response = httpx.Response(200, headers=headers, content=compressed_body)

Response.contentTom Christiebefore adoption4c55113

line 60why, one step back
60 assert response.content == body

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 61–62why, one step back
61
62 headers = [(b"Content-Encoding", b"identity, br")]

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 63why, one step back
63 response = httpx.Response(200, headers=headers, content=compressed_body)

Response.contentTom Christiebefore adoption4c55113

line 64why, one step back
64 assert response.content == body

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 65–66why, one step back
65
66

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 67why, one step back
67def test_streaming():

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 68–70why, one step back
68 body = b"test 123"
69 compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)70

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 71why, one step back
71 def compress(body):

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 72–75why, one step back
72 yield compressor.compress(body)
73 yield compressor.flush()7475 headers = [(b"Content-Encoding", b"gzip")]

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

line 76why, one step back
76 response = httpx.Response(200, headers=headers, content=compress(body))

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 77why, one step back
77 assert not hasattr(response, "body")

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 78why, one step back
78 assert response.read() == body

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 79–85why, one step back
79
8081@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))82def test_decoding_errors(header_value):83 headers = [(b"Content-Encoding", header_value)]84 body = b"test 123"85 compressed_body = brotli.compress(body)[3:]

Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b

lines 86–87why, one step back
86 with pytest.raises(httpx.exceptions.DecodingError):
87 response = httpx.Response(200, headers=headers, content=compressed_body)

Response.contentTom Christiebefore adoption4c55113

line 88why, one step back
88 response.content