aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDamien George2020-03-12 11:57:15 +1100
committerDamien George2020-03-26 01:25:45 +1100
commit081d06766223b326b6d7eeceae817b7a3a3f57b0 (patch)
treee00b558628d811841bda757f146efdb5bb3342dd /tests
parent38904b89376bf628f7d70174204b5330618d49c0 (diff)
tests/net_inet: Add uasyncio internet tests.
Diffstat (limited to 'tests')
-rw-r--r--tests/net_inet/uasyncio_cancel_stream.py35
-rw-r--r--tests/net_inet/uasyncio_cancel_stream.py.exp5
-rw-r--r--tests/net_inet/uasyncio_open_connection.py30
-rw-r--r--tests/net_inet/uasyncio_open_connection.py.exp5
-rw-r--r--tests/net_inet/uasyncio_tcp_read_headers.py34
-rw-r--r--tests/net_inet/uasyncio_tcp_read_headers.py.exp10
6 files changed, 119 insertions, 0 deletions
diff --git a/tests/net_inet/uasyncio_cancel_stream.py b/tests/net_inet/uasyncio_cancel_stream.py
new file mode 100644
index 000000000..6b6b845b0
--- /dev/null
+++ b/tests/net_inet/uasyncio_cancel_stream.py
@@ -0,0 +1,35 @@
+# Test cancelling a task waiting on stream IO
+
+try:
+ import uasyncio as asyncio
+except ImportError:
+ try:
+ import asyncio
+ except ImportError:
+ print("SKIP")
+ raise SystemExit
+
+
+async def get(reader):
+ print("start")
+ try:
+ await reader.read(10)
+ print("fail")
+ except asyncio.CancelledError:
+ print("cancelled")
+
+
+async def main(url):
+ reader, writer = await asyncio.open_connection(url, 80)
+ task = asyncio.create_task(get(reader))
+ await asyncio.sleep(0)
+ print("cancelling")
+ task.cancel()
+ print("waiting")
+ await task
+ print("done")
+ writer.close()
+ await writer.wait_closed()
+
+
+asyncio.run(main("micropython.org"))
diff --git a/tests/net_inet/uasyncio_cancel_stream.py.exp b/tests/net_inet/uasyncio_cancel_stream.py.exp
new file mode 100644
index 000000000..e3fcfa7b3
--- /dev/null
+++ b/tests/net_inet/uasyncio_cancel_stream.py.exp
@@ -0,0 +1,5 @@
+start
+cancelling
+waiting
+cancelled
+done
diff --git a/tests/net_inet/uasyncio_open_connection.py b/tests/net_inet/uasyncio_open_connection.py
new file mode 100644
index 000000000..68285a243
--- /dev/null
+++ b/tests/net_inet/uasyncio_open_connection.py
@@ -0,0 +1,30 @@
+# Test simple HTTP request with uasyncio.open_connection()
+
+try:
+ import uasyncio as asyncio
+except ImportError:
+ try:
+ import asyncio
+ except ImportError:
+ print("SKIP")
+ raise SystemExit
+
+
+async def http_get(url):
+ reader, writer = await asyncio.open_connection(url, 80)
+
+ print("write GET")
+ writer.write(b"GET / HTTP/1.0\r\n\r\n")
+ await writer.drain()
+
+ print("read response")
+ data = await reader.read(100)
+ print("read:", data.split(b"\r\n")[0])
+
+ print("close")
+ writer.close()
+ await writer.wait_closed()
+ print("done")
+
+
+asyncio.run(http_get("micropython.org"))
diff --git a/tests/net_inet/uasyncio_open_connection.py.exp b/tests/net_inet/uasyncio_open_connection.py.exp
new file mode 100644
index 000000000..c8dea365b
--- /dev/null
+++ b/tests/net_inet/uasyncio_open_connection.py.exp
@@ -0,0 +1,5 @@
+write GET
+read response
+read: b'HTTP/1.1 200 OK'
+close
+done
diff --git a/tests/net_inet/uasyncio_tcp_read_headers.py b/tests/net_inet/uasyncio_tcp_read_headers.py
new file mode 100644
index 000000000..8e4375a4f
--- /dev/null
+++ b/tests/net_inet/uasyncio_tcp_read_headers.py
@@ -0,0 +1,34 @@
+# Test uasyncio.open_connection() and stream readline()
+
+try:
+ import uasyncio as asyncio
+except ImportError:
+ try:
+ import asyncio
+ except ImportError:
+ print("SKIP")
+ raise SystemExit
+
+
+async def http_get_headers(url):
+ reader, writer = await asyncio.open_connection(url, 80)
+
+ print("write GET")
+ writer.write(b"GET / HTTP/1.0\r\n\r\n")
+ await writer.drain()
+
+ while True:
+ line = await reader.readline()
+ line = line.strip()
+ if not line:
+ break
+ if line.find(b"Date") == -1 and line.find(b"Modified") == -1 and line.find(b"Server") == -1:
+ print(line)
+
+ print("close")
+ writer.close()
+ await writer.wait_closed()
+ print("done")
+
+
+asyncio.run(http_get_headers("micropython.org"))
diff --git a/tests/net_inet/uasyncio_tcp_read_headers.py.exp b/tests/net_inet/uasyncio_tcp_read_headers.py.exp
new file mode 100644
index 000000000..c200238dc
--- /dev/null
+++ b/tests/net_inet/uasyncio_tcp_read_headers.py.exp
@@ -0,0 +1,10 @@
+write GET
+b'HTTP/1.1 200 OK'
+b'Content-Type: text/html'
+b'Content-Length: 54'
+b'Connection: close'
+b'Vary: Accept-Encoding'
+b'ETag: "54306c85-36"'
+b'Accept-Ranges: bytes'
+close
+done