From 6d58abf7a62edd89cfb21975a6fb4d62494ce003 Mon Sep 17 00:00:00 2001 From: sinwoobang Date: Tue, 22 Dec 2015 18:44:45 +0900 Subject: [PATCH] Add new http status 451 --- aiohttp/web_exceptions.py | 13 +++++++++++++ tests/test_web_exceptions.py | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/aiohttp/web_exceptions.py b/aiohttp/web_exceptions.py index adc8cbb3f3c..254f07319a2 100644 --- a/aiohttp/web_exceptions.py +++ b/aiohttp/web_exceptions.py @@ -45,6 +45,7 @@ 'HTTPPreconditionRequired', 'HTTPTooManyRequests', 'HTTPRequestHeaderFieldsTooLarge', + 'HTTPUnavailableForLegalReasons', 'HTTPServerError', 'HTTPInternalServerError', 'HTTPNotImplemented', @@ -286,6 +287,18 @@ class HTTPRequestHeaderFieldsTooLarge(HTTPClientError): status_code = 431 +class HTTPUnavailableForLegalReasons(HTTPClientError): + status_code = 451 + + def __init__(self, link=None, *, headers=None, reason=None, + body=None, text=None, content_type=None): + super().__init__(headers=headers, reason=reason, + body=body, text=text, content_type=content_type) + if link: + self.headers['Link'] = '<%s>; rel="blocked-by"' % link + self.link = link + + ############################################################ # 5xx Server Error ############################################################ diff --git a/tests/test_web_exceptions.py b/tests/test_web_exceptions.py index b89c659e161..e84dc25a152 100644 --- a/tests/test_web_exceptions.py +++ b/tests/test_web_exceptions.py @@ -163,3 +163,10 @@ def test_empty_body_205(): def test_empty_body_304(): resp = web.HTTPNoContent() resp.body is None + + +def test_link_header_451(buf, request): + resp = web.HTTPUnavailableForLegalReasons(link='http://warning.or.kr/') + + assert 'http://warning.or.kr/' == resp.link + assert '; rel="blocked-by"' == resp.headers['Link']