File: test_http_aiohttp.py

package info (click to toggle)
python-elastic-transport 9.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 644 kB
  • sloc: python: 6,652; makefile: 18
file content (404 lines) | stat: -rw-r--r-- 12,971 bytes parent folder | download
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#  Licensed to Elasticsearch B.V. under one or more contributor
#  license agreements. See the NOTICE file distributed with
#  this work for additional information regarding copyright
#  ownership. Elasticsearch B.V. licenses this file to you under
#  the Apache License, Version 2.0 (the "License"); you may
#  not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
# 	http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing,
#  software distributed under the License is distributed on an
#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#  KIND, either express or implied.  See the License for the
#  specific language governing permissions and limitations
#  under the License.

import gzip
import json
import warnings

import aiohttp
import pytest
from multidict import CIMultiDict

from elastic_transport import AiohttpHttpNode, NodeConfig
from elastic_transport._node._base import DEFAULT_USER_AGENT


class TestAiohttpHttpNode:
    @pytest.mark.asyncio
    async def _get_mock_node(self, node_config, response_body=b"{}"):
        node = AiohttpHttpNode(node_config)
        node._create_aiohttp_session()

        def _dummy_request(*args, **kwargs):
            class DummyResponse:
                async def __aenter__(self, *_, **__):
                    return self

                async def __aexit__(self, *_, **__):
                    pass

                async def read(self):
                    return response_body if args[0] != "HEAD" else b""

                async def release(self):
                    return None

            dummy_response = DummyResponse()
            dummy_response.headers = CIMultiDict()
            dummy_response.status = 200
            _dummy_request.call_args = (args, kwargs)
            return dummy_response

        node.session.request = _dummy_request
        return node

    @pytest.mark.asyncio
    async def test_aiohttp_options(self):
        node = await self._get_mock_node(
            NodeConfig(scheme="http", host="localhost", port=80)
        )
        await node.perform_request(
            "GET",
            "/index",
            body=b"hello, world!",
            headers={"key": "value"},
        )

        args, kwargs = node.session.request.call_args
        assert args == ("GET", "http://localhost:80/index")
        assert kwargs == {
            "data": b"hello, world!",
            "headers": {
                "connection": "keep-alive",
                "key": "value",
                "user-agent": DEFAULT_USER_AGENT,
            },
            "timeout": aiohttp.ClientTimeout(
                total=10,
                connect=None,
                sock_read=None,
                sock_connect=None,
            ),
        }

    @pytest.mark.asyncio
    async def test_aiohttp_options_fingerprint(self):
        node = await self._get_mock_node(
            NodeConfig(
                scheme="https",
                host="localhost",
                port=443,
                ssl_assert_fingerprint=("00:" * 32).strip(":"),
            )
        )
        await node.perform_request(
            "GET",
            "/",
        )

        args, kwargs = node.session.request.call_args
        assert args == ("GET", "https://localhost:443/")

        # aiohttp.Fingerprint() doesn't define equality
        fingerprint: aiohttp.Fingerprint = kwargs.pop("ssl")
        assert fingerprint.fingerprint == b"\x00" * 32

        assert kwargs == {
            "data": None,
            "headers": {"connection": "keep-alive", "user-agent": DEFAULT_USER_AGENT},
            "timeout": aiohttp.ClientTimeout(
                total=10,
                connect=None,
                sock_read=None,
                sock_connect=None,
            ),
        }

    @pytest.mark.parametrize(
        "options",
        [(5, 5, 5), (None, 5, 5), (5, None, 0), (None, None, 0), (5, 5), (None, 0)],
    )
    @pytest.mark.asyncio
    async def test_aiohttp_options_timeout(self, options):
        if len(options) == 3:
            constructor_timeout, request_timeout, aiohttp_timeout = options
            node = await self._get_mock_node(
                NodeConfig(
                    scheme="http",
                    host="localhost",
                    port=80,
                    request_timeout=constructor_timeout,
                )
            )
        else:
            request_timeout, aiohttp_timeout = options
            node = await self._get_mock_node(
                NodeConfig(scheme="http", host="localhost", port=80)
            )

        await node.perform_request(
            "GET",
            "/",
            request_timeout=request_timeout,
        )

        args, kwargs = node.session.request.call_args
        assert args == ("GET", "http://localhost:80/")
        assert kwargs == {
            "data": None,
            "headers": {"connection": "keep-alive", "user-agent": DEFAULT_USER_AGENT},
            "timeout": aiohttp.ClientTimeout(
                total=aiohttp_timeout,
                connect=None,
                sock_read=None,
                sock_connect=None,
            ),
        }

    @pytest.mark.asyncio
    async def test_http_compression(self):
        node = await self._get_mock_node(
            NodeConfig(scheme="http", host="localhost", port=80, http_compress=True)
        )

        # 'content-encoding' shouldn't be set at a session level.
        # Should be applied only if the request is sent with a body.
        assert "content-encoding" not in node.session.headers

        await node.perform_request("GET", "/", body=b"{}")

        args, kwargs = node.session.request.call_args
        assert kwargs["headers"] == {
            "accept-encoding": "gzip",
            "connection": "keep-alive",
            "content-encoding": "gzip",
            "user-agent": DEFAULT_USER_AGENT,
        }
        assert gzip.decompress(kwargs["data"]) == b"{}"

    @pytest.mark.parametrize("http_compress", [None, False])
    @pytest.mark.asyncio
    async def test_no_http_compression(self, http_compress):
        node = await self._get_mock_node(
            NodeConfig(
                scheme="http", host="localhost", port=80, http_compress=http_compress
            )
        )

        assert "content-encoding" not in node.session.headers

        await node.perform_request("GET", "/", body=b"{}")

        args, kwargs = node.session.request.call_args
        assert kwargs["headers"] == {
            "connection": "keep-alive",
            "user-agent": DEFAULT_USER_AGENT,
        }
        assert kwargs["data"] == b"{}"

    @pytest.mark.parametrize("path_prefix", ["url", "/url"])
    @pytest.mark.asyncio
    async def test_uses_https_if_verify_certs_is_off(self, path_prefix):
        with warnings.catch_warnings(record=True) as w:
            await self._get_mock_node(
                NodeConfig(
                    scheme="https",
                    host="localhost",
                    port=443,
                    path_prefix=path_prefix,
                    verify_certs=False,
                )
            )

        assert 1 == len(w)
        assert (
            "Connecting to 'https://localhost:443/url' using TLS with verify_certs=False is insecure"
            == str(w[0].message)
        )

    @pytest.mark.asyncio
    async def test_uses_https_if_verify_certs_is_off_no_show_warning(self):
        with warnings.catch_warnings(record=True) as w:
            node = await self._get_mock_node(
                NodeConfig(
                    scheme="https",
                    host="localhost",
                    port=443,
                    path_prefix="url",
                    ssl_show_warn=False,
                )
            )
            await node.perform_request("GET", "/")

        assert w == []

    @pytest.mark.asyncio
    async def test_merge_headers(self):
        node = await self._get_mock_node(
            NodeConfig(
                scheme="https",
                host="localhost",
                port=443,
                headers={"h1": "v1", "h2": "v2"},
            )
        )
        resp, _ = await node.perform_request(
            "GET", "/", headers={"H2": "v2p", "H3": "v3"}
        )

        args, kwargs = node.session.request.call_args
        assert args == ("GET", "https://localhost:443/")
        assert kwargs["headers"] == {
            "connection": "keep-alive",
            "h1": "v1",
            "h2": "v2p",
            "h3": "v3",
            "user-agent": DEFAULT_USER_AGENT,
        }

    @pytest.mark.parametrize("aiohttp_fixed_head_bug", [True, False])
    @pytest.mark.asyncio
    async def test_head_workaround(self, aiohttp_fixed_head_bug):
        from elastic_transport._node import _http_aiohttp

        prev = _http_aiohttp._AIOHTTP_FIXED_HEAD_BUG
        try:
            _http_aiohttp._AIOHTTP_FIXED_HEAD_BUG = aiohttp_fixed_head_bug

            node = await self._get_mock_node(
                NodeConfig(
                    scheme="https",
                    host="localhost",
                    port=443,
                )
            )
            resp, data = await node.perform_request("HEAD", "/anything")

            method, url = node.session.request.call_args[0]
            assert method == "HEAD" if aiohttp_fixed_head_bug else "GET"
            assert url == "https://localhost:443/anything"

            assert resp.status == 200
            assert data == b""

        finally:
            _http_aiohttp._AIOHTTP_FIXED_HEAD_BUG = prev


