1 // Copyright 2024 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/http/http_cookie_indices.h"
6
7 #include "net/http/http_response_headers.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace net {
12 namespace {
13
14 using ::testing::ElementsAre;
15 using ::testing::Optional;
16
17 constexpr std::string_view kCookieIndicesHeader = "Cookie-Indices";
18
TEST(CookieIndicesTest,Absent)19 TEST(CookieIndicesTest, Absent) {
20 auto headers =
21 HttpResponseHeaders::Builder(HttpVersion(1, 1), "200 OK").Build();
22 auto result = ParseCookieIndices(*headers);
23 EXPECT_FALSE(result.has_value());
24 }
25
TEST(CookieIndicesTest,PresentButEmpty)26 TEST(CookieIndicesTest, PresentButEmpty) {
27 auto headers = HttpResponseHeaders::Builder(HttpVersion(1, 1), "200 OK")
28 .AddHeader(kCookieIndicesHeader, "")
29 .Build();
30 auto result = ParseCookieIndices(*headers);
31 EXPECT_THAT(result, Optional(ElementsAre()));
32 }
33
TEST(CookieIndicesTest,OneCookie)34 TEST(CookieIndicesTest, OneCookie) {
35 auto headers = HttpResponseHeaders::Builder(HttpVersion(1, 1), "200 OK")
36 .AddHeader(kCookieIndicesHeader, R"("alpha")")
37 .Build();
38 auto result = ParseCookieIndices(*headers);
39 EXPECT_THAT(result, Optional(ElementsAre("alpha")));
40 }
41
TEST(CookieIndicesTest,SeveralCookies)42 TEST(CookieIndicesTest, SeveralCookies) {
43 auto headers =
44 HttpResponseHeaders::Builder(HttpVersion(1, 1), "200 OK")
45 .AddHeader(kCookieIndicesHeader, R"("alpha", "bravo")")
46 .AddHeader(kCookieIndicesHeader, R"("charlie", "delta", "echo")")
47 .Build();
48 auto result = ParseCookieIndices(*headers);
49 EXPECT_THAT(result, Optional(ElementsAre("alpha", "bravo", "charlie", "delta",
50 "echo")));
51 }
52
TEST(CookieIndicesTest,NonRfc6265Cookie)53 TEST(CookieIndicesTest, NonRfc6265Cookie) {
54 auto headers = HttpResponseHeaders::Builder(HttpVersion(1, 1), "200 OK")
55 .AddHeader(kCookieIndicesHeader, R"("text/html")")
56 .Build();
57 auto result = ParseCookieIndices(*headers);
58 EXPECT_THAT(result, Optional(ElementsAre()));
59 }
60
TEST(CookieIndicesTest,NotAList)61 TEST(CookieIndicesTest, NotAList) {
62 auto headers = HttpResponseHeaders::Builder(HttpVersion(1, 1), "200 OK")
63 .AddHeader(kCookieIndicesHeader, ",,,")
64 .Build();
65 auto result = ParseCookieIndices(*headers);
66 EXPECT_FALSE(result.has_value());
67 }
68
TEST(CookieIndicesTest,InnerList)69 TEST(CookieIndicesTest, InnerList) {
70 auto headers = HttpResponseHeaders::Builder(HttpVersion(1, 1), "200 OK")
71 .AddHeader(kCookieIndicesHeader, R"(("foo"))")
72 .Build();
73 auto result = ParseCookieIndices(*headers);
74 EXPECT_FALSE(result.has_value());
75 }
76
TEST(CookieIndicesTest,Token)77 TEST(CookieIndicesTest, Token) {
78 auto headers = HttpResponseHeaders::Builder(HttpVersion(1, 1), "200 OK")
79 .AddHeader(kCookieIndicesHeader, R"(alpha)")
80 .Build();
81 auto result = ParseCookieIndices(*headers);
82 EXPECT_FALSE(result.has_value());
83 }
84
TEST(CookieIndicesTest,StringWithUnrecognizedParam)85 TEST(CookieIndicesTest, StringWithUnrecognizedParam) {
86 auto headers = HttpResponseHeaders::Builder(HttpVersion(1, 1), "200 OK")
87 .AddHeader(kCookieIndicesHeader, R"("session"; secure)")
88 .Build();
89 auto result = ParseCookieIndices(*headers);
90 EXPECT_THAT(result, Optional(ElementsAre("session")));
91 }
92
93 } // namespace
94 } // namespace net
95