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.
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"
16 typedef testing::Test HttpVaryDataTest
;
18 struct TestTransaction
{
19 HttpRequestInfo request
;
20 scoped_refptr
<HttpResponseHeaders
> response
;
22 void Init(const std::string
& request_headers
,
23 const std::string
& response_headers
) {
24 std::string
temp(response_headers
);
25 std::replace(temp
.begin(), temp
.end(), '\n', '\0');
26 response
= new HttpResponseHeaders(temp
);
28 request
.extra_headers
.Clear();
29 request
.extra_headers
.AddHeadersFromString(request_headers
);
35 TEST(HttpVaryDataTest
, IsInvalid
) {
36 // All of these responses should result in an invalid vary data object.
37 const char* const kTestResponses
[] = {
38 "HTTP/1.1 200 OK\n\n",
39 "HTTP/1.1 200 OK\nVary: *\n\n",
40 "HTTP/1.1 200 OK\nVary: cookie, *, bar\n\n",
41 "HTTP/1.1 200 OK\nVary: cookie\nFoo: 1\nVary: *\n\n",
44 for (size_t i
= 0; i
< arraysize(kTestResponses
); ++i
) {
46 t
.Init(std::string(), kTestResponses
[i
]);
49 EXPECT_FALSE(v
.is_valid());
50 EXPECT_FALSE(v
.Init(t
.request
, *t
.response
.get()));
51 EXPECT_FALSE(v
.is_valid());
55 TEST(HttpVaryDataTest
, MultipleInit
) {
58 // Init to something valid.
60 t1
.Init("Foo: 1\r\nbar: 23", "HTTP/1.1 200 OK\nVary: foo, bar\n\n");
61 EXPECT_TRUE(v
.Init(t1
.request
, *t1
.response
.get()));
62 EXPECT_TRUE(v
.is_valid());
64 // Now overwrite by initializing to something invalid.
66 t2
.Init("Foo: 1\r\nbar: 23", "HTTP/1.1 200 OK\nVary: *\n\n");
67 EXPECT_FALSE(v
.Init(t2
.request
, *t2
.response
.get()));
68 EXPECT_FALSE(v
.is_valid());
71 TEST(HttpVaryDataTest
, DoesVary
) {
73 a
.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n");
76 b
.Init("Foo: 2", "HTTP/1.1 200 OK\nVary: foo\n\n");
79 EXPECT_TRUE(v
.Init(a
.request
, *a
.response
.get()));
81 EXPECT_FALSE(v
.MatchesRequest(b
.request
, *b
.response
.get()));
84 TEST(HttpVaryDataTest
, DoesVary2
) {
86 a
.Init("Foo: 1\r\nbar: 23", "HTTP/1.1 200 OK\nVary: foo, bar\n\n");
89 b
.Init("Foo: 12\r\nbar: 3", "HTTP/1.1 200 OK\nVary: foo, bar\n\n");
92 EXPECT_TRUE(v
.Init(a
.request
, *a
.response
.get()));
94 EXPECT_FALSE(v
.MatchesRequest(b
.request
, *b
.response
.get()));
97 TEST(HttpVaryDataTest
, DoesntVary
) {
99 a
.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n");
102 b
.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n");
105 EXPECT_TRUE(v
.Init(a
.request
, *a
.response
.get()));
107 EXPECT_TRUE(v
.MatchesRequest(b
.request
, *b
.response
.get()));
110 TEST(HttpVaryDataTest
, DoesntVary2
) {
112 a
.Init("Foo: 1\r\nbAr: 2", "HTTP/1.1 200 OK\nVary: foo, bar\n\n");
115 b
.Init("Foo: 1\r\nbaR: 2", "HTTP/1.1 200 OK\nVary: foo\nVary: bar\n\n");
118 EXPECT_TRUE(v
.Init(a
.request
, *a
.response
.get()));
120 EXPECT_TRUE(v
.MatchesRequest(b
.request
, *b
.response
.get()));
123 TEST(HttpVaryDataTest
, ImplicitCookieForRedirect
) {
125 a
.Init("Cookie: 1", "HTTP/1.1 301 Moved\nLocation: x\n\n");
128 b
.Init("Cookie: 2", "HTTP/1.1 301 Moved\nLocation: x\n\n");
131 EXPECT_TRUE(v
.Init(a
.request
, *a
.response
.get()));
133 EXPECT_FALSE(v
.MatchesRequest(b
.request
, *b
.response
.get()));
136 TEST(HttpVaryDataTest
, ImplicitCookieForRedirect2
) {
137 // This should be no different than the test above
140 a
.Init("Cookie: 1", "HTTP/1.1 301 Moved\nLocation: x\nVary: coOkie\n\n");
143 b
.Init("Cookie: 2", "HTTP/1.1 301 Moved\nLocation: x\nVary: cooKie\n\n");
146 EXPECT_TRUE(v
.Init(a
.request
, *a
.response
.get()));
148 EXPECT_FALSE(v
.MatchesRequest(b
.request
, *b
.response
.get()));