Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / http / http_vary_data_unittest.cc
blobc9bff658451dc52feb08224441f47c21914377de
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.
5 #include <algorithm>
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"
12 namespace net {
14 namespace {
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);
33 } // namespace
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) {
45 TestTransaction t;
46 t.Init(std::string(), kTestResponses[i]);
48 HttpVaryData v;
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) {
56 HttpVaryData v;
58 // Init to something valid.
59 TestTransaction t1;
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.
65 TestTransaction t2;
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) {
72 TestTransaction a;
73 a.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n");
75 TestTransaction b;
76 b.Init("Foo: 2", "HTTP/1.1 200 OK\nVary: foo\n\n");
78 HttpVaryData v;
79 EXPECT_TRUE(v.Init(a.request, *a.response.get()));
81 EXPECT_FALSE(v.MatchesRequest(b.request, *b.response.get()));
84 TEST(HttpVaryDataTest, DoesVary2) {
85 TestTransaction a;
86 a.Init("Foo: 1\r\nbar: 23", "HTTP/1.1 200 OK\nVary: foo, bar\n\n");
88 TestTransaction b;
89 b.Init("Foo: 12\r\nbar: 3", "HTTP/1.1 200 OK\nVary: foo, bar\n\n");
91 HttpVaryData v;
92 EXPECT_TRUE(v.Init(a.request, *a.response.get()));
94 EXPECT_FALSE(v.MatchesRequest(b.request, *b.response.get()));
97 TEST(HttpVaryDataTest, DoesntVary) {
98 TestTransaction a;
99 a.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n");
101 TestTransaction b;
102 b.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n");
104 HttpVaryData v;
105 EXPECT_TRUE(v.Init(a.request, *a.response.get()));
107 EXPECT_TRUE(v.MatchesRequest(b.request, *b.response.get()));
110 TEST(HttpVaryDataTest, DoesntVary2) {
111 TestTransaction a;
112 a.Init("Foo: 1\r\nbAr: 2", "HTTP/1.1 200 OK\nVary: foo, bar\n\n");
114 TestTransaction b;
115 b.Init("Foo: 1\r\nbaR: 2", "HTTP/1.1 200 OK\nVary: foo\nVary: bar\n\n");
117 HttpVaryData v;
118 EXPECT_TRUE(v.Init(a.request, *a.response.get()));
120 EXPECT_TRUE(v.MatchesRequest(b.request, *b.response.get()));
123 TEST(HttpVaryDataTest, ImplicitCookieForRedirect) {
124 TestTransaction a;
125 a.Init("Cookie: 1", "HTTP/1.1 301 Moved\nLocation: x\n\n");
127 TestTransaction b;
128 b.Init("Cookie: 2", "HTTP/1.1 301 Moved\nLocation: x\n\n");
130 HttpVaryData v;
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
139 TestTransaction a;
140 a.Init("Cookie: 1", "HTTP/1.1 301 Moved\nLocation: x\nVary: coOkie\n\n");
142 TestTransaction b;
143 b.Init("Cookie: 2", "HTTP/1.1 301 Moved\nLocation: x\nVary: cooKie\n\n");
145 HttpVaryData v;
146 EXPECT_TRUE(v.Init(a.request, *a.response.get()));
148 EXPECT_FALSE(v.MatchesRequest(b.request, *b.response.get()));
151 } // namespace net