support Unity testing

This commit is contained in:
proddy
2024-08-01 22:13:52 +02:00
parent 4ec5739b67
commit b38ec2f25e
16 changed files with 598 additions and 285 deletions

View File

@@ -64,7 +64,7 @@ void WebAPIService::webAPIService(AsyncWebServerRequest * request, JsonVariant j
}
#ifdef EMSESP_TEST
// for test.cpp so we can invoke GETs to test the API
// for test.cpp and unit tests so we can invoke GETs to test the API
void WebAPIService::webAPIService(AsyncWebServerRequest * request) {
JsonDocument input_doc;
parse(request, input_doc.to<JsonObject>());
@@ -134,6 +134,10 @@ void WebAPIService::parse(AsyncWebServerRequest * request, JsonObject input) {
const char * api_data = output["api_data"];
if (api_data) {
request->send(200, "text/plain; charset=utf-8", api_data);
#if defined(EMSESP_TEST)
// store the result so we can test with Unity later
storeResponse(output);
#endif
#if defined(EMSESP_STANDALONE)
Serial.printf("%sweb output: %s[%s] %s(200)%s ", COLOR_WHITE, COLOR_BRIGHT_CYAN, request->url().c_str(), COLOR_BRIGHT_GREEN, COLOR_MAGENTA);
serializeJson(output, Serial);
@@ -157,6 +161,10 @@ void WebAPIService::parse(AsyncWebServerRequest * request, JsonObject input) {
request->send(response);
api_count_++;
#if defined(EMSESP_TEST)
// store the result so we can test with Unity later
storeResponse(output);
#endif
#if defined(EMSESP_STANDALONE)
Serial.printf("%sweb output: %s[%s]", COLOR_WHITE, COLOR_BRIGHT_CYAN, request->url().c_str());
Serial.printf(" %s(%d)%s ", ret_codes[return_code] == 200 ? COLOR_BRIGHT_GREEN : COLOR_BRIGHT_RED, ret_codes[return_code], COLOR_YELLOW);
@@ -221,4 +229,19 @@ void WebAPIService::getEntities(AsyncWebServerRequest * request) {
request->send(response);
}
#if defined(EMSESP_TEST)
// store the result so we can test with Unity later
static JsonDocument storeResponseDoc_;
void WebAPIService::storeResponse(JsonObject response) {
storeResponseDoc_.clear(); // clear it, so can only recall once
storeResponseDoc_.add(response); // add the object to our doc
}
const char * WebAPIService::getResponse() {
static std::string buffer;
serializeJson(storeResponseDoc_, buffer);
return buffer.c_str();
}
#endif
} // namespace emsesp