Add unit testing, stabilize the container and image listing

This commit is contained in:
mrkmntal 2026-08-14 13:37:22 -04:00
commit 91fa50370a
12 changed files with 394 additions and 66 deletions

View file

@ -0,0 +1,27 @@
#include "container_parser.hpp"
#include <cassert>
#include <iostream>
int main() {
const auto result = parseContainerList(R"([
{"Id":"running-id","Names":["/web"],"State":"running","Status":"Up 2 minutes","Ports":[]},
{"Id":"exited-id","Names":["/old"],"State":"exited","Status":"Exited (0) 1 minute ago","Ports":[{"PrivatePort":80,"Type":"tcp"}]},
{"Id":"odd-id","Names":["/odd"],"State":"exited","Ports":"unexpected"}
])");
assert(result.ok());
assert(result.items.size() == 3);
assert(result.items[0].running);
assert(!result.items[1].running);
assert(result.items[1].name == "old");
assert(result.items[1].ports.empty());
assert(result.items[2].name == "odd");
const auto empty = parseContainerList("[]");
assert(empty.ok());
assert(empty.items.empty());
const auto invalid = parseContainerList("not-json");
assert(!invalid.ok());
std::cout << "Container parser tests passed\n";
}

View file

@ -0,0 +1,43 @@
#include "http_response_parser.hpp"
#include <cassert>
#include <iostream>
void testContentLength() {
const auto response = parseHttpResponse(
"HTTP/1.1 200 OK\r\nContent-Length: 6\r\n\r\n[1,2]\n");
if (!response.ok()) std::cerr << response.error << " status=" << response.status_code << " body=" << response.body << "\n";
assert(response.ok());
assert(response.body == "[1,2]\n");
}
void testChunked() {
const auto response = parseHttpResponse(
"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"
"5\r\nhello\r\n6\r\n world\r\n0\r\n\r\n");
assert(response.ok());
assert(response.body == "hello world");
}
void testTruncatedBody() {
const auto response = parseHttpResponse(
"HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nshort");
assert(!response.ok());
assert(response.error == "Truncated HTTP response body.");
}
void testErrorResponse() {
const auto response = parseHttpResponse(
"HTTP/1.1 500 Internal Server Error\r\nContent-Length: 3\r\n\r\nbad");
assert(!response.ok());
assert(response.status_code == 500);
assert(response.error == "bad");
}
int main() {
testContentLength();
testChunked();
testTruncatedBody();
testErrorResponse();
std::cout << "HTTP response parser tests passed\n";
}