| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <algorithm> | 5 #include <algorithm> |
| 6 | 6 |
| 7 #include "net/http/http_request_info.h" | 7 #include "net/http/http_request_info.h" |
| 8 #include "net/http/http_response_headers.h" | 8 #include "net/http/http_response_headers.h" |
| 9 #include "net/http/http_vary_data.h" | 9 #include "net/http/http_vary_data.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 typedef testing::Test HttpVaryDataTest; | 14 typedef testing::Test HttpVaryDataTest; |
| 15 | 15 |
| 16 struct TestTransaction { | 16 struct TestTransaction { |
| 17 net::HttpRequestInfo request; | 17 net::HttpRequestInfo request; |
| 18 scoped_refptr<net::HttpResponseHeaders> response; | 18 scoped_refptr<net::HttpResponseHeaders> response; |
| 19 | 19 |
| 20 void Init(const std::string& request_headers, | 20 void Init(const std::string& request_headers, |
| 21 const std::string& response_headers) { | 21 const std::string& response_headers) { |
| 22 std::string temp(response_headers); | 22 std::string temp(response_headers); |
| 23 std::replace(temp.begin(), temp.end(), '\n', '\0'); | 23 std::replace(temp.begin(), temp.end(), '\n', '\0'); |
| 24 response = new net::HttpResponseHeaders(temp); | 24 response = new net::HttpResponseHeaders(temp); |
| 25 | 25 |
| 26 request.extra_headers = request_headers; | 26 request.extra_headers.Clear(); |
| 27 request.extra_headers.AddHeadersFromString(request_headers); |
| 27 } | 28 } |
| 28 }; | 29 }; |
| 29 | 30 |
| 30 } // namespace | 31 } // namespace |
| 31 | 32 |
| 32 TEST(HttpVaryDataTest, IsInvalid) { | 33 TEST(HttpVaryDataTest, IsInvalid) { |
| 33 // All of these responses should result in an invalid vary data object. | 34 // All of these responses should result in an invalid vary data object. |
| 34 const char* kTestResponses[] = { | 35 const char* kTestResponses[] = { |
| 35 "HTTP/1.1 200 OK\n\n", | 36 "HTTP/1.1 200 OK\n\n", |
| 36 "HTTP/1.1 200 OK\nVary: *\n\n", | 37 "HTTP/1.1 200 OK\nVary: *\n\n", |
| (...skipping 10 matching lines...) Expand all Loading... |
| 47 EXPECT_FALSE(v.Init(t.request, *t.response)); | 48 EXPECT_FALSE(v.Init(t.request, *t.response)); |
| 48 EXPECT_FALSE(v.is_valid()); | 49 EXPECT_FALSE(v.is_valid()); |
| 49 } | 50 } |
| 50 } | 51 } |
| 51 | 52 |
| 52 TEST(HttpVaryDataTest, MultipleInit) { | 53 TEST(HttpVaryDataTest, MultipleInit) { |
| 53 net::HttpVaryData v; | 54 net::HttpVaryData v; |
| 54 | 55 |
| 55 // Init to something valid. | 56 // Init to something valid. |
| 56 TestTransaction t1; | 57 TestTransaction t1; |
| 57 t1.Init("Foo: 1\nbar: 23", "HTTP/1.1 200 OK\nVary: foo, bar\n\n"); | 58 t1.Init("Foo: 1\r\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.Init(t1.request, *t1.response)); |
| 59 EXPECT_TRUE(v.is_valid()); | 60 EXPECT_TRUE(v.is_valid()); |
| 60 | 61 |
| 61 // Now overwrite by initializing to something invalid. | 62 // Now overwrite by initializing to something invalid. |
| 62 TestTransaction t2; | 63 TestTransaction t2; |
| 63 t2.Init("Foo: 1\nbar: 23", "HTTP/1.1 200 OK\nVary: *\n\n"); | 64 t2.Init("Foo: 1\r\nbar: 23", "HTTP/1.1 200 OK\nVary: *\n\n"); |
| 64 EXPECT_FALSE(v.Init(t2.request, *t2.response)); | 65 EXPECT_FALSE(v.Init(t2.request, *t2.response)); |
| 65 EXPECT_FALSE(v.is_valid()); | 66 EXPECT_FALSE(v.is_valid()); |
| 66 } | 67 } |
| 67 | 68 |
| 68 TEST(HttpVaryDataTest, DoesVary) { | 69 TEST(HttpVaryDataTest, DoesVary) { |
| 69 TestTransaction a; | 70 TestTransaction a; |
| 70 a.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n"); | 71 a.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n"); |
| 71 | 72 |
| 72 TestTransaction b; | 73 TestTransaction b; |
| 73 b.Init("Foo: 2", "HTTP/1.1 200 OK\nVary: foo\n\n"); | 74 b.Init("Foo: 2", "HTTP/1.1 200 OK\nVary: foo\n\n"); |
| 74 | 75 |
| 75 net::HttpVaryData v; | 76 net::HttpVaryData v; |
| 76 EXPECT_TRUE(v.Init(a.request, *a.response)); | 77 EXPECT_TRUE(v.Init(a.request, *a.response)); |
| 77 | 78 |
| 78 EXPECT_FALSE(v.MatchesRequest(b.request, *b.response)); | 79 EXPECT_FALSE(v.MatchesRequest(b.request, *b.response)); |
| 79 } | 80 } |
| 80 | 81 |
| 81 TEST(HttpVaryDataTest, DoesVary2) { | 82 TEST(HttpVaryDataTest, DoesVary2) { |
| 82 TestTransaction a; | 83 TestTransaction a; |
| 83 a.Init("Foo: 1\nbar: 23", "HTTP/1.1 200 OK\nVary: foo, bar\n\n"); | 84 a.Init("Foo: 1\r\nbar: 23", "HTTP/1.1 200 OK\nVary: foo, bar\n\n"); |
| 84 | 85 |
| 85 TestTransaction b; | 86 TestTransaction b; |
| 86 b.Init("Foo: 12\nbar: 3", "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"); |
| 87 | 88 |
| 88 net::HttpVaryData v; | 89 net::HttpVaryData v; |
| 89 EXPECT_TRUE(v.Init(a.request, *a.response)); | 90 EXPECT_TRUE(v.Init(a.request, *a.response)); |
| 90 | 91 |
| 91 EXPECT_FALSE(v.MatchesRequest(b.request, *b.response)); | 92 EXPECT_FALSE(v.MatchesRequest(b.request, *b.response)); |
| 92 } | 93 } |
| 93 | 94 |
| 94 TEST(HttpVaryDataTest, DoesntVary) { | 95 TEST(HttpVaryDataTest, DoesntVary) { |
| 95 TestTransaction a; | 96 TestTransaction a; |
| 96 a.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n"); | 97 a.Init("Foo: 1", "HTTP/1.1 200 OK\nVary: foo\n\n"); |
| 97 | 98 |
| 98 TestTransaction b; | 99 TestTransaction b; |
| 99 b.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"); |
| 100 | 101 |
| 101 net::HttpVaryData v; | 102 net::HttpVaryData v; |
| 102 EXPECT_TRUE(v.Init(a.request, *a.response)); | 103 EXPECT_TRUE(v.Init(a.request, *a.response)); |
| 103 | 104 |
| 104 EXPECT_TRUE(v.MatchesRequest(b.request, *b.response)); | 105 EXPECT_TRUE(v.MatchesRequest(b.request, *b.response)); |
| 105 } | 106 } |
| 106 | 107 |
| 107 TEST(HttpVaryDataTest, DoesntVary2) { | 108 TEST(HttpVaryDataTest, DoesntVary2) { |
| 108 TestTransaction a; | 109 TestTransaction a; |
| 109 a.Init("Foo: 1\nbAr: 2", "HTTP/1.1 200 OK\nVary: foo, bar\n\n"); | 110 a.Init("Foo: 1\r\nbAr: 2", "HTTP/1.1 200 OK\nVary: foo, bar\n\n"); |
| 110 | 111 |
| 111 TestTransaction b; | 112 TestTransaction b; |
| 112 b.Init("Foo: 1\nbaR: 2", "HTTP/1.1 200 OK\nVary: foo\nVary: bar\n\n"); | 113 b.Init("Foo: 1\r\nbaR: 2", "HTTP/1.1 200 OK\nVary: foo\nVary: bar\n\n"); |
| 113 | 114 |
| 114 net::HttpVaryData v; | 115 net::HttpVaryData v; |
| 115 EXPECT_TRUE(v.Init(a.request, *a.response)); | 116 EXPECT_TRUE(v.Init(a.request, *a.response)); |
| 116 | 117 |
| 117 EXPECT_TRUE(v.MatchesRequest(b.request, *b.response)); | 118 EXPECT_TRUE(v.MatchesRequest(b.request, *b.response)); |
| 118 } | 119 } |
| 119 | 120 |
| 120 TEST(HttpVaryDataTest, ImplicitCookieForRedirect) { | 121 TEST(HttpVaryDataTest, ImplicitCookieForRedirect) { |
| 121 TestTransaction a; | 122 TestTransaction a; |
| 122 a.Init("Cookie: 1", "HTTP/1.1 301 Moved\nLocation: x\n\n"); | 123 a.Init("Cookie: 1", "HTTP/1.1 301 Moved\nLocation: x\n\n"); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 137 a.Init("Cookie: 1", "HTTP/1.1 301 Moved\nLocation: x\nVary: coOkie\n\n"); | 138 a.Init("Cookie: 1", "HTTP/1.1 301 Moved\nLocation: x\nVary: coOkie\n\n"); |
| 138 | 139 |
| 139 TestTransaction b; | 140 TestTransaction b; |
| 140 b.Init("Cookie: 2", "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"); |
| 141 | 142 |
| 142 net::HttpVaryData v; | 143 net::HttpVaryData v; |
| 143 EXPECT_TRUE(v.Init(a.request, *a.response)); | 144 EXPECT_TRUE(v.Init(a.request, *a.response)); |
| 144 | 145 |
| 145 EXPECT_FALSE(v.MatchesRequest(b.request, *b.response)); | 146 EXPECT_FALSE(v.MatchesRequest(b.request, *b.response)); |
| 146 } | 147 } |
| OLD | NEW |