File: test_transport_plugin.py

package info (click to toggle)
httpie 3.2.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,904 kB
  • sloc: python: 13,760; xml: 162; makefile: 141; ruby: 79; sh: 32
file content (45 lines) | stat: -rw-r--r-- 1,229 bytes parent folder | download | duplicates (3)
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
from io import BytesIO

from requests.adapters import BaseAdapter
from requests.models import Response
from requests.utils import get_encoding_from_headers

from httpie.plugins import TransportPlugin
from httpie.plugins.registry import plugin_manager

from .utils import HTTP_OK, http

SCHEME = 'http+fake'


class FakeAdapter(BaseAdapter):
    def send(self, request, **kwargs):
        response = Response()
        response.status_code = 200
        response.reason = 'OK'
        response.headers = {
            'Content-Type': 'text/html; charset=UTF-8',
        }
        response.encoding = get_encoding_from_headers(response.headers)
        response.raw = BytesIO(b'<!doctype html><html>Hello</html>')
        return response


class FakeTransportPlugin(TransportPlugin):
    name = 'Fake Transport'

    prefix = SCHEME

    def get_adapter(self):
        return FakeAdapter()


def test_transport_from_requests_response(httpbin):
    plugin_manager.register(FakeTransportPlugin)
    try:
        r = http(f'{SCHEME}://example.com')
        assert HTTP_OK in r
        assert 'Hello' in r
        assert 'Content-Type: text/html; charset=UTF-8' in r
    finally:
        plugin_manager.unregister(FakeTransportPlugin)