forked from mtusi2026/mtusi2026lab2
Возвращать 415 unsupported_media_type для неподдерживаемого Content-Type
Запрос с Content-Type, отличным от application/json, раньше получал 400 invalid_request. Теперь _json_body отвечает 415 unsupported_media_type (ApiError через новую функцию unsupported_media_type в errors.py), как предусмотрено контрактом. Повреждённый JSON с верным Content-Type по-прежнему даёт 400. Тест проверяет точный код ответа на регистрации, создании чата и отправке сообщения; число тестов 73 -> 76, evidence/tests.txt обновлён. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -57,9 +57,35 @@ class InvalidJsonTest(unittest.TestCase):
|
||||
with self.subTest(fields=fields):
|
||||
self.assert_invalid(api.request('POST', USERS, body=registration(**fields)))
|
||||
|
||||
def test_wrong_content_type(self):
|
||||
def assert_unsupported_media_type(self, response):
|
||||
self.assertEqual(response.status, 415)
|
||||
self.assertEqual(response.json['error']['code'], 'unsupported_media_type')
|
||||
|
||||
def test_wrong_content_type_is_415(self):
|
||||
raw = json.dumps(registration()).encode()
|
||||
self.assert_invalid(api.request('POST', USERS, raw=raw, headers={'Content-Type': 'text/plain'}))
|
||||
for content_type in ('text/plain', 'application/xml', 'application/x-www-form-urlencoded', 'application/jsonx'):
|
||||
with self.subTest(content_type=content_type):
|
||||
self.assert_unsupported_media_type(
|
||||
api.request('POST', USERS, raw=raw, headers={'Content-Type': content_type}))
|
||||
|
||||
def test_wrong_content_type_on_chat_and_message_is_415(self):
|
||||
user = api.new_user()
|
||||
chat = api.new_chat(user, [user])
|
||||
headers = {'Content-Type': 'text/plain'}
|
||||
self.assert_unsupported_media_type(api.request(
|
||||
'POST', '/api/v1/chats', user=user, raw=json.dumps({'title': 'Т', 'member_ids': [user['id']]}).encode(),
|
||||
headers=headers))
|
||||
self.assert_unsupported_media_type(api.request(
|
||||
'POST', '/api/v1/chats/%s/messages' % chat['id'], user=user, raw=json.dumps({'text': 'привет'}).encode(), headers=headers))
|
||||
self.assertEqual(api.pages('/api/v1/chats/%s/messages' % chat['id'], user, 100), [[]])
|
||||
|
||||
def test_json_content_type_with_charset_is_accepted(self):
|
||||
raw = json.dumps(registration()).encode()
|
||||
response = api.request('POST', USERS, raw=raw, headers={'Content-Type': 'application/json; charset=utf-8'})
|
||||
self.assertEqual(response.status, 201)
|
||||
|
||||
def test_malformed_json_with_right_content_type_stays_400(self):
|
||||
self.assert_invalid(api.request('POST', USERS, raw=b'{"username": ', headers={'Content-Type': 'application/json'}))
|
||||
|
||||
|
||||
class UserValidationTest(unittest.TestCase):
|
||||
|
||||
Reference in New Issue
Block a user