Refactor WiFiManager: extract server and handlers into separate modules

- Move server lifecycle management to WiFiManagerServer
- Move HTTP handlers and rendering to WiFiManagerHandlers
- Move DEBUG_WM template definitions to WiFiManagerDebug
- Implement lazy initialization of server manager to save memory
- Add proper cleanup in destructor and on multiple createServer calls
- Update tests to use getServer() and getDNSServer() accessors
This commit is contained in:
2025-11-15 22:22:36 +10:00
parent 1788aa3c4a
commit 9cd5f62f9b
14 changed files with 1777 additions and 1753 deletions
@@ -15,13 +15,13 @@ void test_dns_server_created() {
// Wait for DNS server to be created
unsigned long start = millis();
while (!wm.dnsServer && (millis() - start < 1000)) {
while (!wm.getDNSServer() && (millis() - start < 1000)) {
wm.process();
delay(10);
}
// DNS server should be created when portal starts
TEST_ASSERT_NOT_NULL(wm.dnsServer);
TEST_ASSERT_NOT_NULL(wm.getDNSServer());
wm.stopConfigPortal();
@@ -40,13 +40,13 @@ void test_dns_server_cleanup() {
// Wait for DNS server to be created
unsigned long start = millis();
while (!wm.dnsServer && (millis() - start < 1000)) {
while (!wm.getDNSServer() && (millis() - start < 1000)) {
wm.process();
delay(10);
}
// Verify DNS server exists
TEST_ASSERT_NOT_NULL(wm.dnsServer);
TEST_ASSERT_NOT_NULL(wm.getDNSServer());
// Stop portal
wm.stopConfigPortal();
@@ -72,11 +72,11 @@ void test_dns_server_lifecycle_cycles() {
wm.process();
unsigned long start = millis();
while (!wm.dnsServer && (millis() - start < 1000)) {
while (!wm.getDNSServer() && (millis() - start < 1000)) {
wm.process();
delay(10);
}
TEST_ASSERT_NOT_NULL(wm.dnsServer);
TEST_ASSERT_NOT_NULL(wm.getDNSServer());
wm.stopConfigPortal();
wm.process();
@@ -87,11 +87,11 @@ void test_dns_server_lifecycle_cycles() {
wm.process();
start = millis();
while (!wm.dnsServer && (millis() - start < 1000)) {
while (!wm.getDNSServer() && (millis() - start < 1000)) {
wm.process();
delay(10);
}
TEST_ASSERT_NOT_NULL(wm.dnsServer);
TEST_ASSERT_NOT_NULL(wm.getDNSServer());
wm.stopConfigPortal();
wm.process();
@@ -1,6 +1,7 @@
#include <unity.h>
#include <Arduino.h>
#include <WiFiManager.h>
#include <WiFiManagerParameter.h>
#include "../test_main.h"
// Test autoConnect fallback flow
@@ -145,8 +146,8 @@ void test_portal_with_parameters_and_infrastructure() {
TEST_ASSERT_TRUE(wm.getConfigPortalActive());
// Verify infrastructure
TEST_ASSERT_NOT_NULL(wm.server);
TEST_ASSERT_NOT_NULL(wm.dnsServer);
TEST_ASSERT_NOT_NULL(wm.getServer());
TEST_ASSERT_NOT_NULL(wm.getDNSServer());
// Verify parameters
TEST_ASSERT_EQUAL(2, wm.getParametersCount());
@@ -1,6 +1,7 @@
#include <unity.h>
#include <Arduino.h>
#include <WiFiManager.h>
#include <WiFiManagerParameter.h>
#include "../test_main.h"
// Test parameter value set directly
@@ -1,6 +1,7 @@
#include <unity.h>
#include <Arduino.h>
#include <WiFiManager.h>
#include <WiFiManagerParameter.h>
// Test custom parameters
void test_create_parameter() {
@@ -107,10 +107,10 @@ void test_config_portal_infrastructure() {
delay(200); // Give time for initialization
// Verify server object exists (must check while portal is active)
TEST_ASSERT_NOT_NULL_MESSAGE(wm.server, "Server object should exist when portal is active");
TEST_ASSERT_NOT_NULL_MESSAGE(wm.getServer(), "Server object should exist when portal is active");
// Verify DNS server exists
TEST_ASSERT_NOT_NULL_MESSAGE(wm.dnsServer, "DNS server object should exist when portal is active");
TEST_ASSERT_NOT_NULL_MESSAGE(wm.getDNSServer(), "DNS server object should exist when portal is active");
// Verify WiFi is in AP mode
WiFiMode_t mode = WiFi.getMode();
@@ -145,7 +145,7 @@ void test_start_web_portal() {
wm.process();
// Verify server object exists
TEST_ASSERT_NOT_NULL(wm.server);
TEST_ASSERT_NOT_NULL(wm.getServer());
wm.stopWebPortal();
@@ -76,8 +76,8 @@ void test_state_consistency_during_portal() {
// Verify state consistency
TEST_ASSERT_TRUE(wm.getConfigPortalActive());
TEST_ASSERT_NOT_NULL(wm.server);
TEST_ASSERT_NOT_NULL(wm.dnsServer);
TEST_ASSERT_NOT_NULL(wm.getServer());
TEST_ASSERT_NOT_NULL(wm.getDNSServer());
// Process multiple times
for (int i = 0; i < 10; i++) {
+3 -2
View File
@@ -1,6 +1,7 @@
#include <unity.h>
#include <Arduino.h>
#include <WiFiManager.h>
#include <WiFiManagerParameter.h>
#include "../test_main.h"
// Test many start/stop cycles for memory leaks
@@ -56,8 +57,8 @@ void test_long_running_portal() {
TEST_ASSERT_TRUE(wm.getConfigPortalActive());
// Verify infrastructure remains
TEST_ASSERT_NOT_NULL(wm.server);
TEST_ASSERT_NOT_NULL(wm.dnsServer);
TEST_ASSERT_NOT_NULL(wm.getServer());
TEST_ASSERT_NOT_NULL(wm.getDNSServer());
delay(10);
}