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 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); 25 26 request.extra_headers = request_headers; 27 } 28 }; 29 30 } // namespace 31 32 TEST(HttpVaryDataTest, IsInvalid) { 33 // All of these responses should result in an invalid vary data object. 34 const char* kTestResponses[] = { 35 "HTTP/1.1 200 OK\n\n", 36 "HTTP/1.1 200 OK\nVary: *\n\n", 37 "HTTP/1.1 200 OK\nVary: cookie, *, bar\n\n", 38 "HTTP/1.1 200 OK\nVary: cookie\nFoo: 1\nVary: *\n\n", 39 }; 40 41 for (size_t i = 0; i < arraysize(kTestResponses); ++i) { 42 TestTransaction t; 43 t.Init("", kTestResponses[i]); 44 45 net::HttpVaryData v; 46 EXPECT_FALSE(v.is_valid()); 47 EXPECT_FALSE(v.Init(t.request, *t.response)); 48 EXPECT_FALSE(v.is_valid()); 49 } 50 } 51 52 TEST(HttpVaryDataTest, MultipleInit) { 53 net::HttpVaryData v; 54 55 // Init to something valid. 56 TestTransaction t1; 57 t1.Init("Foo: 1\nbar: 23", "HTTP/1.1 200 OK\nVary: foo, bar\n\n"); 58 EXPECT_TRUE(v.Init(t1.request, *t1.response)); 59 EXPECT_TRUE(v.is_valid()); 60 61 // Now overwrite by initializing to something invalid. 62 TestTransaction t2; 63 t2.Init("Foo: 1\nbar: 23", "HTTP/1.1 200 OK\nVary: *\n\n"); 64 EXPECT_FALSE(v.Init(t2.request, *t2.response)); 65 EXPECT_FALSE(v.is_valid()); 66 } 67 68 TEST(HttpVaryDataTest, DoesVary) { 69 TestTransaction a; 70 a.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n"); 71 72 TestTransaction b; 73 b.Init("Foo: 2", "HTTP/1.1 200 OK\nVary: foo\n\n"); 74 75 net::HttpVaryData v; 76 EXPECT_TRUE(v.Init(a.request, *a.response)); 77 78 EXPECT_FALSE(v.MatchesRequest(b.request, *b.response)); 79 } 80 81 TEST(HttpVaryDataTest, DoesVary2) { 82 TestTransaction a; 83 a.Init("Foo: 1\nbar: 23", "HTTP/1.1 200 OK\nVary: foo, bar\n\n"); 84 85 TestTransaction b; 86 b.Init("Foo: 12\nbar: 3", "HTTP/1.1 200 OK\nVary: foo, bar\n\n"); 87 88 net::HttpVaryData v; 89 EXPECT_TRUE(v.Init(a.request, *a.response)); 90 91 EXPECT_FALSE(v.MatchesRequest(b.request, *b.response)); 92 } 93 94 TEST(HttpVaryDataTest, DoesntVary) { 95 TestTransaction a; 96 a.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n"); 97 98 TestTransaction b; 99 b.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n"); 100 101 net::HttpVaryData v; 102 EXPECT_TRUE(v.Init(a.request, *a.response)); 103 104 EXPECT_TRUE(v.MatchesRequest(b.request, *b.response)); 105 } 106 107 TEST(HttpVaryDataTest, DoesntVary2) { 108 TestTransaction a; 109 a.Init("Foo: 1\nbAr: 2", "HTTP/1.1 200 OK\nVary: foo, bar\n\n"); 110 111 TestTransaction b; 112 b.Init("Foo: 1\nbaR: 2", "HTTP/1.1 200 OK\nVary: foo\nVary: bar\n\n"); 113 114 net::HttpVaryData v; 115 EXPECT_TRUE(v.Init(a.request, *a.response)); 116 117 EXPECT_TRUE(v.MatchesRequest(b.request, *b.response)); 118 } 119 120 TEST(HttpVaryDataTest, ImplicitCookieForRedirect) { 121 TestTransaction a; 122 a.Init("Cookie: 1", "HTTP/1.1 301 Moved\nLocation: x\n\n"); 123 124 TestTransaction b; 125 b.Init("Cookie: 2", "HTTP/1.1 301 Moved\nLocation: x\n\n"); 126 127 net::HttpVaryData v; 128 EXPECT_TRUE(v.Init(a.request, *a.response)); 129 130 EXPECT_FALSE(v.MatchesRequest(b.request, *b.response)); 131 } 132 133 TEST(HttpVaryDataTest, ImplicitCookieForRedirect2) { 134 // This should be no different than the test above 135 136 TestTransaction a; 137 a.Init("Cookie: 1", "HTTP/1.1 301 Moved\nLocation: x\nVary: coOkie\n\n"); 138 139 TestTransaction b; 140 b.Init("Cookie: 2", "HTTP/1.1 301 Moved\nLocation: x\nVary: cooKie\n\n"); 141 142 net::HttpVaryData v; 143 EXPECT_TRUE(v.Init(a.request, *a.response)); 144 145 EXPECT_FALSE(v.MatchesRequest(b.request, *b.response)); 146 } 147