-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy path__init__.py
More file actions
142 lines (111 loc) · 3.58 KB
/
__init__.py
File metadata and controls
142 lines (111 loc) · 3.58 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from typing import Any, Dict, Optional
from mocket import mocketize
from mocket.async_mocket import async_mocketize
from mocket.compat import ENCODING
from mocket.mocket import Mocket
from mocket.mockhttp import Entry as MocketHttpEntry
from mocket.mockhttp import Request as MocketHttpRequest
from mocket.mockhttp import Response as MocketHttpResponse
def httprettifier_headers(headers: Dict[str, str]) -> Dict[str, str]:
return {k.lower().replace("_", "-"): v for k, v in headers.items()}
class Request(MocketHttpRequest):
@property
def body(self) -> bytes:
return super().body.encode(ENCODING) # type: ignore[no-any-return]
@property
def headers(self) -> Dict[str, str]:
return httprettifier_headers(super().headers)
class Response(MocketHttpResponse):
headers: Dict[str, str]
def get_protocol_data(self, str_format_fun_name: str = "lower") -> bytes:
if "server" in self.headers and self.headers["server"] == "Python/Mocket":
self.headers["server"] = "Python/HTTPretty"
return super().get_protocol_data(str_format_fun_name=str_format_fun_name) # type: ignore[no-any-return]
def set_base_headers(self) -> None:
super().set_base_headers()
self.headers = httprettifier_headers(self.headers)
original_set_base_headers = set_base_headers
def set_extra_headers(self, headers: Dict[str, str]) -> None:
self.headers.update(headers)
class Entry(MocketHttpEntry):
request_cls = Request
response_cls = Response
activate = mocketize
httprettified = mocketize
async_httprettified = async_mocketize
enable = Mocket.enable
disable = Mocket.disable
reset = Mocket.reset
GET = Entry.GET
PUT = Entry.PUT
POST = Entry.POST
DELETE = Entry.DELETE
HEAD = Entry.HEAD
PATCH = Entry.PATCH
OPTIONS = Entry.OPTIONS
def register_uri(
method: str,
uri: str,
body: str = "HTTPretty :)",
adding_headers: Optional[Dict[str, str]] = None,
forcing_headers: Optional[Dict[str, str]] = None,
status: int = 200,
responses: Any = None,
match_querystring: bool = False,
priority: int = 0,
**headers: str,
) -> None:
headers = httprettifier_headers(headers)
if adding_headers is not None:
headers.update(httprettifier_headers(adding_headers))
if forcing_headers is not None:
def force_headers(self):
self.headers = httprettifier_headers(forcing_headers)
Response.set_base_headers = force_headers # type: ignore[method-assign]
else:
Response.set_base_headers = Response.original_set_base_headers # type: ignore[method-assign]
if responses:
Entry.register(
method,
uri,
*responses,
match_querystring=match_querystring,
)
else:
Entry.single_register(
method,
uri,
body=body,
status=status,
headers=headers,
match_querystring=match_querystring,
)
class MocketHTTPretty:
Response = Response
def __getattr__(self, name):
if name == "last_request":
return Mocket.last_request()
if name == "latest_requests":
return Mocket.request_list()
return getattr(Entry, name)
HTTPretty = MocketHTTPretty()
HTTPretty.register_uri = register_uri # type: ignore[attr-defined]
httpretty = HTTPretty
__all__ = (
"HTTPretty",
"httpretty",
"activate",
"async_httprettified",
"httprettified",
"enable",
"disable",
"reset",
"Response",
"GET",
"PUT",
"POST",
"DELETE",
"HEAD",
"PATCH",
"register_uri",
)