File: holodeck.py

package info (click to toggle)
python-twilio 9.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,756 kB
  • sloc: python: 8,281; makefile: 65
file content (89 lines) | stat: -rw-r--r-- 2,709 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
from twilio.base.exceptions import TwilioRestException
from twilio.http import HttpClient
from twilio.http.request import Request
import platform
from twilio import __version__


class Hologram(object):
    def __init__(self, request, response):
        self.request = request
        self.response = response


class Holodeck(HttpClient):
    def __init__(self):
        self._holograms = []
        self._requests = []

    def mock(self, response, request=None):
        request = request or Request()
        self._holograms.append(Hologram(request, response))

    @property
    def requests(self):
        return self._requests

    def add_standard_headers(self, request):
        standard_headers = {
            "User-Agent": "twilio-python/{} ({} {}) Python/{}".format(
                __version__,
                platform.system(),
                platform.machine(),
                platform.python_version(),
            ),
            "X-Twilio-Client": "python-{}".format(__version__),
            "Accept": "application/json",
            "Accept-Charset": "utf-8",
        }

        if request.method == "POST" and "Content-Type" not in standard_headers:
            standard_headers["Content-Type"] = "application/x-www-form-urlencoded"

        standard_headers.update(request.headers)
        request.headers = standard_headers
        return request

    def assert_has_request(self, request):
        for req in self.requests:
            if req == request or req == self.add_standard_headers(request):
                return

        message = "\nHolodeck never received a request matching: \n + {}\n".format(
            request
        )
        if self._requests:
            message += "Requests received:\n"
            message += "\n".join(" * {}".format(r) for r in self.requests)
        else:
            message += "No Requests received"

        raise AssertionError(message)

    def request(
        self,
        method,
        url,
        params=None,
        data=None,
        headers=None,
        auth=None,
        timeout=None,
        allow_redirects=False,
    ):
        request = Request(method, url, auth, params, data, headers)

        self._requests.append(request)

        for hologram in self._holograms:
            if hologram.request == request:
                return hologram.response

        message = "\nHolodeck has no hologram for: {}\n".format(request)
        if self._holograms:
            message += "Holograms loaded:\n"
            message += "\n".join(" - {}".format(h.request) for h in self._holograms)
        else:
            message += "No Holograms loaded"

        raise TwilioRestException(404, url, message, method=method)