• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
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 <algorithm>
6 
7 #include "net/http/http_request_info.h"
8 #include "net/http/http_response_headers.h"
9 #include "net/http/http_vary_data.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 
12 namespace {
13 
14 typedef testing::Test HttpVaryDataTest;
15 
16 struct TestTransaction {
17   net::HttpRequestInfo request;
18   scoped_refptr<net::HttpResponseHeaders> response;
19 
Init__anona157fd190111::TestTransaction20   void Init(const std::string& request_headers,
21             const std::string& response_headers) {
22     std::string temp(response_headers);
23     std::replace(temp.begin(), temp.end(), '\n', '\0');
24     response = new net::HttpResponseHeaders(temp);
25 
26     request.extra_headers.Clear();
27     request.extra_headers.AddHeadersFromString(request_headers);
28   }
29 };
30 
31 }  // namespace
32 
TEST(HttpVaryDataTest,IsInvalid)33 TEST(HttpVaryDataTest, IsInvalid) {
34   // All of these responses should result in an invalid vary data object.
35   const char* kTestResponses[] = {
36     "HTTP/1.1 200 OK\n\n",
37     "HTTP/1.1 200 OK\nVary: *\n\n",
38     "HTTP/1.1 200 OK\nVary: cookie, *, bar\n\n",
39     "HTTP/1.1 200 OK\nVary: cookie\nFoo: 1\nVary: *\n\n",
40   };
41 
42   for (size_t i = 0; i < arraysize(kTestResponses); ++i) {
43     TestTransaction t;
44     t.Init("", kTestResponses[i]);
45 
46     net::HttpVaryData v;
47     EXPECT_FALSE(v.is_valid());
48     EXPECT_FALSE(v.Init(t.request, *t.response));
49     EXPECT_FALSE(v.is_valid());
50   }
51 }
52 
TEST(HttpVaryDataTest,MultipleInit)53 TEST(HttpVaryDataTest, MultipleInit) {
54   net::HttpVaryData v;
55 
56   // Init to something valid.
57   TestTransaction t1;
58   t1.Init("Foo: 1\r\nbar: 23", "HTTP/1.1 200 OK\nVary: foo, bar\n\n");
59   EXPECT_TRUE(v.Init(t1.request, *t1.response));
60   EXPECT_TRUE(v.is_valid());
61 
62   // Now overwrite by initializing to something invalid.
63   TestTransaction t2;
64   t2.Init("Foo: 1\r\nbar: 23", "HTTP/1.1 200 OK\nVary: *\n\n");
65   EXPECT_FALSE(v.Init(t2.request, *t2.response));
66   EXPECT_FALSE(v.is_valid());
67 }
68 
TEST(HttpVaryDataTest,DoesVary)69 TEST(HttpVaryDataTest, DoesVary) {
70   TestTransaction a;
71   a.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n");
72 
73   TestTransaction b;
74   b.Init("Foo: 2", "HTTP/1.1 200 OK\nVary: foo\n\n");
75 
76   net::HttpVaryData v;
77   EXPECT_TRUE(v.Init(a.request, *a.response));
78 
79   EXPECT_FALSE(v.MatchesRequest(b.request, *b.response));
80 }
81 
TEST(HttpVaryDataTest,DoesVary2)82 TEST(HttpVaryDataTest, DoesVary2) {
83   TestTransaction a;
84   a.Init("Foo: 1\r\nbar: 23", "HTTP/1.1 200 OK\nVary: foo, bar\n\n");
85 
86   TestTransaction b;
87   b.Init("Foo: 12\r\nbar: 3", "HTTP/1.1 200 OK\nVary: foo, bar\n\n");
88 
89   net::HttpVaryData v;
90   EXPECT_TRUE(v.Init(a.request, *a.response));
91 
92   EXPECT_FALSE(v.MatchesRequest(b.request, *b.response));
93 }
94 
TEST(HttpVaryDataTest,DoesntVary)95 TEST(HttpVaryDataTest, DoesntVary) {
96   TestTransaction a;
97   a.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n");
98 
99   TestTransaction b;
100   b.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n");
101 
102   net::HttpVaryData v;
103   EXPECT_TRUE(v.Init(a.request, *a.response));
104 
105   EXPECT_TRUE(v.MatchesRequest(b.request, *b.response));
106 }
107 
TEST(HttpVaryDataTest,DoesntVary2)108 TEST(HttpVaryDataTest, DoesntVary2) {
109   TestTransaction a;
110   a.Init("Foo: 1\r\nbAr: 2", "HTTP/1.1 200 OK\nVary: foo, bar\n\n");
111 
112   TestTransaction b;
113   b.Init("Foo: 1\r\nbaR: 2", "HTTP/1.1 200 OK\nVary: foo\nVary: bar\n\n");
114 
115   net::HttpVaryData v;
116   EXPECT_TRUE(v.Init(a.request, *a.response));
117 
118   EXPECT_TRUE(v.MatchesRequest(b.request, *b.response));
119 }
120 
TEST(HttpVaryDataTest,ImplicitCookieForRedirect)121 TEST(HttpVaryDataTest, ImplicitCookieForRedirect) {
122   TestTransaction a;
123   a.Init("Cookie: 1", "HTTP/1.1 301 Moved\nLocation: x\n\n");
124 
125   TestTransaction b;
126   b.Init("Cookie: 2", "HTTP/1.1 301 Moved\nLocation: x\n\n");
127 
128   net::HttpVaryData v;
129   EXPECT_TRUE(v.Init(a.request, *a.response));
130 
131   EXPECT_FALSE(v.MatchesRequest(b.request, *b.response));
132 }
133 
TEST(HttpVaryDataTest,ImplicitCookieForRedirect2)134 TEST(HttpVaryDataTest, ImplicitCookieForRedirect2) {
135   // This should be no different than the test above
136 
137   TestTransaction a;
138   a.Init("Cookie: 1", "HTTP/1.1 301 Moved\nLocation: x\nVary: coOkie\n\n");
139 
140   TestTransaction b;
141   b.Init("Cookie: 2", "HTTP/1.1 301 Moved\nLocation: x\nVary: cooKie\n\n");
142 
143   net::HttpVaryData v;
144   EXPECT_TRUE(v.Init(a.request, *a.response));
145 
146   EXPECT_FALSE(v.MatchesRequest(b.request, *b.response));
147 }
148