lapis-api 0.2.1.post1__tar.gz → 0.2.2__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.
- lapis_api-0.2.2/.github/workflows/tests.yml +78 -0
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/PKG-INFO +4 -2
- lapis_api-0.2.2/justfile +13 -0
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/pyproject.toml +5 -4
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/src/lapis/lapis.py +16 -12
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/src/lapis/protocols/http1.py +18 -3
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/src/lapis/protocols/websocket.py +6 -5
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/src/lapis/server_types.py +2 -2
- lapis_api-0.2.2/tests/basic_load_test/test_basic_load.py +21 -0
- lapis_api-0.2.2/tests/conftest.py +57 -0
- lapis_api-0.2.2/tests/server_config_test/test_server_config.py +28 -0
- lapis_api-0.2.2/tests/slugs_test/test_slugs.py +16 -0
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/tests/streamed_response_test/api/path.py +0 -4
- lapis_api-0.2.2/tests/streamed_response_test/test_streamed_response.py +12 -0
- lapis_api-0.2.2/tests/websocket_test/test_websocket.py +55 -0
- lapis_api-0.2.1.post1/tests/basic_load_test/test.py +0 -5
- lapis_api-0.2.1.post1/tests/server_config_test/test.py +0 -6
- lapis_api-0.2.1.post1/tests/slugs_test/test.py +0 -5
- lapis_api-0.2.1.post1/tests/streamed_response_test/test.py +0 -5
- lapis_api-0.2.1.post1/tests/websocket_test/test.py +0 -5
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/.github/CONTRIBUTIONS.md +0 -0
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/.github/workflows/pylint.yml +0 -0
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/.github/workflows/python-publish.yml +0 -0
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/.gitignore +0 -0
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/.pylintrc +0 -0
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/LICENSE +0 -0
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/README.md +0 -0
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/src/Lapis.egg-info/PKG-INFO +0 -0
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/src/Lapis.egg-info/SOURCES.txt +0 -0
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/src/Lapis.egg-info/dependency_links.txt +0 -0
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/src/Lapis.egg-info/top_level.txt +0 -0
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/src/lapis/__init__.py +0 -0
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/tests/basic_load_test/api/other/path.py +0 -0
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/tests/basic_load_test/api/path.py +0 -0
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/tests/server_config_test/config.json +0 -0
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/tests/server_config_test/other/api/path.py +0 -0
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/tests/slugs_test/api/[slug1]/other/[slug2]/path.py +0 -0
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/tests/slugs_test/api/[slug1]/path.py +0 -0
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/tests/slugs_test/api/[slug2]/maybe/[slug3]/path.py +0 -0
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/tests/slugs_test/api/path.py +0 -0
- {lapis_api-0.2.1.post1 → lapis_api-0.2.2}/tests/websocket_test/api/path.py +0 -0
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
name: Tests
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: read
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
versions:
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
outputs:
|
|
15
|
+
matrix: ${{ steps.resolve.outputs.matrix }}
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v4
|
|
18
|
+
- uses: actions/setup-python@v5
|
|
19
|
+
with:
|
|
20
|
+
python-version: "3.11"
|
|
21
|
+
- id: resolve
|
|
22
|
+
name: Resolve compatible Python versions
|
|
23
|
+
run: |
|
|
24
|
+
python -m pip install packaging
|
|
25
|
+
python - <<'PY'
|
|
26
|
+
import json
|
|
27
|
+
import os
|
|
28
|
+
import tomllib
|
|
29
|
+
from urllib.request import urlopen
|
|
30
|
+
|
|
31
|
+
from packaging.specifiers import SpecifierSet
|
|
32
|
+
from packaging.version import Version
|
|
33
|
+
|
|
34
|
+
with open("pyproject.toml", "rb") as project_file:
|
|
35
|
+
requirement = tomllib.load(project_file)["project"]["requires-python"]
|
|
36
|
+
specifiers = SpecifierSet(requirement)
|
|
37
|
+
|
|
38
|
+
with urlopen("https://raw.githubusercontent.com/actions/python-versions/main/versions-manifest.json") as response:
|
|
39
|
+
releases = json.load(response)
|
|
40
|
+
|
|
41
|
+
versions = sorted(
|
|
42
|
+
{
|
|
43
|
+
f"{version.major}.{version.minor}"
|
|
44
|
+
for release in releases
|
|
45
|
+
if release["stable"]
|
|
46
|
+
and any(
|
|
47
|
+
file["platform"] == "linux"
|
|
48
|
+
and file.get("platform_version") == "24.04"
|
|
49
|
+
and file["arch"] == "x64"
|
|
50
|
+
for file in release["files"]
|
|
51
|
+
)
|
|
52
|
+
for version in [Version(release["version"])]
|
|
53
|
+
if specifiers.contains(version)
|
|
54
|
+
},
|
|
55
|
+
key=Version,
|
|
56
|
+
)
|
|
57
|
+
if not versions:
|
|
58
|
+
raise SystemExit(f"No stable Python releases satisfy {requirement!r}")
|
|
59
|
+
|
|
60
|
+
with open(os.environ["GITHUB_OUTPUT"], "a") as output:
|
|
61
|
+
print(f"matrix={json.dumps(versions)}", file=output)
|
|
62
|
+
PY
|
|
63
|
+
pytest:
|
|
64
|
+
runs-on: ubuntu-24.04
|
|
65
|
+
needs: versions
|
|
66
|
+
strategy:
|
|
67
|
+
matrix:
|
|
68
|
+
python-version: ${{ fromJSON(needs.versions.outputs.matrix) }}
|
|
69
|
+
steps:
|
|
70
|
+
- uses: actions/checkout@v4
|
|
71
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
72
|
+
uses: actions/setup-python@v5
|
|
73
|
+
with:
|
|
74
|
+
python-version: ${{ matrix.python-version }}
|
|
75
|
+
- name: Install project and test dependencies
|
|
76
|
+
run: python -m pip install -e ".[test]"
|
|
77
|
+
- name: Run pytest suite
|
|
78
|
+
run: python -m pytest -v
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
2
|
Name: lapis-api
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.2
|
|
4
4
|
Summary: An organized way to create REST APIs
|
|
5
5
|
Project-URL: Homepage, https://github.com/CQVan/Lapis
|
|
6
6
|
Project-URL: Wiki, https://github.com/CQVan/Lapis/wiki
|
|
@@ -12,6 +12,8 @@ Classifier: License :: OSI Approved :: MIT License
|
|
|
12
12
|
Classifier: Operating System :: OS Independent
|
|
13
13
|
Classifier: Programming Language :: Python :: 3
|
|
14
14
|
Requires-Python: >=3.9
|
|
15
|
+
Provides-Extra: test
|
|
16
|
+
Requires-Dist: pytest>=8.0; extra == 'test'
|
|
15
17
|
Description-Content-Type: text/markdown
|
|
16
18
|
|
|
17
19
|
Lapis is a file-based REST API framework inspired by Modern Serverless Cloud Services
|
lapis_api-0.2.2/justfile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
build:
|
|
2
|
+
@echo "Starting Build Process..." ;
|
|
3
|
+
@rm -rf ./dist/* ;
|
|
4
|
+
@python -m build > /dev/null 2>&1
|
|
5
|
+
@PACKAGE=$(ls ./dist/*.whl 2>/dev/null | tail -n 1 || echo "") ; \
|
|
6
|
+
echo "Built package: $PACKAGE"; \
|
|
7
|
+
pip install --force "$PACKAGE" > /dev/null 2>&1; \
|
|
8
|
+
echo "Installed package: $PACKAGE"
|
|
9
|
+
|
|
10
|
+
format:
|
|
11
|
+
@echo "Formatting code" ;
|
|
12
|
+
@black . 2>&1 | grep -E "reformatted|left unchanged"
|
|
13
|
+
@pylint .
|
|
@@ -7,7 +7,7 @@ packages = ["src/lapis"]
|
|
|
7
7
|
|
|
8
8
|
[project]
|
|
9
9
|
name = "lapis-api"
|
|
10
|
-
version = "0.2.
|
|
10
|
+
version = "0.2.2"
|
|
11
11
|
description = "An organized way to create REST APIs"
|
|
12
12
|
readme = "README.md"
|
|
13
13
|
requires-python = ">=3.9"
|
|
@@ -15,8 +15,6 @@ authors = [
|
|
|
15
15
|
{ name = "Chandler Van" },
|
|
16
16
|
]
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
18
|
license = {text = "MIT"}
|
|
21
19
|
|
|
22
20
|
classifiers = [
|
|
@@ -28,4 +26,7 @@ classifiers = [
|
|
|
28
26
|
[project.urls]
|
|
29
27
|
Homepage = "https://github.com/CQVan/Lapis"
|
|
30
28
|
Wiki = "https://github.com/CQVan/Lapis/wiki"
|
|
31
|
-
Issues = "https://github.com/CQVan/Lapis/issues"
|
|
29
|
+
Issues = "https://github.com/CQVan/Lapis/issues"
|
|
30
|
+
|
|
31
|
+
[project.optional-dependencies]
|
|
32
|
+
test = ["pytest>=8.0"]
|
|
@@ -12,6 +12,7 @@ import sys
|
|
|
12
12
|
import re
|
|
13
13
|
from threading import Thread
|
|
14
14
|
from datetime import datetime
|
|
15
|
+
from typing import Optional
|
|
15
16
|
|
|
16
17
|
from lapis.protocols.websocket import WebSocketProtocol
|
|
17
18
|
from lapis.protocols.http1 import HTTP1Protocol, Request, Response
|
|
@@ -30,22 +31,25 @@ class Lapis:
|
|
|
30
31
|
The Lapis class implements the centeral object used to run a Lapis REST server
|
|
31
32
|
"""
|
|
32
33
|
|
|
33
|
-
cfg: ServerConfig
|
|
34
|
+
cfg: ServerConfig
|
|
34
35
|
|
|
35
|
-
__s: socket.socket
|
|
36
|
-
__paths: dict
|
|
37
|
-
__taken_endpoints: list[str]
|
|
38
|
-
__protocols: list[type[Protocol]]
|
|
36
|
+
__s: Optional[socket.socket]
|
|
37
|
+
__paths: dict
|
|
38
|
+
__taken_endpoints: list[str]
|
|
39
|
+
__protocols: list[type[Protocol]]
|
|
39
40
|
|
|
40
41
|
__slug_pattern = re.compile(r"\[[^\]]+\]")
|
|
41
42
|
__path_pattern = re.compile(r"^\/([a-zA-Z0-9._-]+)(\/[a-zA-Z0-9._-]+)*$")
|
|
42
43
|
|
|
43
|
-
__running: bool
|
|
44
|
+
__running: bool
|
|
44
45
|
|
|
45
|
-
def __init__(self, config: ServerConfig
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
def __init__(self, config: Optional[ServerConfig] = None):
|
|
47
|
+
self.cfg = config if config is not None else ServerConfig()
|
|
48
|
+
self.__s = None
|
|
49
|
+
self.__paths = {}
|
|
50
|
+
self.__taken_endpoints = []
|
|
51
|
+
self.__protocols = []
|
|
52
|
+
self.__running = False
|
|
49
53
|
|
|
50
54
|
self.__register_protocol(HTTP1Protocol)
|
|
51
55
|
self.__register_protocol(WebSocketProtocol)
|
|
@@ -183,7 +187,7 @@ class Lapis:
|
|
|
183
187
|
|
|
184
188
|
def __has_endpoint_path(
|
|
185
189
|
self, base_url: str
|
|
186
|
-
) -> tuple[dict[str, any]
|
|
190
|
+
) -> tuple[Optional[dict[str, any]], dict[str, str]]:
|
|
187
191
|
# Convert the URL into parts, ignoring the leading slash
|
|
188
192
|
path_parts = pathlib.Path(base_url.lstrip("/")).parts
|
|
189
193
|
|
|
@@ -192,7 +196,7 @@ class Lapis:
|
|
|
192
196
|
|
|
193
197
|
def _search_tree(
|
|
194
198
|
self, current_level: dict, remaining_parts: tuple[str, ...], slugs: dict
|
|
195
|
-
) -> tuple[dict
|
|
199
|
+
) -> tuple[Optional[dict], dict]:
|
|
196
200
|
|
|
197
201
|
# Base case
|
|
198
202
|
if not remaining_parts:
|
|
@@ -4,13 +4,28 @@ Module containing the HTTP 1/1.1 protocol implementation for Lapis server
|
|
|
4
4
|
|
|
5
5
|
from dataclasses import dataclass
|
|
6
6
|
from datetime import datetime
|
|
7
|
-
from
|
|
7
|
+
from enum import Enum
|
|
8
|
+
from http import HTTPStatus
|
|
8
9
|
import socket
|
|
9
|
-
from typing import AsyncGenerator, Callable
|
|
10
|
+
from typing import AsyncGenerator, Callable, Union
|
|
10
11
|
from urllib.parse import parse_qsl, urlparse
|
|
11
12
|
|
|
12
13
|
from lapis.server_types import BadRequest, Protocol
|
|
13
14
|
|
|
15
|
+
try:
|
|
16
|
+
from http import HTTPMethod
|
|
17
|
+
except ImportError:
|
|
18
|
+
class HTTPMethod(str, Enum):
|
|
19
|
+
CONNECT = "CONNECT"
|
|
20
|
+
DELETE = "DELETE"
|
|
21
|
+
GET = "GET"
|
|
22
|
+
HEAD = "HEAD"
|
|
23
|
+
OPTIONS = "OPTIONS"
|
|
24
|
+
PATCH = "PATCH"
|
|
25
|
+
POST = "POST"
|
|
26
|
+
PUT = "PUT"
|
|
27
|
+
TRACE = "TRACE"
|
|
28
|
+
|
|
14
29
|
|
|
15
30
|
@dataclass
|
|
16
31
|
class RequestHeader:
|
|
@@ -123,7 +138,7 @@ class Response:
|
|
|
123
138
|
|
|
124
139
|
def __init__(
|
|
125
140
|
self,
|
|
126
|
-
status_code: int
|
|
141
|
+
status_code: Union[int, HTTPStatus] = HTTPStatus.OK,
|
|
127
142
|
body: str = "",
|
|
128
143
|
headers: dict[str, any] = None,
|
|
129
144
|
):
|
|
@@ -9,6 +9,7 @@ import socket
|
|
|
9
9
|
import base64
|
|
10
10
|
import hashlib
|
|
11
11
|
from enum import Enum
|
|
12
|
+
from typing import Optional, Union
|
|
12
13
|
|
|
13
14
|
from lapis.server_types import Protocol
|
|
14
15
|
from lapis.protocols.http1 import Request, Response
|
|
@@ -122,7 +123,7 @@ class WSFrame:
|
|
|
122
123
|
return 10
|
|
123
124
|
|
|
124
125
|
@property
|
|
125
|
-
def masking_key(self) -> bytes
|
|
126
|
+
def masking_key(self) -> Optional[bytes]:
|
|
126
127
|
"""
|
|
127
128
|
Returns the masking key of the frame used to mask the payload
|
|
128
129
|
|
|
@@ -138,7 +139,7 @@ class WSFrame:
|
|
|
138
139
|
return self.__data[start : start + 4]
|
|
139
140
|
|
|
140
141
|
@property
|
|
141
|
-
def data(self) -> str
|
|
142
|
+
def data(self) -> Union[str, bytes]:
|
|
142
143
|
"""
|
|
143
144
|
Returns the payload data of the frame
|
|
144
145
|
|
|
@@ -212,7 +213,7 @@ class WSPortal:
|
|
|
212
213
|
asyncio.create_task(self.__reader())
|
|
213
214
|
|
|
214
215
|
def __send_frame(
|
|
215
|
-
self, opcode: WSOpcode, payload: str
|
|
216
|
+
self, opcode: WSOpcode, payload: Union[str, bytes] = b"", fin: bool = True
|
|
216
217
|
):
|
|
217
218
|
"""
|
|
218
219
|
Utility function used to send frames from server to client
|
|
@@ -321,7 +322,7 @@ class WSPortal:
|
|
|
321
322
|
"""
|
|
322
323
|
return self.__closed
|
|
323
324
|
|
|
324
|
-
async def recv(self, timeout: float = None) -> str
|
|
325
|
+
async def recv(self, timeout: float = None) -> Union[str, bytes]:
|
|
325
326
|
"""
|
|
326
327
|
Recieves a full frame from the client
|
|
327
328
|
|
|
@@ -373,7 +374,7 @@ class WSPortal:
|
|
|
373
374
|
except asyncio.TimeoutError as err:
|
|
374
375
|
raise WSRecvTimeoutError() from err
|
|
375
376
|
|
|
376
|
-
def send(self, payload: str
|
|
377
|
+
def send(self, payload: Union[str, bytes]):
|
|
377
378
|
"""
|
|
378
379
|
Sends a payload for the client to recieve
|
|
379
380
|
|
|
@@ -7,7 +7,7 @@ from dataclasses import dataclass, field
|
|
|
7
7
|
import socket
|
|
8
8
|
import json
|
|
9
9
|
import sys
|
|
10
|
-
from typing import get_origin, get_type_hints
|
|
10
|
+
from typing import Union, get_origin, get_type_hints
|
|
11
11
|
import pathlib
|
|
12
12
|
|
|
13
13
|
|
|
@@ -25,7 +25,7 @@ class ServerConfig:
|
|
|
25
25
|
protocol_configs: dict[str, dict] = field(default_factory=dict)
|
|
26
26
|
|
|
27
27
|
@classmethod
|
|
28
|
-
def from_json(cls, file_path: str
|
|
28
|
+
def from_json(cls, file_path: Union[str, pathlib.Path]) -> "ServerConfig":
|
|
29
29
|
"""
|
|
30
30
|
Generates a ServerConfig object from the json file found at the file path
|
|
31
31
|
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def test_loads_and_serves_api_routes(server_factory, send_request):
|
|
5
|
+
server = server_factory(Path(__file__))
|
|
6
|
+
|
|
7
|
+
response = send_request(server, "/")
|
|
8
|
+
nested_response = send_request(server, "/other")
|
|
9
|
+
|
|
10
|
+
assert response.startswith(b"HTTP/1.1 200 OK\r\n")
|
|
11
|
+
assert response.endswith(b"Hello World!")
|
|
12
|
+
assert nested_response.startswith(b"HTTP/1.1 200 OK\r\n")
|
|
13
|
+
assert nested_response.endswith(b"Other Hello World!")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def test_unknown_route_returns_not_found(server_factory, send_request):
|
|
17
|
+
server = server_factory(Path(__file__))
|
|
18
|
+
|
|
19
|
+
response = send_request(server, "/missing")
|
|
20
|
+
|
|
21
|
+
assert response.startswith(b"HTTP/1.1 404 Not Found\r\n")
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from lapis import Lapis
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class TestClient:
|
|
9
|
+
def __init__(self, request_data: bytes):
|
|
10
|
+
self.request_data = request_data
|
|
11
|
+
self.sent_data = bytearray()
|
|
12
|
+
|
|
13
|
+
def recv(self, _size: int) -> bytes:
|
|
14
|
+
return self.request_data
|
|
15
|
+
|
|
16
|
+
def sendall(self, data: bytes):
|
|
17
|
+
self.sent_data.extend(data)
|
|
18
|
+
|
|
19
|
+
def send(self, data: bytes):
|
|
20
|
+
self.sent_data.extend(data)
|
|
21
|
+
return len(data)
|
|
22
|
+
|
|
23
|
+
def getpeername(self):
|
|
24
|
+
return ("127.0.0.1", 0)
|
|
25
|
+
|
|
26
|
+
def close(self):
|
|
27
|
+
pass
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@pytest.fixture
|
|
31
|
+
def test_client_factory():
|
|
32
|
+
return TestClient
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@pytest.fixture
|
|
36
|
+
def server_factory(monkeypatch):
|
|
37
|
+
def create_server(test_file, config=None):
|
|
38
|
+
monkeypatch.setattr(sys, "argv", [str(test_file)])
|
|
39
|
+
return Lapis(config=config)
|
|
40
|
+
|
|
41
|
+
return create_server
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@pytest.fixture
|
|
45
|
+
def send_request():
|
|
46
|
+
def send(server, target: str, headers: str = "") -> bytes:
|
|
47
|
+
request = (
|
|
48
|
+
f"GET {target} HTTP/1.1\r\n"
|
|
49
|
+
f"Host: localhost\r\n"
|
|
50
|
+
f"{headers}"
|
|
51
|
+
"\r\n"
|
|
52
|
+
).encode("ascii")
|
|
53
|
+
client = TestClient(request)
|
|
54
|
+
server._handle_request(client)
|
|
55
|
+
return bytes(client.sent_data)
|
|
56
|
+
|
|
57
|
+
return send
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
import sys
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
|
|
6
|
+
from lapis import ServerConfig
|
|
7
|
+
from lapis.server_types import BadConfigError
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def test_config_selects_api_directory(server_factory, send_request, monkeypatch):
|
|
11
|
+
test_file = Path(__file__)
|
|
12
|
+
monkeypatch.setattr(sys, "argv", [str(test_file)])
|
|
13
|
+
config = ServerConfig.from_json("./config.json")
|
|
14
|
+
server = server_factory(test_file, config=config)
|
|
15
|
+
|
|
16
|
+
response = send_request(server, "/")
|
|
17
|
+
|
|
18
|
+
assert response.startswith(b"HTTP/1.1 200 OK\r\n")
|
|
19
|
+
assert response.endswith(b"API path from a different api folder!")
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def test_config_rejects_values_with_the_wrong_type(tmp_path, monkeypatch):
|
|
23
|
+
config_path = tmp_path / "config.json"
|
|
24
|
+
config_path.write_text('{"max_request_size": "large"}', encoding="utf-8")
|
|
25
|
+
monkeypatch.setattr("sys.argv", [str(tmp_path / "test.py")])
|
|
26
|
+
|
|
27
|
+
with pytest.raises(BadConfigError, match='"max_request_size"'):
|
|
28
|
+
ServerConfig.from_json(config_path.name)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def test_resolves_slug_routes_and_captures_values(server_factory, send_request):
|
|
5
|
+
server = server_factory(Path(__file__))
|
|
6
|
+
|
|
7
|
+
cases = [
|
|
8
|
+
("/alice", b"{'slug1': 'alice'}"),
|
|
9
|
+
("/alice/other/document-42", b"{'slug1': 'alice', 'slug2': 'document-42'}"),
|
|
10
|
+
("/team/maybe/project", b"{'slug2': 'team', 'slug3': 'project'}"),
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
for path, expected_slugs in cases:
|
|
14
|
+
response = send_request(server, path)
|
|
15
|
+
assert response.startswith(b"HTTP/1.1 200 OK\r\n")
|
|
16
|
+
assert response.endswith(expected_slugs)
|
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
from lapis import Request, StreamedResponse
|
|
2
2
|
|
|
3
|
-
import asyncio
|
|
4
3
|
from typing import AsyncGenerator
|
|
5
4
|
|
|
6
5
|
|
|
7
6
|
async def get_stream(req: Request) -> AsyncGenerator[bytes, None]:
|
|
8
|
-
await asyncio.sleep(1)
|
|
9
7
|
yield b"Hello"
|
|
10
|
-
await asyncio.sleep(1)
|
|
11
8
|
yield b" World!"
|
|
12
|
-
await asyncio.sleep(1)
|
|
13
9
|
|
|
14
10
|
|
|
15
11
|
async def GET(req: Request) -> StreamedResponse:
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def test_streams_chunked_response(server_factory, send_request):
|
|
5
|
+
server = server_factory(Path(__file__))
|
|
6
|
+
|
|
7
|
+
response = send_request(server, "/")
|
|
8
|
+
headers, body = response.split(b"\r\n\r\n", 1)
|
|
9
|
+
|
|
10
|
+
assert response.startswith(b"HTTP/1.1 200 OK\r\n")
|
|
11
|
+
assert b"Transfer-Encoding: chunked" in headers
|
|
12
|
+
assert body == b"5\r\nHello\r\n7\r\n World!\r\n0\r\n\r\n"
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
from lapis.protocols.websocket import WSFrame, WSOpcode, WebSocketProtocol
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def test_parses_masked_text_frame():
|
|
5
|
+
payload = b"hello"
|
|
6
|
+
mask = b"\x01\x02\x03\x04"
|
|
7
|
+
masked_payload = bytes(
|
|
8
|
+
value ^ mask[index % 4] for index, value in enumerate(payload)
|
|
9
|
+
)
|
|
10
|
+
frame = WSFrame(
|
|
11
|
+
bytes([0x80 | WSOpcode.TEXT.value, 0x80 | len(payload)]) + mask + masked_payload
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
assert frame.fin
|
|
15
|
+
assert frame.masked
|
|
16
|
+
assert frame.opcode is WSOpcode.TEXT
|
|
17
|
+
assert frame.data == "hello"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def test_performs_websocket_handshake(test_client_factory):
|
|
21
|
+
key = "dGhlIHNhbXBsZSBub25jZQ=="
|
|
22
|
+
client = test_client_factory(b"")
|
|
23
|
+
protocol = WebSocketProtocol()
|
|
24
|
+
request_data = (
|
|
25
|
+
"GET / HTTP/1.1\r\n"
|
|
26
|
+
"Host: localhost\r\n"
|
|
27
|
+
"Connection: Upgrade\r\n"
|
|
28
|
+
"Upgrade: websocket\r\n"
|
|
29
|
+
"Sec-WebSocket-Version: 13\r\n"
|
|
30
|
+
f"Sec-WebSocket-Key: {key}\r\n\r\n"
|
|
31
|
+
).encode("ascii")
|
|
32
|
+
|
|
33
|
+
assert protocol.identify(request_data)
|
|
34
|
+
assert protocol.handshake(client)
|
|
35
|
+
|
|
36
|
+
response = bytes(client.sent_data)
|
|
37
|
+
assert response.startswith(b"HTTP/1.1 101 Switching Protocols\r\n")
|
|
38
|
+
assert b"Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=" in response
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def test_rejects_invalid_websocket_key(test_client_factory):
|
|
42
|
+
client = test_client_factory(b"")
|
|
43
|
+
protocol = WebSocketProtocol()
|
|
44
|
+
request_data = (
|
|
45
|
+
"GET / HTTP/1.1\r\n"
|
|
46
|
+
"Host: localhost\r\n"
|
|
47
|
+
"Connection: Upgrade\r\n"
|
|
48
|
+
"Upgrade: websocket\r\n"
|
|
49
|
+
"Sec-WebSocket-Version: 13\r\n"
|
|
50
|
+
"Sec-WebSocket-Key: invalid\r\n\r\n"
|
|
51
|
+
).encode("ascii")
|
|
52
|
+
|
|
53
|
+
assert protocol.identify(request_data)
|
|
54
|
+
assert not protocol.handshake(client)
|
|
55
|
+
assert bytes(client.sent_data).startswith(b"HTTP/1.1 400 Bad Request\r\n")
|
|
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
|
{lapis_api-0.2.1.post1 → lapis_api-0.2.2}/tests/slugs_test/api/[slug1]/other/[slug2]/path.py
RENAMED
|
File without changes
|
|
File without changes
|
{lapis_api-0.2.1.post1 → lapis_api-0.2.2}/tests/slugs_test/api/[slug2]/maybe/[slug3]/path.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|