File: urllib_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 (47 lines) | stat: -rw-r--r-- 1,304 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
from urllib.error import HTTPError
from urllib.request import Request, urlopen
from http.client import HTTPResponse

import pytest

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


class TestUrllib(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

        request = Request(
            url=url,
            method=method,
            data=content,
            headers=req_headers,
        )
        try:
            response: HTTPResponse = urlopen(request)
            return response.status, response.read(), response.headers
        except HTTPError as e:
            return e.code, e.msg


@pytest.mark.pook
def test_urllib_ssl():
    pook.get("https://example.com").reply(200).body("Hello from pook")
    res = urlopen("https://example.com")

    assert res.read() == b"Hello from pook"


@pytest.mark.pook
def test_urllib_clear():
    pook.get("http://example.com").reply(200).body("Hello from pook")
    res = urlopen("http://example.com")

    assert res.read() == b"Hello from pook"