extends TestCase ## Issue N7: the dedicated server's health endpoint answers GET /health with JSON, ## refuses everything else, and drops requests that never finish. var health: HealthServer func before_each() -> void: health = HealthServer.new() health.report = func() -> Dictionary: return {"phase": "lobby", "players": 2} assert_eq(health.listen(0), OK) func after_each() -> void: health.close() ## Sends `request` and returns everything the server says before it closes. func _ask(request: String, limit_msec: int = 3000) -> String: var peer: StreamPeerTCP = StreamPeerTCP.new() assert_eq(peer.connect_to_host("127.0.0.1", health.port()), OK) var reply: PackedByteArray = PackedByteArray() var sent: bool = false var started: int = Time.get_ticks_msec() while Time.get_ticks_msec() - started < limit_msec: health.poll() peer.poll() var status: int = peer.get_status() if status == StreamPeerTCP.STATUS_CONNECTED: if not sent and not request.is_empty(): peer.put_data(request.to_ascii_buffer()) sent = true var available: int = peer.get_available_bytes() if available > 0: reply.append_array(peer.get_data(available)[1]) elif status != StreamPeerTCP.STATUS_CONNECTING: break OS.delay_msec(5) peer.disconnect_from_host() return reply.get_string_from_utf8() func test_health_answers_with_json() -> void: var reply: String = _ask("GET /health HTTP/1.1\r\nHost: x\r\n\r\n") assert_true(reply.begins_with("HTTP/1.0 200 OK\r\n"), reply) var body: Variant = JSON.parse_string(reply.get_slice("\r\n\r\n", 1)) assert_eq(body, {"status": "ok", "phase": "lobby", "players": 2.0}) assert_eq(health.answered.get(200, 0), 1) func test_other_paths_and_methods_are_not_found() -> void: assert_true(_ask("GET /admin HTTP/1.0\r\n\r\n").begins_with("HTTP/1.0 404")) assert_true(_ask("POST /health HTTP/1.0\r\n\r\n").begins_with("HTTP/1.0 404")) assert_eq(health.answered.get(404, 0), 2) func test_unfinished_and_oversized_requests_are_dropped() -> void: assert_eq(_ask("GET /health HTTP/1.0\r\n", 2600), "", "never finished: closed without an answer") assert_eq(_ask("GET /" + "a".repeat(HealthServer.MAX_REQUEST_BYTES) + " HTTP/1.0\r\n\r\n"), "") assert_eq(health.answered.size(), 0)