File: urllib3_test.py

package info (click to toggle)
python-pook 2.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 672 kB
  • sloc: python: 3,558; makefile: 13
file content (113 lines) | stat: -rw-r--r-- 3,062 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
import pytest
import urllib3
import requests

import pook
from tests.unit.fixtures import BINARY_FILE
from tests.unit.interceptors.base import StandardTests


class TestStandardUrllib3(StandardTests):
    def make_request(self, method, url, content=None, headers=None):
        req_headers = {}
        if headers:
            for header, value in headers:
                if header in req_headers:
                    req_headers[header] += f", {value}"
                else:
                    req_headers[header] = value

        http = urllib3.PoolManager()
        response = http.request(method, url, content, headers=req_headers)
        return response.status, response.read(), response.headers


class TestStandardRequests(StandardTests):
    def make_request(self, method, url, content=None, headers=None):
        req_headers = {}
        if headers:
            for header, value in headers:
                if header in req_headers:
                    req_headers[header] += f", {value}"
                else:
                    req_headers[header] = value

        response = requests.request(method, url, data=content, headers=req_headers)
        return response.status_code, response.content, response.headers


@pytest.fixture
def URL(httpbin):
    return f"{httpbin.url}/foo"


@pook.on
def assert_chunked_response(URL, input_data, expected):
    (pook.get(URL).reply(201).body(input_data, chunked=True))

    http = urllib3.PoolManager()
    r = http.request("GET", URL)

    assert r.status == 201

    chunks = list(r.read_chunked())
    assert chunks == expected


def test_chunked_response_list(URL):
    assert_chunked_response(URL, ["a", "b", "c"], [b"a", b"b", b"c"])


def test_chunked_response_str(URL):
    assert_chunked_response(URL, "text", [b"text"])


def test_chunked_response_byte(URL):
    assert_chunked_response(URL, b"byteman", [b"byteman"])


def test_chunked_response_empty(URL):
    assert_chunked_response(URL, "", [])


def test_chunked_response_contains_newline(URL):
    assert_chunked_response(URL, "newline\r\n", [b"newline\r\n"])


def test_activate_disable():
    original = urllib3.connectionpool.HTTPConnectionPool.urlopen

    interceptor = pook.interceptors.Urllib3Interceptor(pook.MockEngine)
    interceptor.activate()
    interceptor.disable()

    assert urllib3.connectionpool.HTTPConnectionPool.urlopen == original


@pook.on
def test_binary_body(URL):
    (pook.get(URL).reply(200).body(BINARY_FILE))

    http = urllib3.PoolManager()
    r = http.request("GET", URL)

    assert r.read() == BINARY_FILE


@pook.on
def test_binary_body_chunked(URL):
    (pook.get(URL).reply(200).body(BINARY_FILE, chunked=True))

    http = urllib3.PoolManager()
    r = http.request("GET", URL)

    assert list(r.read_chunked()) == [BINARY_FILE]


@pytest.mark.pook
def test_post_with_headers(URL):
    mock = pook.post(URL).header("k", "v").reply(200).mock
    http = urllib3.PoolManager(headers={"k": "v"})
    resp = http.request("POST", URL)
    assert resp.status == 200
    assert len(mock.matches) == 1