|
15 | 15 | #include <cstdint> |
16 | 16 | #include <cstring> |
17 | 17 | #include <filesystem> |
| 18 | +#include <fstream> |
| 19 | +#include <iterator> |
18 | 20 | #include <memory> |
19 | 21 | #include <ranges> |
20 | 22 | #include <regex> |
@@ -313,12 +315,40 @@ TEST(HttpBasic, SimpleGet) { |
313 | 315 | return resp; |
314 | 316 | }); |
315 | 317 | std::string resp = httpGet("/abc"); |
316 | | - ASSERT_FALSE(resp.empty()); |
317 | 318 | ASSERT_TRUE(resp.starts_with("HTTP/1.1 200")); |
318 | 319 | ASSERT_TRUE(resp.contains("You requested: /abc")); |
319 | 320 | ASSERT_TRUE(resp.contains("x-test=abc123")); |
320 | 321 | } |
321 | 322 |
|
| 323 | +TEST(HttpBasic, AccessLogWriterFile) { |
| 324 | + ts.router().setDefault([](const HttpRequestView& req) { return req.makeResponse("OK"); }); |
| 325 | + |
| 326 | + test::ScopedTempDir tmpDir; |
| 327 | + |
| 328 | + std::string logFilePath = tmpDir.dirPath().string() + "/log.txt"; |
| 329 | + |
| 330 | + ts.postConfigUpdate([&logFilePath](HttpServerConfig& cfg) { |
| 331 | + cfg.accessLog.sink = AccessLogConfig::Sink::File; |
| 332 | + cfg.accessLog.filePath = logFilePath; |
| 333 | + cfg.accessLog.flushThresholdInBytes = 0; |
| 334 | + }); |
| 335 | + |
| 336 | + std::string resp = httpGet("/my-path"); |
| 337 | + ASSERT_TRUE(resp.starts_with("HTTP/1.1 200")); |
| 338 | + |
| 339 | + resp = httpGet("/other/path"); |
| 340 | + ASSERT_TRUE(resp.starts_with("HTTP/1.1 200")); |
| 341 | + |
| 342 | + // load contents of logFilePath |
| 343 | + std::ifstream ifs(logFilePath); |
| 344 | + std::string logFileContent((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>())); |
| 345 | + |
| 346 | + ts.postConfigUpdate([](HttpServerConfig& cfg) { cfg.accessLog.sink = AccessLogConfig::Sink::None; }); |
| 347 | + |
| 348 | + EXPECT_TRUE(logFileContent.contains(R"(GET /my-path HTTP/1.1" 200 0 )")); |
| 349 | + EXPECT_TRUE(logFileContent.contains(R"(GET /other/path HTTP/1.1" 200 0 )")); |
| 350 | +} |
| 351 | + |
322 | 352 | TEST(HttpKeepAlive, MultipleSequentialRequests) { |
323 | 353 | ts.router().setDefault([](const HttpRequestView& req) { |
324 | 354 | HttpResponse resp; |
|
0 commit comments