spiderweb-framework 1.0.0__tar.gz → 1.1.0__tar.gz
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.
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/PKG-INFO +2 -7
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/README.md +1 -7
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/pyproject.toml +1 -1
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/constants.py +1 -1
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/db.py +3 -0
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/exceptions.py +4 -0
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/middleware/cors.py +4 -1
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/routes.py +54 -16
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/tests/middleware.py +1 -1
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/tests/test_middleware.py +57 -39
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/tests/test_responses.py +66 -2
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/tests/views_for_tests.py +1 -1
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/LICENSE.txt +0 -0
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/__init__.py +0 -0
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/converters.py +0 -0
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/decorators.py +0 -0
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/default_views.py +0 -0
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/example_validator.py +0 -0
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/local_server.py +0 -0
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/main.py +0 -0
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/middleware/__init__.py +0 -0
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/middleware/base.py +0 -0
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/middleware/csrf.py +0 -0
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/middleware/pydantic.py +0 -0
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/middleware/sessions.py +0 -0
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/request.py +0 -0
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/response.py +0 -0
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/secrets.py +0 -0
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/server_checks.py +0 -0
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/tests/__init__.py +0 -0
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/tests/helpers.py +0 -0
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/tests/test.html +0 -0
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/tests/test_variable_urls.py +0 -0
- {spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: spiderweb-framework
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.1.0
|
|
4
4
|
Summary: A small web framework, just big enough for a spider.
|
|
5
5
|
License: LICENSE.txt
|
|
6
6
|
Author: Joe Kaufeld
|
|
@@ -97,10 +97,5 @@ And, honestly, I think I got there. Here's a non-exhaustive list of things this
|
|
|
97
97
|
- Optional POST data validation middleware with Pydantic
|
|
98
98
|
- Session middleware with built-in session store
|
|
99
99
|
- Database support (using Peewee, but you can use whatever you want as long as there's a Peewee driver for it)
|
|
100
|
-
- Tests (currently
|
|
100
|
+
- Tests (currently roughly 89% coverage)
|
|
101
101
|
|
|
102
|
-
Once tests are in and proven to work, then I'll release as version 1.0.
|
|
103
|
-
|
|
104
|
-
- currently missing tests for cors middleware
|
|
105
|
-
|
|
106
|
-
If you're reading this on GitHub, this repository is a public mirror of https://git.joekaufeld.com/jkaufeld/spiderweb.
|
|
@@ -69,10 +69,4 @@ And, honestly, I think I got there. Here's a non-exhaustive list of things this
|
|
|
69
69
|
- Optional POST data validation middleware with Pydantic
|
|
70
70
|
- Session middleware with built-in session store
|
|
71
71
|
- Database support (using Peewee, but you can use whatever you want as long as there's a Peewee driver for it)
|
|
72
|
-
- Tests (currently
|
|
73
|
-
|
|
74
|
-
Once tests are in and proven to work, then I'll release as version 1.0.
|
|
75
|
-
|
|
76
|
-
- currently missing tests for cors middleware
|
|
77
|
-
|
|
78
|
-
If you're reading this on GitHub, this repository is a public mirror of https://git.joekaufeld.com/jkaufeld/spiderweb.
|
|
72
|
+
- Tests (currently roughly 89% coverage)
|
|
@@ -2,7 +2,7 @@ from peewee import DatabaseProxy
|
|
|
2
2
|
|
|
3
3
|
DEFAULT_ALLOWED_METHODS = ["POST", "GET", "PUT", "PATCH", "DELETE"]
|
|
4
4
|
DEFAULT_ENCODING = "UTF-8"
|
|
5
|
-
__version__ = "1.
|
|
5
|
+
__version__ = "1.1.0"
|
|
6
6
|
|
|
7
7
|
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
|
|
8
8
|
REGEX_COOKIE_NAME = r"^[a-zA-Z0-9\s\(\)<>@,;:\/\\\[\]\?=\{\}\"\t]*$"
|
|
@@ -101,7 +101,10 @@ class CorsMiddleware(SpiderwebMiddleware):
|
|
|
101
101
|
)
|
|
102
102
|
if (
|
|
103
103
|
self.server.cors_allow_private_network
|
|
104
|
-
and request.headers.get(
|
|
104
|
+
and request.headers.get(
|
|
105
|
+
ACCESS_CONTROL_REQUEST_PRIVATE_NETWORK.replace("-", "_")
|
|
106
|
+
)
|
|
107
|
+
== "true"
|
|
105
108
|
):
|
|
106
109
|
response.headers[ACCESS_CONTROL_ALLOW_PRIVATE_NETWORK] = "true"
|
|
107
110
|
|
|
@@ -4,7 +4,13 @@ from typing import Callable, Any, Optional, Sequence
|
|
|
4
4
|
from spiderweb.constants import DEFAULT_ALLOWED_METHODS
|
|
5
5
|
from spiderweb.converters import * # noqa: F403
|
|
6
6
|
from spiderweb.default_views import * # noqa: F403
|
|
7
|
-
from spiderweb.exceptions import
|
|
7
|
+
from spiderweb.exceptions import (
|
|
8
|
+
NotFound,
|
|
9
|
+
ConfigError,
|
|
10
|
+
ParseError,
|
|
11
|
+
SpiderwebException,
|
|
12
|
+
ReverseNotFound,
|
|
13
|
+
)
|
|
8
14
|
from spiderweb.response import RedirectResponse
|
|
9
15
|
|
|
10
16
|
|
|
@@ -35,7 +41,7 @@ class RoutesMixin:
|
|
|
35
41
|
error_routes: dict[int, Callable]
|
|
36
42
|
append_slash: bool
|
|
37
43
|
|
|
38
|
-
def route(self, path, allowed_methods=None) -> Callable:
|
|
44
|
+
def route(self, path, allowed_methods=None, name=None) -> Callable:
|
|
39
45
|
"""
|
|
40
46
|
Decorator for adding a route to a view.
|
|
41
47
|
|
|
@@ -49,11 +55,12 @@ class RoutesMixin:
|
|
|
49
55
|
|
|
50
56
|
:param path: str
|
|
51
57
|
:param allowed_methods: list[str]
|
|
58
|
+
:param name: str
|
|
52
59
|
:return: Callable
|
|
53
60
|
"""
|
|
54
61
|
|
|
55
62
|
def outer(func):
|
|
56
|
-
self.add_route(path, func, allowed_methods)
|
|
63
|
+
self.add_route(path, func, allowed_methods, name)
|
|
57
64
|
return func
|
|
58
65
|
|
|
59
66
|
return outer
|
|
@@ -115,7 +122,11 @@ class RoutesMixin:
|
|
|
115
122
|
return re.compile(rf"^{'/'.join(parts)}$")
|
|
116
123
|
|
|
117
124
|
def add_route(
|
|
118
|
-
self,
|
|
125
|
+
self,
|
|
126
|
+
path: str,
|
|
127
|
+
method: Callable,
|
|
128
|
+
allowed_methods: None | list[str] = None,
|
|
129
|
+
name: str = None,
|
|
119
130
|
):
|
|
120
131
|
"""Add a route to the server."""
|
|
121
132
|
allowed_methods = (
|
|
@@ -124,24 +135,27 @@ class RoutesMixin:
|
|
|
124
135
|
or DEFAULT_ALLOWED_METHODS
|
|
125
136
|
)
|
|
126
137
|
|
|
138
|
+
reverse_path = re.sub(r"<(.*?):(.*?)>", r"{\2}", path) if "<" in path else path
|
|
139
|
+
|
|
140
|
+
def get_packet(func):
|
|
141
|
+
return {
|
|
142
|
+
"func": func,
|
|
143
|
+
"allowed_methods": allowed_methods,
|
|
144
|
+
"name": name,
|
|
145
|
+
"reverse": reverse_path,
|
|
146
|
+
}
|
|
147
|
+
|
|
127
148
|
if self.append_slash and not path.endswith("/"):
|
|
128
149
|
updated_path = path + "/"
|
|
129
150
|
self.check_for_route_duplicates(updated_path)
|
|
130
151
|
self.check_for_route_duplicates(path)
|
|
131
|
-
self._routes[self.convert_path(path)] =
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
self._routes[self.convert_path(updated_path)] = {
|
|
136
|
-
"func": method,
|
|
137
|
-
"allowed_methods": allowed_methods,
|
|
138
|
-
}
|
|
152
|
+
self._routes[self.convert_path(path)] = get_packet(
|
|
153
|
+
DummyRedirectRoute(updated_path)
|
|
154
|
+
)
|
|
155
|
+
self._routes[self.convert_path(updated_path)] = get_packet(method)
|
|
139
156
|
else:
|
|
140
157
|
self.check_for_route_duplicates(path)
|
|
141
|
-
self._routes[self.convert_path(path)] =
|
|
142
|
-
"func": method,
|
|
143
|
-
"allowed_methods": allowed_methods,
|
|
144
|
-
}
|
|
158
|
+
self._routes[self.convert_path(path)] = get_packet(method)
|
|
145
159
|
|
|
146
160
|
def add_routes(self):
|
|
147
161
|
for line in self.routes:
|
|
@@ -156,3 +170,27 @@ class RoutesMixin:
|
|
|
156
170
|
def add_error_routes(self):
|
|
157
171
|
for code, func in self.error_routes.items():
|
|
158
172
|
self.add_error_route(int(code), func)
|
|
173
|
+
|
|
174
|
+
def reverse(
|
|
175
|
+
self, view_name: str, data: dict[str, Any] = None, query: dict[str, Any] = None
|
|
176
|
+
) -> str:
|
|
177
|
+
# take in a view name and return the path
|
|
178
|
+
for option in self._routes.values():
|
|
179
|
+
if option["name"] == view_name:
|
|
180
|
+
path = option["reverse"]
|
|
181
|
+
if args := re.findall(r"{(.*?)}", path):
|
|
182
|
+
if not data:
|
|
183
|
+
raise SpiderwebException(
|
|
184
|
+
f"Missing arguments for reverse: {args}"
|
|
185
|
+
)
|
|
186
|
+
for arg in args:
|
|
187
|
+
if arg not in data:
|
|
188
|
+
raise SpiderwebException(
|
|
189
|
+
f"Missing argument '{arg}' for reverse."
|
|
190
|
+
)
|
|
191
|
+
path = path.replace(f"{{{arg}}}", str(data[arg]))
|
|
192
|
+
|
|
193
|
+
if query:
|
|
194
|
+
path += "?" + "&".join([f"{k}={str(v)}" for k, v in query.items()])
|
|
195
|
+
return path
|
|
196
|
+
raise ReverseNotFound(f"View '{view_name}' not found.")
|
|
@@ -21,7 +21,9 @@ from spiderweb.tests.helpers import setup
|
|
|
21
21
|
from spiderweb.tests.views_for_tests import (
|
|
22
22
|
form_view_with_csrf,
|
|
23
23
|
form_csrf_exempt,
|
|
24
|
-
form_view_without_csrf,
|
|
24
|
+
form_view_without_csrf,
|
|
25
|
+
text_view,
|
|
26
|
+
unauthorized_view,
|
|
25
27
|
)
|
|
26
28
|
|
|
27
29
|
|
|
@@ -371,11 +373,14 @@ class TestCorsMiddleware:
|
|
|
371
373
|
app, environ, start_response = setup(
|
|
372
374
|
**self.middleware,
|
|
373
375
|
cors_allow_all_origins=True,
|
|
374
|
-
cors_expose_headers=["accept", "content-type"]
|
|
376
|
+
cors_expose_headers=["accept", "content-type"],
|
|
375
377
|
)
|
|
376
378
|
environ["HTTP_ORIGIN"] = "https://example.com"
|
|
377
379
|
app(environ, start_response)
|
|
378
|
-
assert
|
|
380
|
+
assert (
|
|
381
|
+
start_response.get_headers()[ACCESS_CONTROL_EXPOSE_HEADERS]
|
|
382
|
+
== "accept, content-type"
|
|
383
|
+
)
|
|
379
384
|
|
|
380
385
|
def test_get_dont_expose_headers(self):
|
|
381
386
|
app, environ, start_response = setup(
|
|
@@ -419,18 +424,20 @@ class TestCorsMiddleware:
|
|
|
419
424
|
app, environ, start_response = setup(
|
|
420
425
|
**self.middleware,
|
|
421
426
|
cors_allow_private_network=True,
|
|
422
|
-
cors_allow_all_origins=True
|
|
427
|
+
cors_allow_all_origins=True,
|
|
423
428
|
)
|
|
424
429
|
environ["HTTP_ACCESS_CONTROL_REQUEST_PRIVATE_NETWORK"] = "true"
|
|
425
430
|
environ["HTTP_ORIGIN"] = "http://example.com"
|
|
426
431
|
app(environ, start_response)
|
|
427
|
-
assert
|
|
432
|
+
assert (
|
|
433
|
+
start_response.get_headers()[ACCESS_CONTROL_ALLOW_PRIVATE_NETWORK] == "true"
|
|
434
|
+
)
|
|
428
435
|
|
|
429
436
|
def test_allow_private_network_not_added_if_enabled_and_not_requested(self):
|
|
430
437
|
app, environ, start_response = setup(
|
|
431
438
|
**self.middleware,
|
|
432
439
|
cors_allow_private_network=True,
|
|
433
|
-
cors_allow_all_origins=True
|
|
440
|
+
cors_allow_all_origins=True,
|
|
434
441
|
)
|
|
435
442
|
environ["HTTP_ORIGIN"] = "http://example.com"
|
|
436
443
|
app(environ, start_response)
|
|
@@ -440,19 +447,18 @@ class TestCorsMiddleware:
|
|
|
440
447
|
app, environ, start_response = setup(
|
|
441
448
|
**self.middleware,
|
|
442
449
|
cors_allow_private_network=True,
|
|
443
|
-
cors_allowed_origins=["http://example.com"]
|
|
450
|
+
cors_allowed_origins=["http://example.com"],
|
|
444
451
|
)
|
|
445
452
|
environ["HTTP_ACCESS_CONTROL_REQUEST_PRIVATE_NETWORK"] = "true"
|
|
446
453
|
environ["HTTP_ORIGIN"] = "http://example.org"
|
|
447
454
|
app(environ, start_response)
|
|
448
455
|
assert ACCESS_CONTROL_ALLOW_PRIVATE_NETWORK not in start_response.get_headers()
|
|
449
456
|
|
|
450
|
-
|
|
451
457
|
def test_allow_private_network_not_added_if_disabled_and_requested(self):
|
|
452
458
|
app, environ, start_response = setup(
|
|
453
459
|
**self.middleware,
|
|
454
460
|
cors_allow_private_network=False,
|
|
455
|
-
cors_allow_all_origins=True
|
|
461
|
+
cors_allow_all_origins=True,
|
|
456
462
|
)
|
|
457
463
|
environ["HTTP_ACCESS_CONTROL_REQUEST_PRIVATE_NETWORK"] = "true"
|
|
458
464
|
environ["HTTP_ORIGIN"] = "http://example.com"
|
|
@@ -465,7 +471,7 @@ class TestCorsMiddleware:
|
|
|
465
471
|
cors_allow_headers=["content-type"],
|
|
466
472
|
cors_allow_methods=["GET", "OPTIONS"],
|
|
467
473
|
cors_preflight_max_age=1002,
|
|
468
|
-
cors_allow_all_origins=True
|
|
474
|
+
cors_allow_all_origins=True,
|
|
469
475
|
)
|
|
470
476
|
environ["HTTP_ACCESS_CONTROL_REQUEST_METHOD"] = "GET"
|
|
471
477
|
environ["HTTP_ORIGIN"] = "https://example.com"
|
|
@@ -474,26 +480,24 @@ class TestCorsMiddleware:
|
|
|
474
480
|
|
|
475
481
|
headers = start_response.get_headers()
|
|
476
482
|
|
|
477
|
-
assert start_response.status ==
|
|
483
|
+
assert start_response.status == "200 OK"
|
|
478
484
|
assert headers[ACCESS_CONTROL_ALLOW_HEADERS] == "content-type"
|
|
479
485
|
assert headers[ACCESS_CONTROL_ALLOW_METHODS] == "GET, OPTIONS"
|
|
480
486
|
assert headers[ACCESS_CONTROL_MAX_AGE] == "1002"
|
|
481
487
|
|
|
482
|
-
|
|
483
488
|
def test_options_no_max_age(self):
|
|
484
489
|
app, environ, start_response = setup(
|
|
485
490
|
**self.middleware,
|
|
486
491
|
cors_allow_headers=["content-type"],
|
|
487
492
|
cors_allow_methods=["GET", "OPTIONS"],
|
|
488
493
|
cors_preflight_max_age=0,
|
|
489
|
-
cors_allow_all_origins=True
|
|
494
|
+
cors_allow_all_origins=True,
|
|
490
495
|
)
|
|
491
496
|
environ["HTTP_ACCESS_CONTROL_REQUEST_METHOD"] = "GET"
|
|
492
497
|
environ["HTTP_ORIGIN"] = "https://example.com"
|
|
493
498
|
environ["REQUEST_METHOD"] = "OPTIONS"
|
|
494
499
|
app(environ, start_response)
|
|
495
500
|
|
|
496
|
-
|
|
497
501
|
headers = start_response.get_headers()
|
|
498
502
|
assert headers[ACCESS_CONTROL_ALLOW_HEADERS] == "content-type"
|
|
499
503
|
assert headers[ACCESS_CONTROL_ALLOW_METHODS] == "GET, OPTIONS"
|
|
@@ -501,34 +505,39 @@ class TestCorsMiddleware:
|
|
|
501
505
|
|
|
502
506
|
def test_options_allowed_origins_with_port(self):
|
|
503
507
|
app, environ, start_response = setup(
|
|
504
|
-
**self.middleware,
|
|
505
|
-
cors_allowed_origins=["https://localhost:9000"]
|
|
508
|
+
**self.middleware, cors_allowed_origins=["https://localhost:9000"]
|
|
506
509
|
)
|
|
507
510
|
environ["HTTP_ACCESS_CONTROL_REQUEST_METHOD"] = "GET"
|
|
508
511
|
environ["HTTP_ORIGIN"] = "https://localhost:9000"
|
|
509
512
|
environ["REQUEST_METHOD"] = "OPTIONS"
|
|
510
513
|
app(environ, start_response)
|
|
511
514
|
|
|
512
|
-
assert
|
|
515
|
+
assert (
|
|
516
|
+
start_response.get_headers()[ACCESS_CONTROL_ALLOW_ORIGIN]
|
|
517
|
+
== "https://localhost:9000"
|
|
518
|
+
)
|
|
513
519
|
|
|
514
520
|
def test_options_adds_origin_when_domain_found_in_allowed_regexes(self):
|
|
515
521
|
app, environ, start_response = setup(
|
|
516
522
|
**self.middleware,
|
|
517
|
-
cors_allowed_origin_regexes=[r"^https://\w+\.example\.com$"]
|
|
523
|
+
cors_allowed_origin_regexes=[r"^https://\w+\.example\.com$"],
|
|
518
524
|
)
|
|
519
525
|
environ["HTTP_ACCESS_CONTROL_REQUEST_METHOD"] = "GET"
|
|
520
526
|
environ["HTTP_ORIGIN"] = "https://foo.example.com"
|
|
521
527
|
environ["REQUEST_METHOD"] = "OPTIONS"
|
|
522
528
|
app(environ, start_response)
|
|
523
529
|
|
|
524
|
-
assert
|
|
530
|
+
assert (
|
|
531
|
+
start_response.get_headers()[ACCESS_CONTROL_ALLOW_ORIGIN]
|
|
532
|
+
== "https://foo.example.com"
|
|
533
|
+
)
|
|
525
534
|
|
|
526
535
|
def test_options_adds_origin_when_domain_found_in_allowed_regexes_second(self):
|
|
527
536
|
app, environ, start_response = setup(
|
|
528
537
|
**self.middleware,
|
|
529
538
|
cors_allowed_origin_regexes=[
|
|
530
|
-
|
|
531
|
-
|
|
539
|
+
r"^https://\w+\.example\.org$",
|
|
540
|
+
r"^https://\w+\.example\.com$",
|
|
532
541
|
],
|
|
533
542
|
)
|
|
534
543
|
environ["HTTP_ACCESS_CONTROL_REQUEST_METHOD"] = "GET"
|
|
@@ -536,7 +545,10 @@ class TestCorsMiddleware:
|
|
|
536
545
|
environ["REQUEST_METHOD"] = "OPTIONS"
|
|
537
546
|
app(environ, start_response)
|
|
538
547
|
|
|
539
|
-
assert
|
|
548
|
+
assert (
|
|
549
|
+
start_response.get_headers()[ACCESS_CONTROL_ALLOW_ORIGIN]
|
|
550
|
+
== "https://foo.example.com"
|
|
551
|
+
)
|
|
540
552
|
|
|
541
553
|
def test_options_doesnt_add_origin_when_domain_not_found_in_allowed_regexes(self):
|
|
542
554
|
app, environ, start_response = setup(
|
|
@@ -562,14 +574,9 @@ class TestCorsMiddleware:
|
|
|
562
574
|
|
|
563
575
|
assert start_response.status == "200 OK"
|
|
564
576
|
|
|
565
|
-
|
|
566
577
|
def test_options_no_headers(self):
|
|
567
578
|
app, environ, start_response = setup(
|
|
568
|
-
**self.middleware,
|
|
569
|
-
cors_allow_all_origins=True,
|
|
570
|
-
routes=[
|
|
571
|
-
("/", text_view)
|
|
572
|
-
]
|
|
579
|
+
**self.middleware, cors_allow_all_origins=True, routes=[("/", text_view)]
|
|
573
580
|
)
|
|
574
581
|
environ["REQUEST_METHOD"] = "OPTIONS"
|
|
575
582
|
app(environ, start_response)
|
|
@@ -580,7 +587,7 @@ class TestCorsMiddleware:
|
|
|
580
587
|
**self.middleware,
|
|
581
588
|
cors_allow_credentials=True,
|
|
582
589
|
cors_allow_all_origins=True,
|
|
583
|
-
routes=[("/", text_view)]
|
|
590
|
+
routes=[("/", text_view)],
|
|
584
591
|
)
|
|
585
592
|
environ["HTTP_ACCESS_CONTROL_REQUEST_METHOD"] = "GET"
|
|
586
593
|
environ["HTTP_ORIGIN"] = "https://example.com"
|
|
@@ -588,7 +595,10 @@ class TestCorsMiddleware:
|
|
|
588
595
|
app(environ, start_response)
|
|
589
596
|
|
|
590
597
|
assert start_response.status == "200 OK"
|
|
591
|
-
assert
|
|
598
|
+
assert (
|
|
599
|
+
start_response.get_headers()[ACCESS_CONTROL_ALLOW_ORIGIN]
|
|
600
|
+
== "https://example.com"
|
|
601
|
+
)
|
|
592
602
|
assert start_response.get_headers()["vary"] == "origin"
|
|
593
603
|
|
|
594
604
|
def test_allow_all_origins_options(self):
|
|
@@ -596,7 +606,7 @@ class TestCorsMiddleware:
|
|
|
596
606
|
**self.middleware,
|
|
597
607
|
cors_allow_credentials=True,
|
|
598
608
|
cors_allow_all_origins=True,
|
|
599
|
-
routes=[("/", text_view)]
|
|
609
|
+
routes=[("/", text_view)],
|
|
600
610
|
)
|
|
601
611
|
|
|
602
612
|
environ["HTTP_ACCESS_CONTROL_REQUEST_METHOD"] = "GET"
|
|
@@ -605,7 +615,10 @@ class TestCorsMiddleware:
|
|
|
605
615
|
app(environ, start_response)
|
|
606
616
|
|
|
607
617
|
assert start_response.status == "200 OK"
|
|
608
|
-
assert
|
|
618
|
+
assert (
|
|
619
|
+
start_response.get_headers()[ACCESS_CONTROL_ALLOW_ORIGIN]
|
|
620
|
+
== "https://example.com"
|
|
621
|
+
)
|
|
609
622
|
assert start_response.get_headers()["vary"] == "origin"
|
|
610
623
|
|
|
611
624
|
def test_non_200_headers_still_set(self):
|
|
@@ -618,15 +631,17 @@ class TestCorsMiddleware:
|
|
|
618
631
|
**self.middleware,
|
|
619
632
|
cors_allow_credentials=True,
|
|
620
633
|
cors_allow_all_origins=True,
|
|
621
|
-
routes=[("/unauthorized", unauthorized_view)]
|
|
634
|
+
routes=[("/unauthorized", unauthorized_view)],
|
|
622
635
|
)
|
|
623
636
|
environ["HTTP_ORIGIN"] = "https://example.com"
|
|
624
637
|
environ["PATH_INFO"] = "/unauthorized"
|
|
625
638
|
app(environ, start_response)
|
|
626
639
|
|
|
627
|
-
|
|
628
640
|
assert start_response.status == "401 Unauthorized"
|
|
629
|
-
assert
|
|
641
|
+
assert (
|
|
642
|
+
start_response.get_headers()[ACCESS_CONTROL_ALLOW_ORIGIN]
|
|
643
|
+
== "https://example.com"
|
|
644
|
+
)
|
|
630
645
|
|
|
631
646
|
def test_auth_view_options(self):
|
|
632
647
|
"""
|
|
@@ -637,7 +652,7 @@ class TestCorsMiddleware:
|
|
|
637
652
|
**self.middleware,
|
|
638
653
|
cors_allow_credentials=True,
|
|
639
654
|
cors_allow_all_origins=True,
|
|
640
|
-
routes=[("/unauthorized", unauthorized_view)]
|
|
655
|
+
routes=[("/unauthorized", unauthorized_view)],
|
|
641
656
|
)
|
|
642
657
|
environ["HTTP_ORIGIN"] = "https://example.com"
|
|
643
658
|
environ["HTTP_ACCESS_CONTROL_REQUEST_METHOD"] = "GET"
|
|
@@ -646,10 +661,12 @@ class TestCorsMiddleware:
|
|
|
646
661
|
app(environ, start_response)
|
|
647
662
|
|
|
648
663
|
assert start_response.status == "200 OK"
|
|
649
|
-
assert
|
|
664
|
+
assert (
|
|
665
|
+
start_response.get_headers()[ACCESS_CONTROL_ALLOW_ORIGIN]
|
|
666
|
+
== "https://example.com"
|
|
667
|
+
)
|
|
650
668
|
assert start_response.get_headers()["content-length"] == "0"
|
|
651
669
|
|
|
652
|
-
|
|
653
670
|
def test_get_short_circuit(self):
|
|
654
671
|
"""
|
|
655
672
|
Test a scenario when a middleware that returns a response is run before
|
|
@@ -716,6 +733,7 @@ class TestCorsMiddleware:
|
|
|
716
733
|
Just in case something crazy happens in the view or other middleware,
|
|
717
734
|
check that get_response doesn't fall over if `_cors_enabled` is removed
|
|
718
735
|
"""
|
|
736
|
+
|
|
719
737
|
def yeet(request):
|
|
720
738
|
del request._cors_enabled
|
|
721
739
|
return HttpResponse("hahaha")
|
|
@@ -723,7 +741,7 @@ class TestCorsMiddleware:
|
|
|
723
741
|
app, environ, start_response = setup(
|
|
724
742
|
**self.middleware,
|
|
725
743
|
cors_allowed_origins=["https://example.com"],
|
|
726
|
-
routes=[(
|
|
744
|
+
routes=[("/yeet", yeet)],
|
|
727
745
|
)
|
|
728
746
|
|
|
729
747
|
environ["HTTP_ORIGIN"] = "https://example.com"
|
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import pytest
|
|
2
2
|
|
|
3
|
-
from spiderweb import
|
|
3
|
+
from spiderweb import ConfigError
|
|
4
4
|
from spiderweb.constants import DEFAULT_ENCODING
|
|
5
|
-
from spiderweb.exceptions import
|
|
5
|
+
from spiderweb.exceptions import (
|
|
6
|
+
NoResponseError,
|
|
7
|
+
SpiderwebNetworkException,
|
|
8
|
+
SpiderwebException,
|
|
9
|
+
ReverseNotFound,
|
|
10
|
+
)
|
|
6
11
|
from spiderweb.response import (
|
|
7
12
|
HttpResponse,
|
|
8
13
|
JsonResponse,
|
|
@@ -176,3 +181,62 @@ def test_missing_view_with_custom_404_alt():
|
|
|
176
181
|
app, environ, start_response = setup(error_routes={404: custom_404})
|
|
177
182
|
|
|
178
183
|
assert app(environ, start_response) == [b"Custom 404 2"]
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def test_getting_nonexistent_error_view():
|
|
187
|
+
app, environ, start_response = setup()
|
|
188
|
+
|
|
189
|
+
assert app.get_error_route(10101).__name__ == "http500"
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def test_view_gets_name():
|
|
193
|
+
app, environ, start_response = setup()
|
|
194
|
+
|
|
195
|
+
@app.route("/", name="asdfasdf")
|
|
196
|
+
def index(request): ...
|
|
197
|
+
|
|
198
|
+
assert [v for k, v in app._routes.items()][0]["name"] == "asdfasdf"
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def test_view_can_be_reversed():
|
|
202
|
+
app, environ, start_response = setup()
|
|
203
|
+
|
|
204
|
+
@app.route("/", name="asdfasdf")
|
|
205
|
+
def index(request): ...
|
|
206
|
+
|
|
207
|
+
@app.route("/<int:hi>", name="qwer")
|
|
208
|
+
def index(request, hi): ...
|
|
209
|
+
|
|
210
|
+
assert app.reverse("asdfasdf") == "/"
|
|
211
|
+
assert app.reverse("asdfasdf", {"id": 1}) == "/"
|
|
212
|
+
assert app.reverse("asdfasdf", {"id": 1}, query={"key": "value"}) == "/?key=value"
|
|
213
|
+
|
|
214
|
+
assert app.reverse("qwer", {"hi": 1}) == "/1"
|
|
215
|
+
assert app.reverse("qwer", {"hi": 1}, query={"key": "value"}) == "/1?key=value"
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def test_reversed_views_explode_when_missing_all_args():
|
|
219
|
+
app, environ, start_response = setup()
|
|
220
|
+
|
|
221
|
+
@app.route("/<int:hi>", name="qwer")
|
|
222
|
+
def index(request, hi): ...
|
|
223
|
+
|
|
224
|
+
with pytest.raises(SpiderwebException):
|
|
225
|
+
app.reverse("qwer")
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def test_reversed_views_explode_when_missing_some_args():
|
|
229
|
+
app, environ, start_response = setup()
|
|
230
|
+
|
|
231
|
+
@app.route("/<int:hi>/<str:bye>", name="qwer")
|
|
232
|
+
def index(request, hi, bye): ...
|
|
233
|
+
|
|
234
|
+
with pytest.raises(SpiderwebException):
|
|
235
|
+
app.reverse("qwer", {"hi": 1})
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def test_reverse_nonexistent_view():
|
|
239
|
+
app, environ, start_response = setup()
|
|
240
|
+
|
|
241
|
+
with pytest.raises(ReverseNotFound):
|
|
242
|
+
app.reverse("qwer")
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{spiderweb_framework-1.0.0 → spiderweb_framework-1.1.0}/spiderweb/tests/test_variable_urls.py
RENAMED
|
File without changes
|
|
File without changes
|