From a30c5673367533aec48faa59dd0d76b283dda1ba Mon Sep 17 00:00:00 2001 From: Pierre Bourdon Date: Sat, 11 May 2024 15:10:45 +0200 Subject: [PATCH] filetransfer: unit test content-encoding handling Very basic behavior test to ensure that gzip data gets internally decompressed by the file transfer pipeline. Change a std::string_view return value in the test harness to std::string. I wouldn't call myself a C++ beginner and I still managed to shoot myself in the foot like three times with the lifetime managements there (e.g. [&] { return an_std_string; } ends up with a dangling string_view!). Change-Id: I1360750d4181ce1ca2a3aa4dc0e97e131351c469 --- tests/unit/libstore/filetransfer.cc | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/unit/libstore/filetransfer.cc b/tests/unit/libstore/filetransfer.cc index ebd38f19d..684697c69 100644 --- a/tests/unit/libstore/filetransfer.cc +++ b/tests/unit/libstore/filetransfer.cc @@ -1,4 +1,5 @@ #include "filetransfer.hh" +#include "compression.hh" #include #include @@ -25,7 +26,7 @@ using namespace std::chrono_literals; namespace nix { static std::tuple -serveHTTP(std::string_view status, std::string_view headers, std::function content) +serveHTTP(std::string_view status, std::string_view headers, std::function content) { AutoCloseFD listener(::socket(AF_INET6, SOCK_STREAM, 0)); if (!listener) { @@ -152,4 +153,17 @@ TEST(FileTransfer, NOT_ON_DARWIN(reportsTransferError)) req.baseRetryTimeMs = 0; ASSERT_THROW(ft->download(req), FileTransferError); } + +TEST(FileTransfer, NOT_ON_DARWIN(handlesContentEncoding)) +{ + std::string original = "Test data string"; + std::string compressed = compress("gzip", original); + + auto [port, srv] = serveHTTP("200 ok", "content-encoding: gzip\r\n", [&] { return compressed; }); + auto ft = makeFileTransfer(); + + StringSink sink; + ft->download(FileTransferRequest(fmt("http://[::1]:%d/index", port)), sink); + EXPECT_EQ(sink.s, original); +} }