diff --git a/Meta/gn/secondary/Tests/LibWeb/BUILD.gn b/Meta/gn/secondary/Tests/LibWeb/BUILD.gn index c2b69538ffe..9f6efc97a31 100644 --- a/Meta/gn/secondary/Tests/LibWeb/BUILD.gn +++ b/Meta/gn/secondary/Tests/LibWeb/BUILD.gn @@ -12,6 +12,12 @@ unittest("TestCSSPixels") { deps = [ "//Userland/Libraries/LibWeb" ] } +unittest("TestFetchInfrastructure") { + include_dirs = [ "//Userland/Libraries" ] + sources = [ "TestFetchInfrastructure.cpp" ] + deps = [ "//Userland/Libraries/LibWeb" ] +} + unittest("TestFetchURL") { include_dirs = [ "//Userland/Libraries" ] sources = [ "TestFetchURL.cpp" ] @@ -50,6 +56,7 @@ group("LibWeb") { deps = [ ":TestCSSIDSpeed", ":TestCSSPixels", + ":TestFetchInfrastructure", ":TestFetchURL", ":TestHTMLTokenizer", ":TestMicrosyntax", diff --git a/Tests/LibWeb/CMakeLists.txt b/Tests/LibWeb/CMakeLists.txt index 9253df290b1..65268e0a40b 100644 --- a/Tests/LibWeb/CMakeLists.txt +++ b/Tests/LibWeb/CMakeLists.txt @@ -1,6 +1,7 @@ set(TEST_SOURCES TestCSSIDSpeed.cpp TestCSSPixels.cpp + TestFetchInfrastructure.cpp TestFetchURL.cpp TestHTMLTokenizer.cpp TestMicrosyntax.cpp diff --git a/Tests/LibWeb/TestFetchInfrastructure.cpp b/Tests/LibWeb/TestFetchInfrastructure.cpp new file mode 100644 index 00000000000..0ac6618733e --- /dev/null +++ b/Tests/LibWeb/TestFetchInfrastructure.cpp @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2024, Tim Flynn + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +#include +#include +#include + +TEST_CASE(collect_an_http_quoted_string) +{ + { + auto test = "\"\""_string; + GenericLexer lexer { test }; + + auto result = MUST(Web::Fetch::Infrastructure::collect_an_http_quoted_string(lexer)); + EXPECT_EQ(result, "\"\""_string); + } + { + auto test = "\"abc\""_string; + GenericLexer lexer { test }; + + auto result = MUST(Web::Fetch::Infrastructure::collect_an_http_quoted_string(lexer)); + EXPECT_EQ(result, "\"abc\""_string); + } + { + auto test = "foo \"abc\""_string; + + GenericLexer lexer { test }; + lexer.ignore(4); + + auto result = MUST(Web::Fetch::Infrastructure::collect_an_http_quoted_string(lexer)); + EXPECT_EQ(result, "\"abc\""_string); + } + { + auto test = "foo=\"abc\""_string; + + GenericLexer lexer { test }; + lexer.ignore(4); + + auto result = MUST(Web::Fetch::Infrastructure::collect_an_http_quoted_string(lexer)); + EXPECT_EQ(result, "\"abc\""_string); + } + { + auto test = "foo=\"abc\" bar"_string; + + GenericLexer lexer { test }; + lexer.ignore(4); + + auto result = MUST(Web::Fetch::Infrastructure::collect_an_http_quoted_string(lexer)); + EXPECT_EQ(result, "\"abc\""_string); + } + { + auto test = "\"abc\" bar"_string; + GenericLexer lexer { test }; + + auto result = MUST(Web::Fetch::Infrastructure::collect_an_http_quoted_string(lexer)); + EXPECT_EQ(result, "\"abc\""_string); + } +} diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP.cpp index 8f8df4c3a83..9e9ed333e22 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP.cpp @@ -72,10 +72,7 @@ ErrorOr collect_an_http_quoted_string(GenericLexer& lexer, HttpQuotedStr return value.to_string(); // 7. Return the code points from positionStart to position, inclusive, within input. - auto position = lexer.tell(); - auto number_of_characters_to_consume = position - position_start + 1; - lexer.retreat(number_of_characters_to_consume); - return String::from_utf8(lexer.consume(number_of_characters_to_consume)); + return String::from_utf8(lexer.input().substring_view(position_start, lexer.tell() - position_start)); } }