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"
14 typedef testing::Test HttpVaryDataTest
;
16 struct TestTransaction
{
17 net::HttpRequestInfo request
;
18 scoped_refptr
<net::HttpResponseHeaders
> response
;
20 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
);
26 request
.extra_headers
.Clear();
27 request
.extra_headers
.AddHeadersFromString(request_headers
);
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",
42 for (size_t i
= 0; i
< arraysize(kTestResponses
); ++i
) {
44 t
.Init(std::string(), kTestResponses
[i
]);
47 EXPECT_FALSE(v
.is_valid());
48 EXPECT_FALSE(v
.Init(t
.request
, *t
.response
.get()));
49 EXPECT_FALSE(v
.is_valid());
53 TEST(HttpVaryDataTest
, MultipleInit
) {
56 // Init to something valid.
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
.get()));
60 EXPECT_TRUE(v
.is_valid());
62 // Now overwrite by initializing to something invalid.
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
.get()));
66 EXPECT_FALSE(v
.is_valid());
69 TEST(HttpVaryDataTest
, DoesVary
) {
71 a
.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n");
74 b
.Init("Foo: 2", "HTTP/1.1 200 OK\nVary: foo\n\n");
77 EXPECT_TRUE(v
.Init(a
.request
, *a
.response
.get()));
79 EXPECT_FALSE(v
.MatchesRequest(b
.request
, *b
.response
.get()));
82 TEST(HttpVaryDataTest
, DoesVary2
) {
84 a
.Init("Foo: 1\r\nbar: 23", "HTTP/1.1 200 OK\nVary: foo, bar\n\n");
87 b
.Init("Foo: 12\r\nbar: 3", "HTTP/1.1 200 OK\nVary: foo, bar\n\n");
90 EXPECT_TRUE(v
.Init(a
.request
, *a
.response
.get()));
92 EXPECT_FALSE(v
.MatchesRequest(b
.request
, *b
.response
.get()));
95 TEST(HttpVaryDataTest
, DoesntVary
) {
97 a
.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n");
100 b
.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n");
103 EXPECT_TRUE(v
.Init(a
.request
, *a
.response
.get()));
105 EXPECT_TRUE(v
.MatchesRequest(b
.request
, *b
.response
.get()));
108 TEST(HttpVaryDataTest
, DoesntVary2
) {
110 a
.Init("Foo: 1\r\nbAr: 2", "HTTP/1.1 200 OK\nVary: foo, bar\n\n");
113 b
.Init("Foo: 1\r\nbaR: 2", "HTTP/1.1 200 OK\nVary: foo\nVary: bar\n\n");
116 EXPECT_TRUE(v
.Init(a
.request
, *a
.response
.get()));
118 EXPECT_TRUE(v
.MatchesRequest(b
.request
, *b
.response
.get()));
121 TEST(HttpVaryDataTest
, ImplicitCookieForRedirect
) {
123 a
.Init("Cookie: 1", "HTTP/1.1 301 Moved\nLocation: x\n\n");
126 b
.Init("Cookie: 2", "HTTP/1.1 301 Moved\nLocation: x\n\n");
129 EXPECT_TRUE(v
.Init(a
.request
, *a
.response
.get()));
131 EXPECT_FALSE(v
.MatchesRequest(b
.request
, *b
.response
.get()));
134 TEST(HttpVaryDataTest
, ImplicitCookieForRedirect2
) {
135 // This should be no different than the test above
138 a
.Init("Cookie: 1", "HTTP/1.1 301 Moved\nLocation: x\nVary: coOkie\n\n");
141 b
.Init("Cookie: 2", "HTTP/1.1 301 Moved\nLocation: x\nVary: cooKie\n\n");
144 EXPECT_TRUE(v
.Init(a
.request
, *a
.response
.get()));
146 EXPECT_FALSE(v
.MatchesRequest(b
.request
, *b
.response
.get()));