fresco 3.3.1__py3-none-any.whl → 3.3.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of fresco might be problematic. Click here for more details.

@@ -0,0 +1,40 @@
1
+ # Copyright 2015 Oliver Cope
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+ from fresco.decorators import json_response
16
+
17
+
18
+ class TestJSONResponse(object):
19
+ def test_it_allows_custom_formatting(self):
20
+ r = json_response({"l": [1, 2, 3]}, indent=1, separators=(", ", ": "))
21
+ assert list(r.content_iterator) == [b'{\n "l": [\n 1, \n 2, \n 3\n ]\n}']
22
+
23
+ def test_it_acts_as_a_filter(self):
24
+ r = json_response({"l": [1, 2, 3]})
25
+ assert list(r.content_iterator) == [b'{"l":[1,2,3]}']
26
+ assert r.get_header("content-type") == "application/json"
27
+
28
+ def test_it_acts_as_a_decorator(self):
29
+ @json_response()
30
+ def f():
31
+ return {"l": [1, 2, 3]}
32
+
33
+ assert list(f().content_iterator) == [b'{"l":[1,2,3]}']
34
+
35
+ def test_it_acts_as_a_decorator_without_arguments(self):
36
+ @json_response
37
+ def f():
38
+ return {"l": [1]}
39
+
40
+ assert list(f().content_iterator) == [b'{"l":[1]}']
@@ -0,0 +1,30 @@
1
+ from fresco import FrescoApp
2
+ from fresco.routing import GET
3
+ from fresco.exceptions import BadRequest, RedirectTemporary
4
+
5
+
6
+ class TestResponseExceptions(object):
7
+ def test_exception_is_converted_to_response(self):
8
+ def redirector():
9
+ raise RedirectTemporary("/foo")
10
+
11
+ app = FrescoApp()
12
+ app.route("/", GET, redirector)
13
+
14
+ with app.requestcontext("/"):
15
+ assert app.view().status_code == 302
16
+
17
+ def test_exception_can_have_its_response_modified(self):
18
+ def view():
19
+ raise BadRequest(
20
+ content="custom error message", content_type="application/foo"
21
+ )
22
+
23
+ app = FrescoApp()
24
+ app.route("/", GET, view)
25
+
26
+ with app.requestcontext("/"):
27
+ response = app.view()
28
+ assert response.status_code == 400
29
+ assert "".join(response.content) == "custom error message"
30
+ assert response.get_header("Content-Type") == "application/foo"
@@ -0,0 +1,92 @@
1
+ # Copyright 2015 Oliver Cope
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+ from fresco import FrescoApp
16
+ from fresco import context
17
+ from fresco.middleware import XForwarded
18
+ from fresco.response import Response
19
+ from fresco.routing import GET
20
+
21
+
22
+ class TestXForwarded(object):
23
+ def get_app(self, *args, **kwargs):
24
+ app = FrescoApp()
25
+
26
+ @app.route("/", GET)
27
+ def view():
28
+ request = context.request
29
+ return Response([request.url, request.environ["REMOTE_ADDR"]])
30
+
31
+ app.add_middleware(XForwarded, *args, **kwargs)
32
+ return app
33
+
34
+ def test_forwards_x_forwarded_for(self):
35
+ app = self.get_app()
36
+ with app.requestcontext(
37
+ "/", REMOTE_ADDR="127.0.0.1", HTTP_X_FORWARDED_FOR="1.2.3.4"
38
+ ):
39
+ url, addr = app.view().content_iterator
40
+ assert addr == b"1.2.3.4"
41
+
42
+ def test_forwards_x_forwarded_host(self):
43
+ app = self.get_app()
44
+ with app.requestcontext(
45
+ "/",
46
+ REMOTE_ADDR="127.0.0.1",
47
+ HTTP_X_FORWARDED_HOST="frontendserver",
48
+ ):
49
+ url, addr = app.view().content_iterator
50
+ assert url == b"http://frontendserver/"
51
+
52
+ def test_forwards_x_forwarded_ssl(self):
53
+ app = self.get_app()
54
+ with app.requestcontext(
55
+ "/",
56
+ REMOTE_ADDR="127.0.0.1",
57
+ HTTP_X_FORWARDED_SSL="on",
58
+ HTTP_X_FORWARDED_HOST="localhost",
59
+ ):
60
+ url, addr = app.view().content_iterator
61
+ assert url == b"https://localhost/"
62
+
63
+ def test_forwards_x_forwarded_proto(self):
64
+ app = self.get_app()
65
+ with app.requestcontext(
66
+ "/",
67
+ REMOTE_ADDR="127.0.0.1",
68
+ HTTP_X_FORWARDED_PROTO="https",
69
+ HTTP_X_FORWARDED_HOST="localhost",
70
+ ):
71
+ url, addr = app.view().content_iterator
72
+ assert url == b"https://localhost/"
73
+
74
+ def test_reports_first_trusted_ip(self):
75
+ app = self.get_app(trusted=["127.0.0.1", "3.3.3.3"])
76
+ with app.requestcontext(
77
+ "/",
78
+ REMOTE_ADDR="127.0.0.1",
79
+ HTTP_X_FORWARDED_FOR="1.1.1.1, " "2.2.2.2, 3.3.3.3",
80
+ ):
81
+ url, addr = app.view().content_iterator
82
+ assert addr == b"2.2.2.2"
83
+
84
+ def test_reports_first_listed_ip_as_remote_addr_when_untrusted(self):
85
+ app = self.get_app()
86
+ with app.requestcontext(
87
+ "/",
88
+ REMOTE_ADDR="127.0.0.1",
89
+ HTTP_X_FORWARDED_FOR="1.1.1.1, 2.2.2.2",
90
+ ):
91
+ url, addr = app.view().content_iterator
92
+ assert addr == b"1.1.1.1"
@@ -0,0 +1,234 @@
1
+ # Copyright 2015 Oliver Cope
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+ import pytest
16
+ from fresco.multidict import MultiDict
17
+
18
+
19
+ def is_consistent(multidict: MultiDict) -> bool:
20
+ "Verify that the MultiDict has internal consistency"
21
+ dictallitems = sorted((k, v) for k, vs in multidict._dict.items() for v in vs)
22
+ orderallitems = sorted(multidict._order)
23
+ return list(sorted(dictallitems)) == list(sorted(orderallitems))
24
+
25
+
26
+ class TestMultDict(object):
27
+ def test_getitem_returns_only_first(self):
28
+ m = MultiDict([("a", 1), ("a", 2), ("b", 3)])
29
+ assert m["a"] == 1
30
+
31
+ def test_getitem_throws_keyerror(self):
32
+ m = MultiDict([("a", 1), ("a", 2), ("b", 3)])
33
+ with pytest.raises(KeyError):
34
+ m["c"]
35
+
36
+ def test_get_returns_only_first(self):
37
+ m = MultiDict([("a", 1), ("a", 2), ("b", 3)])
38
+ assert m.get("a") == 1
39
+
40
+ def test_get_returns_default(self):
41
+ m = MultiDict()
42
+ assert m.get("a") is None
43
+ assert m.get("a", "foo") == "foo"
44
+
45
+ def test_getlist_returns_list(self):
46
+ m = MultiDict([("a", 1), ("a", 2)])
47
+ assert m.getlist("a") == [1, 2]
48
+ assert m.getlist("b") == []
49
+
50
+ def test_copy_is_equal(self):
51
+ m = MultiDict([("a", 1), ("a", 2)])
52
+ assert list(m.allitems()) == list(m.copy().allitems())
53
+
54
+ def test_copy_is_independent(self):
55
+ m = MultiDict([("a", 1), ("a", 2)])
56
+ n = m.copy()
57
+ n["b"] = "foo"
58
+ assert list(m.allitems()) == [("a", 1), ("a", 2)]
59
+ assert list(n.allitems()) == [("a", 1), ("a", 2), ("b", "foo")]
60
+
61
+ def test_fromkeys(self):
62
+ m = MultiDict.fromkeys(["a", "b"])
63
+ assert list(m.allitems()) == [("a", None), ("b", None)]
64
+
65
+ def test_fromkeys_with_value(self):
66
+ m = MultiDict.fromkeys(["a", "b"], 42)
67
+ assert list(m.allitems()) == [("a", 42), ("b", 42)]
68
+
69
+ def test_items_only_returns_first_of_each(self):
70
+ m = MultiDict([("a", 1), ("a", 2)])
71
+ assert list(m.items()) == [("a", 1)]
72
+
73
+ def test_listitems_returns_lists(self):
74
+ m = MultiDict([("a", 1), ("a", 2)])
75
+ assert list(m.listitems()) == [("a", [1, 2])]
76
+
77
+ def test_allitems_returns_all(self):
78
+ m = MultiDict([("a", 1), ("a", 2)])
79
+ assert list(m.allitems()) == [("a", 1), ("a", 2)]
80
+
81
+ def test_allitems_returns_iterator(self):
82
+ m = MultiDict([("a", 1), ("a", 2)])
83
+ i = m.allitems()
84
+ assert next(i) == ("a", 1)
85
+ assert next(i) == ("a", 2)
86
+ with pytest.raises(StopIteration):
87
+ next(i)
88
+
89
+ def test_keys(self):
90
+ m = MultiDict([("a", 1), ("a", 2), ("b", 3)])
91
+ i = m.keys()
92
+ assert next(i) == "a"
93
+ assert next(i) == "b"
94
+ with pytest.raises(StopIteration):
95
+ next(i)
96
+
97
+ def test_values(self):
98
+ m = MultiDict([("a", 1), ("a", 2), ("b", 3)])
99
+ i = m.values()
100
+ assert next(i) == 1
101
+ assert next(i) == 3
102
+ with pytest.raises(StopIteration):
103
+ next(i)
104
+
105
+ def test_listvalues(self):
106
+ m = MultiDict([("a", 1), ("a", 2), ("b", 3)])
107
+ assert list(m.listvalues()) == [[1, 2], [3]]
108
+
109
+ def test_setitem(self):
110
+ m = MultiDict()
111
+ m["a"] = 1
112
+ assert m["a"] == 1
113
+ assert m.getlist("a") == [1]
114
+ assert is_consistent(m)
115
+
116
+ def test_setitem_replaces_existing(self):
117
+ m = MultiDict()
118
+ m["a"] = 1
119
+ m["a"] = 2
120
+ assert m.getlist("a") == [2]
121
+ assert is_consistent(m)
122
+
123
+ def test_delitem(self):
124
+ m = MultiDict([("a", 1), ("a", 2), ("b", 3)])
125
+
126
+ del m["a"]
127
+ assert list(m.allitems()) == [("b", 3)]
128
+ assert is_consistent(m)
129
+
130
+ m = MultiDict([("a", 1), ("a", 2), ("b", 3)])
131
+ del m["b"]
132
+ assert list(m.allitems()) == [("a", 1), ("a", 2)]
133
+ assert is_consistent(m)
134
+
135
+ def test_iterable(self):
136
+ m = MultiDict([("a", 1), ("a", 2), ("b", 3)])
137
+ assert list(sorted(iter(m))) == ["a", "b"]
138
+
139
+ def test_update_with_list(self):
140
+ m = MultiDict([("a", 1), ("b", 2)])
141
+ m.update([("a", 2), ("x", 2)])
142
+ assert list(sorted(m.allitems())) == [("a", 2), ("b", 2), ("x", 2)]
143
+ assert is_consistent(m)
144
+
145
+ def test_update_with_dict(self):
146
+ m = MultiDict([("a", 1), ("b", 2)])
147
+ m.update({"a": 2, "x": 2})
148
+ assert list(sorted(m.allitems())) == [("a", 2), ("b", 2), ("x", 2)]
149
+ assert is_consistent(m)
150
+
151
+ def test_update_with_multidict(self):
152
+ m = MultiDict([("a", 1), ("b", 2)])
153
+ m.update(MultiDict([("a", 2), ("x", 2)]))
154
+ assert list(sorted(m.allitems())) == [("a", 2), ("b", 2), ("x", 2)]
155
+ assert is_consistent(m)
156
+
157
+ def test_update_with_kwargs(self):
158
+ m = MultiDict([("a", 1), ("b", 2)])
159
+ m.update(a=2, x=2)
160
+ assert sorted(list(m.allitems())) == [("a", 2), ("b", 2), ("x", 2)]
161
+ assert is_consistent(m)
162
+
163
+ def test_extend_with_list(self):
164
+ m = MultiDict([("a", 1), ("b", 2)])
165
+ m.extend([("a", 2), ("x", 2)])
166
+ assert list(m.allitems()) == [("a", 1), ("b", 2), ("a", 2), ("x", 2)]
167
+ assert is_consistent(m)
168
+
169
+ def test_extend_with_dict(self):
170
+ m = MultiDict([("a", 1), ("b", 2)])
171
+ m.extend({"a": 2, "x": 2})
172
+ assert list(sorted(m.allitems())) == [
173
+ ("a", 1),
174
+ ("a", 2),
175
+ ("b", 2),
176
+ ("x", 2),
177
+ ]
178
+ assert is_consistent(m)
179
+
180
+ def test_extend_with_multidict(self):
181
+ m = MultiDict([("a", 1), ("b", 2)])
182
+ m.extend(MultiDict([("a", 2), ("x", 2)]))
183
+ assert list(sorted(m.allitems())) == [
184
+ ("a", 1),
185
+ ("a", 2),
186
+ ("b", 2),
187
+ ("x", 2),
188
+ ]
189
+ assert is_consistent(m)
190
+
191
+ def test_extend_with_kwargs(self):
192
+ m = MultiDict([("a", 1), ("b", 2)])
193
+ m.extend(a=2, x=2)
194
+ assert list(sorted(m.allitems())) == [
195
+ ("a", 1),
196
+ ("a", 2),
197
+ ("b", 2),
198
+ ("x", 2),
199
+ ]
200
+ assert is_consistent(m)
201
+
202
+ def test_pop(self):
203
+ m = MultiDict([("a", 1), ("b", 2), ("a", 3)])
204
+ assert m.pop("b") == 2
205
+ assert list(sorted(m.allitems())) == [("a", 1), ("a", 3)]
206
+ assert is_consistent(m)
207
+
208
+ def test_pop_returns_first_item(self):
209
+ m = MultiDict([("a", 1), ("b", 2), ("a", 3)])
210
+ assert m.pop("a") == 1
211
+ assert list(sorted(m.allitems())) == [("a", 3), ("b", 2)]
212
+ assert is_consistent(m)
213
+
214
+ def test_pop_with_default(self):
215
+ m = MultiDict([])
216
+ assert m.pop("c", "foo") == "foo"
217
+ assert is_consistent(m)
218
+
219
+ def test_popitem(self):
220
+ m = MultiDict([("a", 1), ("b", 2), ("b", 3)])
221
+
222
+ assert m.popitem() == ("b", 2)
223
+ assert is_consistent(m)
224
+ assert m.popitem() == ("b", 3)
225
+ assert is_consistent(m)
226
+ assert m.popitem() == ("a", 1)
227
+ assert is_consistent(m)
228
+
229
+ with pytest.raises(KeyError):
230
+ m.popitem()
231
+
232
+ def test_len(self):
233
+ m = MultiDict([("a", 1), ("b", 2), ("b", 3)])
234
+ assert len(m) == 2