From 0f141e09ea26867f9094bbea1efa2077e5438b3c Mon Sep 17 00:00:00 2001 From: Florian Loitsch Date: Thu, 14 Mar 2024 10:08:15 +0100 Subject: [PATCH] Add a way to get all keys of the headers. (#131) Fixes #120. --- src/headers.toit | 7 +++++++ tests/headers_test.toit | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/src/headers.toit b/src/headers.toit index 5f8d0bf..f15538a 100644 --- a/src/headers.toit +++ b/src/headers.toit @@ -23,6 +23,13 @@ class Headers: constructor.private_ .headers_: + /** + The keys of the header. + */ + keys -> List: + if not headers_: return [] + return headers_.keys + /** Returns a single string value for the header or null if the header is not present. If there are multiple values, the last value is returned. diff --git a/tests/headers_test.toit b/tests/headers_test.toit index 279902a..afd421f 100644 --- a/tests/headers_test.toit +++ b/tests/headers_test.toit @@ -8,6 +8,7 @@ import http main: test_from_map + test_keys /** Converts the given $headers to a string. @@ -35,3 +36,10 @@ test_from_map: headers = http.Headers.from_map {"foo": ["bar", "baz"], "Foo": "corge"} expect_equals "Foo: bar\r\nFoo: baz\r\nFoo: corge\r\n" (stringify headers) + +test_keys: + headers := http.Headers.from_map {"foo": ["bar", "baz"], "qux": "quux"} + expect_list_equals ["Foo", "Qux"] headers.keys + + headers = http.Headers + expect_list_equals [] headers.keys