Skip to content

Commit b9b7c23

Browse files
paulefoepaulefoaawarecan
authored
Allow sending request with different methods during connection (#66)
* Allow sending request with different methods during connection, add unit test, update the doc * Fix test Co-authored-by: Pavel Filatov <[email protected]> Co-authored-by: Jason Hu <[email protected]>
1 parent 78a5c6e commit b9b7c23

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

aiohttp_sse_client/client.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,18 @@ def __init__(self, url: str,
6969
7070
:param url: specifies the URL to which to connect
7171
:param option: specifies the settings, if any,
72-
in the form of an Dict[str, Any]. Reserved for future use
72+
in the form of an Dict[str, Any]. Accept the "method" key for
73+
specifying the HTTP method with which connection
74+
should be established
7375
:param reconnection_time: wait time before try to reconnect in case
7476
connection broken
7577
:param session: specifies a aiohttp.ClientSession, if not, create
7678
a default ClientSession
7779
:param on_open: event handler for open event
7880
:param on_message: event handler for message event
7981
:param on_error: event handler for error event
80-
:param kwargs: keyword arguments will pass to underlying aiohttp get()
81-
method.
82+
:param kwargs: keyword arguments will pass to underlying
83+
aiohttp request() method.
8284
"""
8385
self._url = URL(url)
8486
self._ready_state = READY_STATE_CONNECTING
@@ -109,6 +111,8 @@ def __init__(self, url: str,
109111
self._origin = None
110112
self._response = None
111113

114+
self._method = 'GET' if option is None else option.get('method', 'GET')
115+
112116
def __enter__(self):
113117
"""Use async with instead."""
114118
raise TypeError("Use async with instead")
@@ -207,7 +211,11 @@ async def connect(self, retry=0):
207211
headers[hdrs.CACHE_CONTROL] = 'no-cache'
208212

209213
try:
210-
response = await self._session.get(self._url, **self._kwargs)
214+
response = await self._session.request(
215+
self._method,
216+
self._url,
217+
**self._kwargs
218+
)
211219
except ClientConnectionError:
212220
if retry <= 0 or self._ready_state == READY_STATE_CLOSED:
213221
await self._fail_connect()

tests/web_platform_tests/test_request.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
from datetime import datetime, timedelta
43

54
import pytest
65

76
from aiohttp_sse_client import client as sse_client
87

98
from .const import WPT_SERVER
109

10+
1111
async def test_rquest_accept():
1212
"""Test EventSource: Accept header.
13-
13+
1414
..seealso: https://github.com/web-platform-tests/wpt/blob/master/
1515
eventsource/request-accept.htm
1616
"""
@@ -25,7 +25,7 @@ async def test_rquest_accept():
2525

2626
async def test_rquest_cache_control():
2727
"""Test EventSource: Cache-Control.
28-
28+
2929
..seealso: https://github.com/web-platform-tests/wpt/blob/master/
3030
eventsource/request-cache-control.htm
3131
"""
@@ -40,14 +40,14 @@ async def test_rquest_cache_control():
4040

4141
async def test_rquest_redirect():
4242
"""Test EventSource: redirect.
43-
43+
4444
..seealso: https://github.com/web-platform-tests/wpt/blob/master/
4545
eventsource/request-redirect.htm
4646
"""
4747
async def test(status):
4848
def on_error():
4949
assert False
50-
50+
5151
def on_open():
5252
assert source.ready_state == sse_client.READY_STATE_OPEN
5353

@@ -68,7 +68,7 @@ def on_open():
6868

6969
async def test_rquest_status_error():
7070
"""Test EventSource: redirect.
71-
71+
7272
..seealso: https://github.com/web-platform-tests/wpt/blob/master/
7373
eventsource/request-status-error.htm
7474
"""
@@ -93,3 +93,17 @@ def on_message():
9393
await test(404)
9494
await test(410)
9595
await test(503)
96+
97+
98+
async def test_request_post_to_connect():
99+
"""Test EventSource option method for connection.
100+
"""
101+
source = sse_client.EventSource(
102+
WPT_SERVER + 'resources/message.py',
103+
option={'method': "POST"}
104+
)
105+
await source.connect()
106+
async for e in source:
107+
assert e.data == "data"
108+
break
109+
await source.close()

0 commit comments

Comments
 (0)