2025-07-08 17:30:46 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
from mosq_test_helper import *
|
2026-05-11 00:00:20 +02:00
|
|
|
|
|
|
|
|
from broker_config import BrokerConfig, ListenerConfig
|
|
|
|
|
from mosquitto_broker import MosquittoBroker
|
2025-07-08 17:30:46 +02:00
|
|
|
import http.client
|
|
|
|
|
import json
|
|
|
|
|
|
2026-03-10 13:21:00 +01:00
|
|
|
mosq_test.require_features(["WITH_HTTP_API"])
|
|
|
|
|
|
2026-05-11 00:00:20 +02:00
|
|
|
def write_index(port):
|
2025-07-08 17:30:46 +02:00
|
|
|
with open("index.html", 'w') as f:
|
|
|
|
|
f.write("<html></html>")
|
|
|
|
|
|
|
|
|
|
mqtt_port, http_port = mosq_test.get_port(2)
|
|
|
|
|
|
2026-05-11 00:00:20 +02:00
|
|
|
broker_config = BrokerConfig(
|
|
|
|
|
listeners=[
|
|
|
|
|
ListenerConfig(port=mqtt_port),
|
|
|
|
|
ListenerConfig(
|
|
|
|
|
port=http_port,
|
2026-05-18 19:46:15 +02:00
|
|
|
address="127.0.0.1",
|
2026-05-11 00:00:20 +02:00
|
|
|
protocol="http_api",
|
2026-05-18 19:46:15 +02:00
|
|
|
http_dir=Path("."),
|
2026-05-11 00:00:20 +02:00
|
|
|
)
|
|
|
|
|
],
|
|
|
|
|
allow_anonymous=True
|
|
|
|
|
)
|
|
|
|
|
write_index(mqtt_port)
|
|
|
|
|
|
|
|
|
|
broker = MosquittoBroker(config=broker_config)
|
|
|
|
|
broker.add_extra_file("index.html")
|
|
|
|
|
with broker:
|
2025-07-08 17:30:46 +02:00
|
|
|
http_conn = http.client.HTTPConnection(f"localhost:{http_port}")
|
|
|
|
|
|
|
|
|
|
# Bad request
|
|
|
|
|
http_conn.request("POST", "/post")
|
|
|
|
|
response = http_conn.getresponse()
|
|
|
|
|
if response.status != 405:
|
2025-10-14 01:18:37 +02:00
|
|
|
raise ValueError(f"Error: /post {response.status}")
|
2025-07-08 17:30:46 +02:00
|
|
|
|
|
|
|
|
# Bad request
|
|
|
|
|
http_conn.request("PUT", "/put")
|
|
|
|
|
response = http_conn.getresponse()
|
|
|
|
|
if response.status != 405:
|
2025-10-14 01:18:37 +02:00
|
|
|
raise ValueError(f"Error: /put {response.status}")
|
2025-07-08 17:30:46 +02:00
|
|
|
|
|
|
|
|
# Missing file
|
|
|
|
|
http_conn.request("GET", "/missing")
|
|
|
|
|
response = http_conn.getresponse()
|
|
|
|
|
if response.status != 404:
|
2025-10-14 01:18:37 +02:00
|
|
|
raise ValueError(f"Error: /api/missing {response.status}")
|
2025-07-08 17:30:46 +02:00
|
|
|
|
|
|
|
|
# File not in dir
|
|
|
|
|
http_conn.request("GET", "../../../../../../../../etc/passwd")
|
|
|
|
|
response = http_conn.getresponse()
|
|
|
|
|
if response.status != 404:
|
2025-10-14 01:18:37 +02:00
|
|
|
raise ValueError(f"Error: ../../../../../../../../etc/passwd {response.status}")
|
2025-07-08 17:30:46 +02:00
|
|
|
|
|
|
|
|
# Present file
|
|
|
|
|
http_conn.request("GET", "/index.html")
|
|
|
|
|
response = http_conn.getresponse()
|
|
|
|
|
if response.status != 200:
|
2025-10-14 01:18:37 +02:00
|
|
|
raise ValueError(f"Error: /index.html {response.status}")
|
2025-07-08 17:30:46 +02:00
|
|
|
|
|
|
|
|
# Root
|
|
|
|
|
http_conn.request("GET", "/")
|
|
|
|
|
response = http_conn.getresponse()
|
|
|
|
|
if response.status != 200:
|
2025-10-14 01:18:37 +02:00
|
|
|
raise ValueError(f"Error: / {response.status}")
|
2025-07-08 17:30:46 +02:00
|
|
|
payload = response.read().decode('utf-8')
|
|
|
|
|
if payload != "<html></html>":
|
2025-10-14 01:18:37 +02:00
|
|
|
raise ValueError(f"Error: / {payload}")
|