tests/net_inet: Add uasyncio internet tests.

This commit is contained in:
Damien George 2020-03-12 11:57:15 +11:00
parent 38904b8937
commit 081d067662
6 changed files with 119 additions and 0 deletions

View File

@ -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"))

View File

@ -0,0 +1,5 @@
start
cancelling
waiting
cancelled
done

View File

@ -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"))

View File

@ -0,0 +1,5 @@
write GET
read response
read: b'HTTP/1.1 200 OK'
close
done

View File

@ -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"))

View File

@ -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