Harden request bounds and verify benchmark responses
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"""Focused HTTP checks for the contract transport layer."""
|
||||
|
||||
import json
|
||||
import http.client
|
||||
import socket
|
||||
import threading
|
||||
import time
|
||||
@@ -80,6 +81,19 @@ class APIContractTests(unittest.TestCase):
|
||||
def test_unsupported_media_type(self):
|
||||
self.assert_error(*self.call("POST", "/api/v1/users", b"{}", "text/plain"), 415)
|
||||
|
||||
def test_oversized_chunked_json(self):
|
||||
conn = http.client.HTTPConnection(self.url.removeprefix("http://"), timeout=5)
|
||||
try:
|
||||
conn.request(
|
||||
"POST", "/api/v1/users", body=iter((b" " * 600_000, b" " * 600_000)),
|
||||
headers={"Content-Type": "application/json"}, encode_chunked=True,
|
||||
)
|
||||
response = conn.getresponse()
|
||||
payload = json.loads(response.read())
|
||||
self.assert_error(response.status, response.headers, payload, 413)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def test_unexpected_error_is_private(self):
|
||||
status, headers, payload = self.call("GET", "/_test_error")
|
||||
self.assert_error(status, headers, payload, 500)
|
||||
@@ -118,6 +132,7 @@ class APIContractTests(unittest.TestCase):
|
||||
self.assertEqual(self.call("GET", next_path, actor=alice["id"])[2], {"items": [second], "next_cursor": None})
|
||||
self.assert_error(*self.call("GET", next_path, actor=bob["id"]), 400)
|
||||
self.assert_error(*self.call("GET", "/api/v1/chats?limit=0", actor=alice["id"]), 400)
|
||||
self.assert_error(*self.call("GET", "/api/v1/chats?limit=" + "9" * 4301, actor=alice["id"]), 400)
|
||||
|
||||
def test_messages_access_validation_and_pages(self):
|
||||
from urllib.parse import quote
|
||||
@@ -168,6 +183,7 @@ class APIContractTests(unittest.TestCase):
|
||||
self.assert_error(*send(eve["id"], {"text": "чужое"}), 404)
|
||||
self.assert_error(*self.call("GET", path, actor=eve["id"]), 404)
|
||||
self.assert_error(*self.call("GET", path + "?limit=0", actor=alice["id"]), 400)
|
||||
self.assert_error(*self.call("GET", path + "?limit=" + "9" * 4301, actor=alice["id"]), 400)
|
||||
self.assert_error(*self.call("GET", path + "?cursor=broken", actor=alice["id"]), 400)
|
||||
self.assert_error(*self.call("GET", other_path + "?cursor=" + quote(self.call("GET", path + "?limit=1", actor=alice["id"])[2]["next_cursor"], safe=""), actor=alice["id"]), 400)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user