@pytest.mark.asyncio
async def test_ssl_assert_fingerprint(cert_fingerprint, httpbin_secure):
    with warnings.catch_warnings(record=True) as w:
        node = AiohttpHttpNode(
            NodeConfig(
                scheme="https",
                host=httpbin_secure.host,
                port=httpbin_secure.port,
                ssl_assert_fingerprint=cert_fingerprint,
            )
        )
        resp, _ = await node.perform_request("GET", "/")

    assert resp.status == 200
    assert [str(x.message) for x in w if x.category != DeprecationWarning] == []


@pytest.mark.asyncio
async def test_default_headers(httpbin):
    node = AiohttpHttpNode(
        NodeConfig(scheme="http", host=httpbin.host, port=httpbin.port)
    )
    resp, data = await node.perform_request("GET", "/anything")

    assert resp.status == 200
    headers = json.loads(data)["headers"]
    headers.pop("X-Amzn-Trace-Id", None)
    assert headers == {
        "Connection": "keep-alive",
        "Host": f"{httpbin.host}:{httpbin.port}",
        "User-Agent": DEFAULT_USER_AGENT,
    }


@pytest.mark.asyncio
async def test_custom_headers(httpbin):
    node = AiohttpHttpNode(
        NodeConfig(
            scheme="http",
            host=httpbin.host,
            port=httpbin.port,
            headers={"accept-encoding": "gzip", "Content-Type": "application/json"},
        )
    )
    resp, data = await node.perform_request(
        "GET",
        "/anything",
        headers={
            "conTent-type": "application/x-ndjson",
            "user-agent": "custom-agent/1.2.3",
        },
    )

    assert resp.status == 200
    headers = json.loads(data)["headers"]
    headers.pop("X-Amzn-Trace-Id", None)
    assert headers == {
        "Accept-Encoding": "gzip",
        "Connection": "keep-alive",
        "Content-Type": "application/x-ndjson",
        "Host": f"{httpbin.host}:{httpbin.port}",
        "User-Agent": "custom-agent/1.2.3",
    }


