blob: f1d881a9203c7d44fa1f8b993870ab3d012c42d5 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.
initial.commit586acc5fe2008-07-26 22:42:524
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
ttuttle859dc7a2015-04-23 19:42:2912namespace net {
13
initial.commit586acc5fe2008-07-26 22:42:5214namespace {
15
16typedef testing::Test HttpVaryDataTest;
17
18struct TestTransaction {
ttuttle859dc7a2015-04-23 19:42:2919 HttpRequestInfo request;
20 scoped_refptr<HttpResponseHeaders> response;
initial.commit586acc5fe2008-07-26 22:42:5221
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');
ttuttle859dc7a2015-04-23 19:42:2926 response = new HttpResponseHeaders(temp);
initial.commit586acc5fe2008-07-26 22:42:5227
[email protected]8c76ae22010-04-20 22:15:4328 request.extra_headers.Clear();
29 request.extra_headers.AddHeadersFromString(request_headers);
initial.commit586acc5fe2008-07-26 22:42:5230 }
31};
32
33} // namespace
34
[email protected]89ceba9a2009-03-21 03:46:0635TEST(HttpVaryDataTest, IsInvalid) {
initial.commit586acc5fe2008-07-26 22:42:5236 // All of these responses should result in an invalid vary data object.
thestig9d3bb0c2015-01-24 00:49:5137 const char* const kTestResponses[] = {
initial.commit586acc5fe2008-07-26 22:42:5238 "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",
42 };
43
44 for (size_t i = 0; i < arraysize(kTestResponses); ++i) {
45 TestTransaction t;
[email protected]007b3f82013-04-09 08:46:4546 t.Init(std::string(), kTestResponses[i]);
initial.commit586acc5fe2008-07-26 22:42:5247
ttuttle859dc7a2015-04-23 19:42:2948 HttpVaryData v;
[email protected]89ceba9a2009-03-21 03:46:0649 EXPECT_FALSE(v.is_valid());
[email protected]90499482013-06-01 00:39:5050 EXPECT_FALSE(v.Init(t.request, *t.response.get()));
initial.commit586acc5fe2008-07-26 22:42:5251 EXPECT_FALSE(v.is_valid());
52 }
53}
54
[email protected]89ceba9a2009-03-21 03:46:0655TEST(HttpVaryDataTest, MultipleInit) {
ttuttle859dc7a2015-04-23 19:42:2956 HttpVaryData v;
[email protected]89ceba9a2009-03-21 03:46:0657
58 // Init to something valid.
59 TestTransaction t1;
[email protected]8c76ae22010-04-20 22:15:4360 t1.Init("Foo: 1\r\nbar: 23", "HTTP/1.1 200 OK\nVary: foo, bar\n\n");
[email protected]90499482013-06-01 00:39:5061 EXPECT_TRUE(v.Init(t1.request, *t1.response.get()));
[email protected]89ceba9a2009-03-21 03:46:0662 EXPECT_TRUE(v.is_valid());
63
64 // Now overwrite by initializing to something invalid.
65 TestTransaction t2;
[email protected]8c76ae22010-04-20 22:15:4366 t2.Init("Foo: 1\r\nbar: 23", "HTTP/1.1 200 OK\nVary: *\n\n");
[email protected]90499482013-06-01 00:39:5067 EXPECT_FALSE(v.Init(t2.request, *t2.response.get()));
[email protected]89ceba9a2009-03-21 03:46:0668 EXPECT_FALSE(v.is_valid());
69}
70
initial.commit586acc5fe2008-07-26 22:42:5271TEST(HttpVaryDataTest, DoesVary) {
72 TestTransaction a;
73 a.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n");
74
75 TestTransaction b;
76 b.Init("Foo: 2", "HTTP/1.1 200 OK\nVary: foo\n\n");
77
ttuttle859dc7a2015-04-23 19:42:2978 HttpVaryData v;
[email protected]90499482013-06-01 00:39:5079 EXPECT_TRUE(v.Init(a.request, *a.response.get()));
initial.commit586acc5fe2008-07-26 22:42:5280
[email protected]90499482013-06-01 00:39:5081 EXPECT_FALSE(v.MatchesRequest(b.request, *b.response.get()));
initial.commit586acc5fe2008-07-26 22:42:5282}
83
84TEST(HttpVaryDataTest, DoesVary2) {
85 TestTransaction a;
[email protected]8c76ae22010-04-20 22:15:4386 a.Init("Foo: 1\r\nbar: 23", "HTTP/1.1 200 OK\nVary: foo, bar\n\n");
initial.commit586acc5fe2008-07-26 22:42:5287
88 TestTransaction b;
[email protected]8c76ae22010-04-20 22:15:4389 b.Init("Foo: 12\r\nbar: 3", "HTTP/1.1 200 OK\nVary: foo, bar\n\n");
initial.commit586acc5fe2008-07-26 22:42:5290
ttuttle859dc7a2015-04-23 19:42:2991 HttpVaryData v;
[email protected]90499482013-06-01 00:39:5092 EXPECT_TRUE(v.Init(a.request, *a.response.get()));
initial.commit586acc5fe2008-07-26 22:42:5293
[email protected]90499482013-06-01 00:39:5094 EXPECT_FALSE(v.MatchesRequest(b.request, *b.response.get()));
initial.commit586acc5fe2008-07-26 22:42:5295}
96
97TEST(HttpVaryDataTest, DoesntVary) {
98 TestTransaction a;
99 a.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n");
100
101 TestTransaction b;
102 b.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n");
103
ttuttle859dc7a2015-04-23 19:42:29104 HttpVaryData v;
[email protected]90499482013-06-01 00:39:50105 EXPECT_TRUE(v.Init(a.request, *a.response.get()));
initial.commit586acc5fe2008-07-26 22:42:52106
[email protected]90499482013-06-01 00:39:50107 EXPECT_TRUE(v.MatchesRequest(b.request, *b.response.get()));
initial.commit586acc5fe2008-07-26 22:42:52108}
109
110TEST(HttpVaryDataTest, DoesntVary2) {
111 TestTransaction a;
[email protected]8c76ae22010-04-20 22:15:43112 a.Init("Foo: 1\r\nbAr: 2", "HTTP/1.1 200 OK\nVary: foo, bar\n\n");
initial.commit586acc5fe2008-07-26 22:42:52113
114 TestTransaction b;
[email protected]8c76ae22010-04-20 22:15:43115 b.Init("Foo: 1\r\nbaR: 2", "HTTP/1.1 200 OK\nVary: foo\nVary: bar\n\n");
initial.commit586acc5fe2008-07-26 22:42:52116
ttuttle859dc7a2015-04-23 19:42:29117 HttpVaryData v;
[email protected]90499482013-06-01 00:39:50118 EXPECT_TRUE(v.Init(a.request, *a.response.get()));
initial.commit586acc5fe2008-07-26 22:42:52119
[email protected]90499482013-06-01 00:39:50120 EXPECT_TRUE(v.MatchesRequest(b.request, *b.response.get()));
initial.commit586acc5fe2008-07-26 22:42:52121}
122
gabadiedd6d9b12016-05-11 16:52:36123TEST(HttpVaryDataTest, DoesntVaryByCookieForRedirect) {
initial.commit586acc5fe2008-07-26 22:42:52124 TestTransaction a;
125 a.Init("Cookie: 1", "HTTP/1.1 301 Moved\nLocation: x\n\n");
126
ttuttle859dc7a2015-04-23 19:42:29127 HttpVaryData v;
gabadiedd6d9b12016-05-11 16:52:36128 EXPECT_FALSE(v.Init(a.request, *a.response.get()));
initial.commit586acc5fe2008-07-26 22:42:52129}
ttuttle859dc7a2015-04-23 19:42:29130
131} // namespace net