Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Expect: 100-continue handling for #267 #268

Closed
wants to merge 10 commits into from
4 changes: 4 additions & 0 deletions aiohttp/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,10 @@ def post(self):
self._post = MultiDictProxy(MultiDict())
return self._post

if self.headers.get('expect', '').lower() == "100-continue":
if self.version == HttpVersion11:
self.transport.write(b"HTTP/1.1 100 Continue\r\n\r\n")

body = yield from self.read()
content_charset = self.charset or 'utf-8'

Expand Down
25 changes: 25 additions & 0 deletions tests/test_web_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,31 @@ def go():

self.loop.run_until_complete(go())

def test_post_100_continue(self):
@asyncio.coroutine
def handler(request):
data = yield from request.post()
self.assertEqual(b'123', data['name'])
return web.Response()

@asyncio.coroutine
def go():
_, _, url = yield from self.create_server('POST', '/', handler)

form = FormData()
form.add_field('name', b'123',
content_transfer_encoding='base64')

resp = yield from request(
'post', url, data=form,
expect100=True, # wait until server returns 100 continue
loop=self.loop)

self.assertEqual(200, resp.status)

self.loop.run_until_complete(
asyncio.wait_for(go(), timeout=0.1, loop=self.loop))

def test_post_json(self):

dct = {'key': 'текст'}
Expand Down
36 changes: 36 additions & 0 deletions tests/test_web_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,42 @@ def test_call_POST_on_GET_request(self):
ret = self.loop.run_until_complete(req.post())
self.assertEqual(CIMultiDict(), ret)

def test_call_POST_for_100_continue(self):
body = b"123"
req = self.make_request(
'POST', '/',
headers=CIMultiDict({"EXPECT": "100-Continue",
'CONTENT-TYPE': ''}))
with mock.patch.object(req, "read", side_effect=StopIteration(body)):
ret = self.loop.run_until_complete(req.post())
self.assertEqual(ret, body)
req.transport.write.assert_called_with(
b'HTTP/1.1 100 Continue\r\n\r\n')

def test_call_POST_for_100_continue_HTTP10(self):
body = b"123"
req = self.make_request(
'POST', '/',
headers=CIMultiDict({"EXPECT": "100-Continue",
'CONTENT-TYPE': ''}),
version=HttpVersion(1, 0))

with mock.patch.object(req, "read", side_effect=StopIteration(body)):
ret = self.loop.run_until_complete(req.post())
self.assertEqual(ret, body)
self.assertFalse(req.transport.write.call_args_list)

def test_call_POST_for_other_expect(self):
body = b"123"
req = self.make_request(
'POST', '/',
headers=CIMultiDict({"EXPECT": "Other-thing",
'CONTENT-TYPE': ''}))
with mock.patch.object(req, "read", side_effect=StopIteration(body)):
ret = self.loop.run_until_complete(req.post())
self.assertEqual(ret, body)
self.assertFalse(req.transport.write.call_args_list)

def test_call_POST_on_weird_content_type(self):
req = self.make_request(
'POST', '/',
Expand Down