@@ -105,6 +105,15 @@ def test_non_existent(self):
105
105
return self .assertFailure (d , IOError )
106
106
107
107
108
+ class ContentLengthHeaderResource (resource .Resource ):
109
+ """
110
+ A testing resource which renders itself as the value of the Content-Length
111
+ header from the request.
112
+ """
113
+ def render (self , request ):
114
+ return request .requestHeaders .getRawHeaders (b"content-length" )[0 ]
115
+
116
+
108
117
class HttpTestCase (unittest .TestCase ):
109
118
110
119
scheme = 'http'
@@ -122,6 +131,7 @@ def setUp(self):
122
131
r .putChild (b"host" , HostHeaderResource ())
123
132
r .putChild (b"payload" , PayloadResource ())
124
133
r .putChild (b"broken" , BrokenDownloadResource ())
134
+ r .putChild (b"contentlength" , ContentLengthHeaderResource ())
125
135
self .site = server .Site (r , timeout = None )
126
136
self .wrapper = WrappingFactory (self .site )
127
137
self .host = 'localhost'
@@ -209,6 +219,28 @@ def _test(response):
209
219
d .addCallback (self .assertEquals , b'example.com' )
210
220
return d
211
221
222
+ def test_content_length_zero_bodyless_post_request_headers (self ):
223
+ """Tests if "Content-Length: 0" is sent for bodyless POST requests.
224
+
225
+ This is not strictly required by HTTP RFCs but can cause trouble
226
+ for some web servers.
227
+ See:
228
+ https://github.com/scrapy/scrapy/issues/823
229
+ https://issues.apache.org/jira/browse/TS-2902
230
+ https://github.com/kennethreitz/requests/issues/405
231
+ https://bugs.python.org/issue14721
232
+ """
233
+ def _test (response ):
234
+ self .assertEquals (response .body , b'0' )
235
+
236
+ request = Request (self .getURL ('contentlength' ), method = 'POST' , headers = {'Host' : 'example.com' })
237
+ return self .download_request (request , Spider ('foo' )).addCallback (_test )
238
+
239
+ d = self .download_request (request , Spider ('foo' ))
240
+ d .addCallback (lambda r : r .body )
241
+ d .addCallback (self .assertEquals , b'0' )
242
+ return d
243
+
212
244
def test_payload (self ):
213
245
body = b'1' * 100 # PayloadResource requires body length to be 100
214
246
request = Request (self .getURL ('payload' ), method = 'POST' , body = body )
0 commit comments