MCPcopy Index your code
hub / github.com/python/cpython / test_content_length_0

Method test_content_length_0

Lib/test/test_httplib.py:167–231  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

165 self.assertEqual(conn._buffer.count[header.lower()], 1)
166
167 def test_content_length_0(self):
168
169 class ContentLengthChecker(list):
170 def __init__(self):
171 list.__init__(self)
172 self.content_length = None
173 def append(self, item):
174 kv = item.split(b':', 1)
175 if len(kv) > 1 and kv[0].lower() == b'content-length':
176 self.content_length = kv[1].strip()
177 list.append(self, item)
178
179 # Here, we're testing that methods expecting a body get a
180 # content-length set to zero if the body is empty (either None or '')
181 bodies = (None, '')
182 methods_with_body = ('PUT', 'POST', 'PATCH')
183 for method, body in itertools.product(methods_with_body, bodies):
184 conn = client.HTTPConnection('example.com')
185 conn.sock = FakeSocket(None)
186 conn._buffer = ContentLengthChecker()
187 conn.request(method, '/', body)
188 self.assertEqual(
189 conn._buffer.content_length, b'0',
190 'Header Content-Length incorrect on {}'.format(method)
191 )
192
193 # For these methods, we make sure that content-length is not set when
194 # the body is None because it might cause unexpected behaviour on the
195 # server.
196 methods_without_body = (
197 'GET', 'CONNECT', 'DELETE', 'HEAD', 'OPTIONS', 'TRACE',
198 )
199 for method in methods_without_body:
200 conn = client.HTTPConnection('example.com')
201 conn.sock = FakeSocket(None)
202 conn._buffer = ContentLengthChecker()
203 conn.request(method, '/', None)
204 self.assertEqual(
205 conn._buffer.content_length, None,
206 'Header Content-Length set for empty body on {}'.format(method)
207 )
208
209 # If the body is set to '', that's considered to be "present but
210 # empty" rather than "missing", so content length would be set, even
211 # for methods that don't expect a body.
212 for method in methods_without_body:
213 conn = client.HTTPConnection('example.com')
214 conn.sock = FakeSocket(None)
215 conn._buffer = ContentLengthChecker()
216 conn.request(method, '/', '')
217 self.assertEqual(
218 conn._buffer.content_length, b'0',
219 'Header Content-Length incorrect on {}'.format(method)
220 )
221
222 # If the body is set, make sure Content-Length is set.
223 for method in itertools.chain(methods_without_body, methods_with_body):
224 conn = client.HTTPConnection('example.com')

Callers

nothing calls this directly

Calls 6

requestMethod · 0.95
chainMethod · 0.80
FakeSocketClass · 0.70
assertEqualMethod · 0.45
formatMethod · 0.45

Tested by

no test coverage detected