File: test_client.py

package info (click to toggle)
hishel 0.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,548 kB
  • sloc: python: 6,387; sh: 13; makefile: 4
file content (248 lines) | stat: -rw-r--r-- 8,676 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import os
from datetime import datetime, timedelta
from pathlib import Path
from time import mktime
from wsgiref.handlers import format_date_time

import httpx
import pytest
from httpcore import Request

import hishel
from hishel._utils import generate_key

date_header = format_date_time(mktime((datetime.now() - timedelta(hours=2)).timetuple()))



def test_client_301():
    with hishel.MockTransport() as transport:
        transport.add_responses([httpx.Response(301, headers=[(b"Location", b"https://example.com")])])
        with hishel.CacheClient(transport=transport, storage=hishel.InMemoryStorage()) as client:
            client.request(
                "GET",
                "https://www.example.com",
            )
            response = client.request(
                "GET",
                "https://www.example.com",
            )
            assert response.extensions["from_cache"]



def test_empty_cachefile_handling(use_temp_dir):
    with hishel.MockTransport() as transport:
        transport.add_responses(
            [
                httpx.Response(
                    status_code=200,
                    headers=[
                        ("Cache-Control", "public, max-age=86400, s-maxage=86400"),
                        ("Date", date_header),
                    ],
                    text="test",
                )
                for _ in range(2)
            ]
        )

        with hishel.CacheClient(storage=hishel.FileStorage(), transport=transport) as client:
            request = Request(b"GET", "https://example.com/")
            key = generate_key(request)
            filedir = Path(os.getcwd() + "/.cache/hishel/" + key)

            client.get("https://example.com/")
            response = client.get("https://example.com/")

            assert response.status_code == 200
            assert response.text == "test"
            assert response.extensions["from_cache"]

            with open(filedir, "w+", encoding="utf-8") as file:
                file.truncate(0)
            assert os.path.getsize(filedir) == 0

            response = client.get("https://example.com/")
            assert response.status_code == 200
            assert response.text == "test"
            assert response.extensions["from_cache"] is False

            response = client.get("https://example.com/")
            assert response.extensions["from_cache"]



def test_post_caching():
    with hishel.MockTransport() as transport:
        transport.add_responses(
            [
                httpx.Response(
                    status_code=200,
                    headers=[
                        ("Cache-Control", "public, max-age=86400, s-maxage=86400"),
                        ("Date", date_header),
                    ],
                    text=f"test-{idx}",
                )
                for idx in range(2)
            ]
        )

        with hishel.CacheClient(
            storage=hishel.InMemoryStorage(),
            transport=transport,
            controller=hishel.Controller(cacheable_methods=["POST"]),
        ) as client:
            # Create cache file.
            response = client.post("https://example.com", json={"test": 1})
            assert response.status_code == 200
            assert not response.extensions["from_cache"]
            assert response.text == "test-0"

            # Get from cache file.
            response = client.post("https://example.com", json={"test": 1})
            assert response.status_code == 200
            assert response.extensions["from_cache"]
            assert response.text == "test-0"

            # Create a new cache file
            response = client.post("https://example.com", json={"test": 2})
            assert response.status_code == 200
            assert not response.extensions["from_cache"]
            assert response.text == "test-1"

            # Take second response from cache
            response = client.post("https://example.com", json={"test": 2})
            assert response.status_code == 200
            assert response.extensions["from_cache"]
            assert response.text == "test-1"

            # Check on first response
            response = client.post("https://example.com", json={"test": 1})
            assert response.status_code == 200
            assert response.extensions["from_cache"]
            assert response.text == "test-0"



def test_client_get():
    with hishel.MockTransport() as transport:
        transport.add_responses(
            [
                httpx.Response(
                    status_code=200,
                    headers=[
                        ("Cache-Control", "public, max-age=86400, s-maxage=86400"),
                        ("Date", date_header),
                    ],
                    text="test text",
                )
            ]
        )

        with hishel.CacheClient(storage=hishel.InMemoryStorage(), transport=transport) as client:
            response = client.get("https://example.com")
            assert response.status_code == 200
            assert not response.extensions["from_cache"]
            assert response.text == "test text"

            response = client.get("https://example.com")
            assert response.status_code == 200
            assert response.extensions["from_cache"]
            assert response.text == "test text"



def test_client_head():
    with hishel.MockTransport() as transport:
        transport.add_responses(
            [
                httpx.Response(
                    status_code=200,
                    headers=[
                        ("Cache-Control", "public, max-age=86400, s-maxage=86400"),
                        ("Date", date_header),
                    ],
                )
                for _ in range(2)
            ]
        )

        with hishel.CacheClient(storage=hishel.InMemoryStorage(), transport=transport) as client:
            response = client.head("https://example.com")
            assert response.status_code == 200
            assert not response.extensions["from_cache"]

            response = client.head("https://example.com")
            assert response.status_code == 200
            assert not response.extensions["from_cache"]



def test_force_cache():
    with hishel.MockTransport() as transport:
        transport.add_responses(
            [
                httpx.Response(
                    status_code=200,
                    headers=[
                        ("Cache-Control", "no-store"),
                        ("Date", date_header),
                    ],
                )
                for _ in range(3)
            ]
        )

        with hishel.CacheClient(
            storage=hishel.InMemoryStorage(),
            controller=hishel.Controller(cacheable_methods=["HEAD"]),
            transport=transport,
        ) as client:
            response = client.head("https://example.com")
            assert response.status_code == 200
            assert not response.extensions["from_cache"]

            # Check that "no-store" is respected
            response = client.head("https://example.com")
            assert response.status_code == 200
            assert not response.extensions["from_cache"]

            response = client.head("https://example.com", extensions={"force_cache": True})
            assert response.status_code == 200
            assert not response.extensions["from_cache"]

            response = client.head("https://example.com", extensions={"force_cache": True})
            assert response.status_code == 200
            assert response.extensions["from_cache"]



def test_cache_disabled():
    with hishel.MockTransport() as transport:
        transport.add_responses(
            [
                httpx.Response(
                    status_code=200,
                    headers=[
                        ("Cache-Control", "public, max-age=86400, s-maxage=86400"),
                        ("Date", date_header),
                    ],
                )
                for _ in range(2)
            ]
        )

        with hishel.CacheClient(storage=hishel.InMemoryStorage(), transport=transport) as client:
            response = client.get(
                "https://www.example.com/cacheable-endpoint", extensions={"cache_disabled": True}
            )
            assert response.status_code == 200
            assert not response.extensions["from_cache"]

            response = client.get(
                "https://www.example.com/cacheable-endpoint", extensions={"cache_disabled": True}
            )
            assert response.status_code == 200
            assert not response.extensions["from_cache"]