Skip to content

Commit

Permalink
twisted#12104 Add support for HTTP status code 418 (twisted#12105)
Browse files Browse the repository at this point in the history
  • Loading branch information
adiroiban committed Feb 21, 2024
2 parents 446ee13 + af793d5 commit 108d99a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/twisted/web/_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
UNSUPPORTED_MEDIA_TYPE = 415
REQUESTED_RANGE_NOT_SATISFIABLE = 416
EXPECTATION_FAILED = 417
IM_A_TEAPOT = 418

INTERNAL_SERVER_ERROR = 500
NOT_IMPLEMENTED = 501
Expand Down Expand Up @@ -98,6 +99,7 @@
UNSUPPORTED_MEDIA_TYPE: b"Unsupported Media Type",
REQUESTED_RANGE_NOT_SATISFIABLE: b"Requested Range not satisfiable",
EXPECTATION_FAILED: b"Expectation Failed",
IM_A_TEAPOT: b"I'm a teapot",
# 500
INTERNAL_SERVER_ERROR: b"Internal Server Error",
NOT_IMPLEMENTED: b"Not Implemented",
Expand Down
2 changes: 2 additions & 0 deletions src/twisted/web/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"UNSUPPORTED_MEDIA_TYPE",
"REQUESTED_RANGE_NOT_SATISFIABLE",
"EXPECTATION_FAILED",
"IM_A_TEAPOT",
"INTERNAL_SERVER_ERROR",
"NOT_IMPLEMENTED",
"BAD_GATEWAY",
Expand Down Expand Up @@ -144,6 +145,7 @@
GATEWAY_TIMEOUT,
GONE,
HTTP_VERSION_NOT_SUPPORTED,
IM_A_TEAPOT,
INSUFFICIENT_STORAGE_SPACE,
INTERNAL_SERVER_ERROR,
LENGTH_REQUIRED,
Expand Down
3 changes: 3 additions & 0 deletions src/twisted/web/newsfragments/12104.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
twisted.web.http.IM_A_TEAPOT was added and returns `I'm a teapot`
as default message for the status code 418,
as defined in RFC 2324 section 2.3.2.
16 changes: 16 additions & 0 deletions src/twisted/web/test/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2890,6 +2890,22 @@ def test_setResponseCodeAcceptsLongIntegers(self):
req = http.Request(DummyChannel(), False)
req.setResponseCode(1)

def test_setResponseCode418(self):
"""
L{http.Request.setResponseCode} supports RFC 2324 section 2.3.2
418 response code and will automatically set the associated message.
"""
channel = DummyChannel()
req = http.Request(channel, False)

req.setResponseCode(http.IM_A_TEAPOT)
req.write(b"")

self.assertEqual(
channel.transport.written.getvalue().splitlines()[0],
b"(no clientproto yet) 418 I'm a teapot",
)

def test_setLastModifiedNeverSet(self):
"""
When no previous value was set and no 'if-modified-since' value was
Expand Down

0 comments on commit 108d99a

Please sign in to comment.