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
This commit is contained in:
Pierre Bourdon 2024-05-11 15:10:45 +02:00
parent 38d825b21e
commit a30c567336
No known key found for this signature in database
GPG key ID: 6FB80DCD84DA0F1C

View file

@ -1,4 +1,5 @@
#include "filetransfer.hh" #include "filetransfer.hh"
#include "compression.hh"
#include <cstdint> #include <cstdint>
#include <exception> #include <exception>
@ -25,7 +26,7 @@ using namespace std::chrono_literals;
namespace nix { namespace nix {
static std::tuple<uint16_t, AutoCloseFD> static std::tuple<uint16_t, AutoCloseFD>
serveHTTP(std::string_view status, std::string_view headers, std::function<std::string_view()> content) serveHTTP(std::string_view status, std::string_view headers, std::function<std::string()> content)
{ {
AutoCloseFD listener(::socket(AF_INET6, SOCK_STREAM, 0)); AutoCloseFD listener(::socket(AF_INET6, SOCK_STREAM, 0));
if (!listener) { if (!listener) {
@ -152,4 +153,17 @@ TEST(FileTransfer, NOT_ON_DARWIN(reportsTransferError))
req.baseRetryTimeMs = 0; req.baseRetryTimeMs = 0;
ASSERT_THROW(ft->download(req), FileTransferError); 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);
}
} }