From 081d06766223b326b6d7eeceae817b7a3a3f57b0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 12 Mar 2020 11:57:15 +1100 Subject: [PATCH] tests/net_inet: Add uasyncio internet tests. --- tests/net_inet/uasyncio_cancel_stream.py | 35 +++++++++++++++++++ tests/net_inet/uasyncio_cancel_stream.py.exp | 5 +++ tests/net_inet/uasyncio_open_connection.py | 30 ++++++++++++++++ .../net_inet/uasyncio_open_connection.py.exp | 5 +++ tests/net_inet/uasyncio_tcp_read_headers.py | 34 ++++++++++++++++++ .../net_inet/uasyncio_tcp_read_headers.py.exp | 10 ++++++ 6 files changed, 119 insertions(+) create mode 100644 tests/net_inet/uasyncio_cancel_stream.py create mode 100644 tests/net_inet/uasyncio_cancel_stream.py.exp create mode 100644 tests/net_inet/uasyncio_open_connection.py create mode 100644 tests/net_inet/uasyncio_open_connection.py.exp create mode 100644 tests/net_inet/uasyncio_tcp_read_headers.py create mode 100644 tests/net_inet/uasyncio_tcp_read_headers.py.exp 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