File: test_requests_universal.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (68 lines) | stat: -rw-r--r-- 2,773 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
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See LICENSE.txt in the project root for
# license information.
# -------------------------------------------------------------------------
import concurrent.futures
import requests.utils

from corehttp.rest import HttpRequest
from corehttp.transport.requests import RequestsTransport
from corehttp.rest._requests_basic import RestRequestsTransportResponse
from utils import create_transport_response, SYNC_TRANSPORT_RESPONSES


def test_threading_basic_requests():
    # Basic should have the session for all threads, it's why it's not recommended
    sender = RequestsTransport()
    main_thread_session = sender.session

    def thread_body(local_sender):
        # Should be the same session - we are no longer managing a session per thread.
        assert local_sender.session is main_thread_session

        return True

    with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
        future = executor.submit(thread_body, sender)
        assert future.result()


def test_requests_auto_headers(port):
    request = HttpRequest("POST", "http://localhost:{}/basic/string".format(port))
    with RequestsTransport() as sender:
        response = sender.send(request)
        auto_headers = response._internal_response.request.headers
        assert "Content-Type" not in auto_headers


def _create_requests_response(http_response, body_bytes, headers=None):
    # https://github.com/psf/requests/blob/67a7b2e8336951d527e223429672354989384197/requests/adapters.py#L255
    req_response = requests.Response()
    req_response._content = body_bytes
    req_response._content_consumed = True
    req_response.status_code = 200
    req_response.reason = "OK"
    if headers:
        # req_response.headers is type CaseInsensitiveDict
        req_response.headers.update(headers)
    req_response.encoding = requests.utils.get_encoding_from_headers(req_response.headers)

    response = create_transport_response(http_response, None, req_response)  # Don't need a request here

    return response


def test_requests_response_text():
    for encoding in ["utf-8", "utf-8-sig", None]:
        res = _create_requests_response(
            RestRequestsTransportResponse, b"\xef\xbb\xbf56", {"Content-Type": "text/plain"}
        )
        res.read()
        assert res.text(encoding) == "56", "Encoding {} didn't work".format(encoding)


def test_repr():
    res = _create_requests_response(RestRequestsTransportResponse, b"\xef\xbb\xbf56", {"Content-Type": "text/plain"})
    class_name = "HttpResponse"
    assert repr(res) == "<{}: 200 OK, Content-Type: text/plain>".format(class_name)