Files
Viper_RegressionTesting/WebTest/App/main.vp
T
TermiNexus ee6461be46 chore(comments): Test 仓注释英文化(R11)
把 81 个 .vp 文件的中文 # 注释与三引号文档串改写为简洁英文,每个连续注释块 <=2 行;运行时中文字符串字面量(testcheck.begin/print 等用户可见消息)保持原样。骨架比对 HEAD 与工作区 112 文件漂移 0,全量 77/77 顺序回归通过。
2026-10-05 22:43:38 +08:00

81 lines
2.6 KiB
Plaintext

import t, c
from stdint import *
import stdio
import stdlib
import socket
import memhub
import requests
# Global MBuddy pointer
_mbuddy: memhub.MemBuddy*
@t.CExport
def main() -> int:
global _mbuddy
stdio.printf("WebTest: HTTP client library test\n")
# Initialize the memory manager (16MB arena)
arena: bytes = stdlib.malloc(16 * 1024 * 1024)
if arena is None:
stdio.printf("FATAL: malloc arena failed\n")
return 1
_mbuddy = memhub.MemBuddy(arena, 16 * 1024 * 1024)
requests._mbuddy = _mbuddy
# Initialize the socket library
result: INT = socket.SocketInit()
stdio.printf("SocketInit: %d\n", result)
# Test the Socket OOP API
sock = socket.Socket(socket.AF_INET, socket.SOCK_STREAM)
if sock.fd != socket.INVALID_SOCKET:
stdio.printf("Socket: OK (fd=%d)\n", sock.fd)
sock.close()
stdio.printf("Socket.close: OK\n")
else:
stdio.printf("Socket: FAIL\n")
# Test DNS resolution indirectly via Socket.connect (calls _MakeAddr internally)
stdio.printf("\n--- DNS test ---\n")
dns_sock = socket.Socket(socket.AF_INET, socket.SOCK_STREAM)
if dns_sock.fd != socket.INVALID_SOCKET:
dns_sock.settimeout(5000)
result: INT = dns_sock.connect("cn.bing.com", 80)
if result == 0:
stdio.printf("DNS: cn.bing.com resolved OK\n")
else:
stdio.printf("DNS: cn.bing.com resolved but connect failed (err in _MakeAddr debug above)\n")
dns_sock.close()
# Test HTTP GET via cn.bing.com (example.com is unreachable on some networks)
stdio.printf("\n--- HTTP GET test (cn.bing.com) ---\n")
resp = requests.get("http://cn.bing.com/")
if resp is not None:
stdio.printf("requests.get: OK\n")
code: INT = resp.status_code
length: INT = resp.body_len
stdio.printf(" status_code: %d\n", code)
stdio.printf(" body_len: %d\n", length)
if resp.text is not None and length > 0:
print_len: INT = 300
if length < print_len:
print_len = length
stdio.printf(" text (first %d chars):\n", print_len)
txt: str = resp.text
i: INT = 0
while i < print_len and txt[i] != 0:
stdio.printf("%c", txt[i])
i += 1
stdio.printf("\n")
requests.release(resp)
else:
stdio.printf("requests.get: FAIL (network unavailable)\n")
# Cleanup
socket.SocketCleanup()
stdio.printf("\nSocketCleanup: OK\n")
stdio.printf("WebTest: ALL PASSED\n")
return 0