@pytest.mark.asyncio
async def test_custom_user_agent(httpbin):
    node = AiohttpHttpNode(
        NodeConfig(
            scheme="http",
            host=httpbin.host,
            port=httpbin.port,
            headers={
                "accept-encoding": "gzip",
                "Content-Type": "application/json",
                "user-agent": "custom-agent/1.2.3",
            },
        )
    )
    resp, data = await node.perform_request(
        "GET",
        "/anything",
    )

    assert resp.status == 200
    headers = json.loads(data)["headers"]
    headers.pop("X-Amzn-Trace-Id", None)
    assert headers == {
        "Accept-Encoding": "gzip",
        "Connection": "keep-alive",
        "Content-Type": "application/json",
        "Host": f"{httpbin.host}:{httpbin.port}",
        "User-Agent": "custom-agent/1.2.3",
    }


def test_repr():
    node = AiohttpHttpNode(NodeConfig(scheme="https", host="localhost", port=443))
    assert "<AiohttpHttpNode(https://localhost:443)>" == repr(node)


@pytest.mark.asyncio
async def test_head(httpbin):
    node = AiohttpHttpNode(
        NodeConfig(
            scheme="http", host=httpbin.host, port=httpbin.port, http_compress=True
        )
    )
    resp, data = await node.perform_request("HEAD", "/anything")

    assert resp.status == 200
    assert resp.headers["content-type"] == "application/json"
    assert data == b""