From 3bc0e129b37a53b97d9ae12ff8402cc1f35d5c98 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Mon, 12 Jun 2023 10:00:00 +0200 Subject: [PATCH] Add test for automatic decompression in net builtin --- packages/lib/src/tests.rs | 1 + tests/net/request/compression.luau | 56 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 tests/net/request/compression.luau diff --git a/packages/lib/src/tests.rs b/packages/lib/src/tests.rs index 8534027..7d6abd6 100644 --- a/packages/lib/src/tests.rs +++ b/packages/lib/src/tests.rs @@ -49,6 +49,7 @@ create_tests! { fs_move: "fs/move", net_request_codes: "net/request/codes", + net_request_compression: "net/request/compression", net_request_methods: "net/request/methods", net_request_query: "net/request/query", net_request_redirect: "net/request/redirect", diff --git a/tests/net/request/compression.luau b/tests/net/request/compression.luau new file mode 100644 index 0000000..6fd6b49 --- /dev/null +++ b/tests/net/request/compression.luau @@ -0,0 +1,56 @@ +local net = require("@lune/net") + +-- Should decompress automatically by default + +local response = net.request({ + url = "https://httpbingo.org/gzip", + headers = { + ["Accept-Encoding"] = "gzip", + }, +}) + +assert( + response.ok, + "Request failed with status " + .. tostring(response.statusCode) + .. " " + .. tostring(response.statusMessage) +) + +local success, json = pcall(net.jsonDecode, response.body) +assert(success, "Failed to decode json response\n" .. tostring(json)) + +-- Content encoding header should no longer exist when automatically decompressed + +assert( + response.headers["content-encoding"] == nil, + "Content encoding header still exists after automatic decompression" +) + +-- Should do nothing when explicitly disabled + +local response2 = net.request({ + url = "https://httpbingo.org/gzip", + headers = { + ["Accept-Encoding"] = "gzip", + }, + options = { decompress = false }, +}) + +assert( + response2.ok, + "Request failed with status " + .. tostring(response2.statusCode) + .. " " + .. tostring(response2.statusMessage) +) + +local success2 = pcall(net.jsonDecode, response2.body) +assert(not success2, "Decompression disabled still returned json response") + +-- Content encoding header should still exist when not automatically decompressed + +assert( + response2.headers["content-encoding"] ~= nil, + "Content encoding header is missing when automatic decompression is disabled" +)