Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(143)

Side by Side Diff: chrome/browser/extensions/api/web_request/web_request_api_unittest.cc

Issue 154473002: Support redirectUrl at onHeadersReceived in WebRequest / DWR API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix WebRequestRulesRegistrySimpleTest.StageChecker test Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <map> 5 #include <map>
6 #include <queue> 6 #include <queue>
7 7
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
(...skipping 1256 matching lines...) Expand 10 before | Expand all | Expand 10 after
1267 scoped_refptr<net::HttpResponseHeaders> base_headers( 1267 scoped_refptr<net::HttpResponseHeaders> base_headers(
1268 new net::HttpResponseHeaders( 1268 new net::HttpResponseHeaders(
1269 net::HttpUtil::AssembleRawHeaders( 1269 net::HttpUtil::AssembleRawHeaders(
1270 base_headers_string, sizeof(base_headers_string)))); 1270 base_headers_string, sizeof(base_headers_string))));
1271 1271
1272 ResponseHeaders new_headers; 1272 ResponseHeaders new_headers;
1273 new_headers.push_back(ResponseHeader("kEy1", "Value1")); // Unchanged 1273 new_headers.push_back(ResponseHeader("kEy1", "Value1")); // Unchanged
1274 new_headers.push_back(ResponseHeader("Key2", "Value1")); // Modified 1274 new_headers.push_back(ResponseHeader("Key2", "Value1")); // Modified
1275 // Key3 is deleted 1275 // Key3 is deleted
1276 new_headers.push_back(ResponseHeader("Key4", "Value4")); // Added 1276 new_headers.push_back(ResponseHeader("Key4", "Value4")); // Added
1277 GURL effective_new_url;
1277 1278
1278 scoped_ptr<EventResponseDelta> delta(CalculateOnHeadersReceivedDelta( 1279 scoped_ptr<EventResponseDelta> delta(
1279 "extid", base::Time::Now(), cancel, base_headers.get(), &new_headers)); 1280 CalculateOnHeadersReceivedDelta("extid",
1281 base::Time::Now(),
1282 cancel,
1283 effective_new_url,
1284 base_headers.get(),
1285 &new_headers));
1280 ASSERT_TRUE(delta.get()); 1286 ASSERT_TRUE(delta.get());
1281 EXPECT_TRUE(delta->cancel); 1287 EXPECT_TRUE(delta->cancel);
1282 EXPECT_EQ(2u, delta->added_response_headers.size()); 1288 EXPECT_EQ(2u, delta->added_response_headers.size());
1283 EXPECT_TRUE(Contains(delta->added_response_headers, 1289 EXPECT_TRUE(Contains(delta->added_response_headers,
1284 ResponseHeader("Key2", "Value1"))); 1290 ResponseHeader("Key2", "Value1")));
1285 EXPECT_TRUE(Contains(delta->added_response_headers, 1291 EXPECT_TRUE(Contains(delta->added_response_headers,
1286 ResponseHeader("Key4", "Value4"))); 1292 ResponseHeader("Key4", "Value4")));
1287 EXPECT_EQ(2u, delta->deleted_response_headers.size()); 1293 EXPECT_EQ(2u, delta->deleted_response_headers.size());
1288 EXPECT_TRUE(Contains(delta->deleted_response_headers, 1294 EXPECT_TRUE(Contains(delta->deleted_response_headers,
1289 ResponseHeader("Key2", "Value2, Bar"))); 1295 ResponseHeader("Key2", "Value2, Bar")));
(...skipping 662 matching lines...) Expand 10 before | Expand all | Expand 10 after
1952 scoped_refptr<net::HttpResponseHeaders> base_headers( 1958 scoped_refptr<net::HttpResponseHeaders> base_headers(
1953 new net::HttpResponseHeaders( 1959 new net::HttpResponseHeaders(
1954 net::HttpUtil::AssembleRawHeaders( 1960 net::HttpUtil::AssembleRawHeaders(
1955 base_headers_string, sizeof(base_headers_string)))); 1961 base_headers_string, sizeof(base_headers_string))));
1956 1962
1957 // Check that we can handle if not touching the response headers. 1963 // Check that we can handle if not touching the response headers.
1958 linked_ptr<EventResponseDelta> d0( 1964 linked_ptr<EventResponseDelta> d0(
1959 new EventResponseDelta("extid0", base::Time::FromInternalValue(3000))); 1965 new EventResponseDelta("extid0", base::Time::FromInternalValue(3000)));
1960 deltas.push_back(d0); 1966 deltas.push_back(d0);
1961 scoped_refptr<net::HttpResponseHeaders> new_headers0; 1967 scoped_refptr<net::HttpResponseHeaders> new_headers0;
1962 MergeOnHeadersReceivedResponses(deltas, base_headers.get(), &new_headers0, 1968 GURL allowed_unsafe_redirect_url0;
1963 &warning_set, &net_log); 1969 MergeOnHeadersReceivedResponses(deltas,
1970 base_headers.get(),
1971 &new_headers0,
1972 &allowed_unsafe_redirect_url0,
1973 &warning_set,
1974 &net_log);
1964 EXPECT_FALSE(new_headers0.get()); 1975 EXPECT_FALSE(new_headers0.get());
1976 EXPECT_TRUE(allowed_unsafe_redirect_url0.is_empty());
1965 EXPECT_EQ(0u, warning_set.size()); 1977 EXPECT_EQ(0u, warning_set.size());
1966 EXPECT_EQ(0u, capturing_net_log.GetSize()); 1978 EXPECT_EQ(0u, capturing_net_log.GetSize());
1967 1979
1968 linked_ptr<EventResponseDelta> d1( 1980 linked_ptr<EventResponseDelta> d1(
1969 new EventResponseDelta("extid1", base::Time::FromInternalValue(2000))); 1981 new EventResponseDelta("extid1", base::Time::FromInternalValue(2000)));
1970 d1->deleted_response_headers.push_back(ResponseHeader("KEY1", "Value1")); 1982 d1->deleted_response_headers.push_back(ResponseHeader("KEY1", "Value1"));
1971 d1->deleted_response_headers.push_back(ResponseHeader("KEY2", "Value2, Foo")); 1983 d1->deleted_response_headers.push_back(ResponseHeader("KEY2", "Value2, Foo"));
1972 d1->added_response_headers.push_back(ResponseHeader("Key2", "Value3")); 1984 d1->added_response_headers.push_back(ResponseHeader("Key2", "Value3"));
1973 deltas.push_back(d1); 1985 deltas.push_back(d1);
1974 deltas.sort(&InDecreasingExtensionInstallationTimeOrder); 1986 deltas.sort(&InDecreasingExtensionInstallationTimeOrder);
1975 warning_set.clear(); 1987 warning_set.clear();
1976 capturing_net_log.Clear(); 1988 capturing_net_log.Clear();
1977 scoped_refptr<net::HttpResponseHeaders> new_headers1; 1989 scoped_refptr<net::HttpResponseHeaders> new_headers1;
1978 MergeOnHeadersReceivedResponses( 1990 GURL allowed_unsafe_redirect_url1;
1979 deltas, base_headers.get(), &new_headers1, &warning_set, &net_log); 1991 MergeOnHeadersReceivedResponses(deltas,
1992 base_headers.get(),
1993 &new_headers1,
1994 &allowed_unsafe_redirect_url1,
1995 &warning_set,
1996 &net_log);
1980 ASSERT_TRUE(new_headers1.get()); 1997 ASSERT_TRUE(new_headers1.get());
1998 EXPECT_TRUE(allowed_unsafe_redirect_url1.is_empty());
1981 std::multimap<std::string, std::string> expected1; 1999 std::multimap<std::string, std::string> expected1;
1982 expected1.insert(std::pair<std::string, std::string>("Key2", "Value3")); 2000 expected1.insert(std::pair<std::string, std::string>("Key2", "Value3"));
1983 void* iter = NULL; 2001 void* iter = NULL;
1984 std::string name; 2002 std::string name;
1985 std::string value; 2003 std::string value;
1986 std::multimap<std::string, std::string> actual1; 2004 std::multimap<std::string, std::string> actual1;
1987 while (new_headers1->EnumerateHeaderLines(&iter, &name, &value)) { 2005 while (new_headers1->EnumerateHeaderLines(&iter, &name, &value)) {
1988 actual1.insert(std::pair<std::string, std::string>(name, value)); 2006 actual1.insert(std::pair<std::string, std::string>(name, value));
1989 } 2007 }
1990 EXPECT_EQ(expected1, actual1); 2008 EXPECT_EQ(expected1, actual1);
1991 EXPECT_EQ(0u, warning_set.size()); 2009 EXPECT_EQ(0u, warning_set.size());
1992 EXPECT_EQ(1u, capturing_net_log.GetSize()); 2010 EXPECT_EQ(1u, capturing_net_log.GetSize());
1993 2011
1994 // Check that we replace response headers only once. 2012 // Check that we replace response headers only once.
1995 linked_ptr<EventResponseDelta> d2( 2013 linked_ptr<EventResponseDelta> d2(
1996 new EventResponseDelta("extid2", base::Time::FromInternalValue(1500))); 2014 new EventResponseDelta("extid2", base::Time::FromInternalValue(1500)));
1997 // Note that we use a different capitalization of KeY2. This should not 2015 // Note that we use a different capitalization of KeY2. This should not
1998 // matter. 2016 // matter.
1999 d2->deleted_response_headers.push_back(ResponseHeader("KeY2", "Value2, Foo")); 2017 d2->deleted_response_headers.push_back(ResponseHeader("KeY2", "Value2, Foo"));
2000 d2->added_response_headers.push_back(ResponseHeader("Key2", "Value4")); 2018 d2->added_response_headers.push_back(ResponseHeader("Key2", "Value4"));
2001 deltas.push_back(d2); 2019 deltas.push_back(d2);
2002 deltas.sort(&InDecreasingExtensionInstallationTimeOrder); 2020 deltas.sort(&InDecreasingExtensionInstallationTimeOrder);
2003 warning_set.clear(); 2021 warning_set.clear();
2004 capturing_net_log.Clear(); 2022 capturing_net_log.Clear();
2005 scoped_refptr<net::HttpResponseHeaders> new_headers2; 2023 scoped_refptr<net::HttpResponseHeaders> new_headers2;
2006 MergeOnHeadersReceivedResponses( 2024 GURL allowed_unsafe_redirect_url2;
2007 deltas, base_headers.get(), &new_headers2, &warning_set, &net_log); 2025 MergeOnHeadersReceivedResponses(deltas,
2026 base_headers.get(),
2027 &new_headers2,
2028 &allowed_unsafe_redirect_url2,
2029 &warning_set,
2030 &net_log);
2008 ASSERT_TRUE(new_headers2.get()); 2031 ASSERT_TRUE(new_headers2.get());
2032 EXPECT_TRUE(allowed_unsafe_redirect_url2.is_empty());
2009 iter = NULL; 2033 iter = NULL;
2010 std::multimap<std::string, std::string> actual2; 2034 std::multimap<std::string, std::string> actual2;
2011 while (new_headers2->EnumerateHeaderLines(&iter, &name, &value)) { 2035 while (new_headers2->EnumerateHeaderLines(&iter, &name, &value)) {
2012 actual2.insert(std::pair<std::string, std::string>(name, value)); 2036 actual2.insert(std::pair<std::string, std::string>(name, value));
2013 } 2037 }
2014 EXPECT_EQ(expected1, actual2); 2038 EXPECT_EQ(expected1, actual2);
2015 EXPECT_EQ(1u, warning_set.size()); 2039 EXPECT_EQ(1u, warning_set.size());
2016 EXPECT_TRUE(HasWarning(warning_set, "extid2")); 2040 EXPECT_TRUE(HasWarning(warning_set, "extid2"));
2017 EXPECT_EQ(2u, capturing_net_log.GetSize()); 2041 EXPECT_EQ(2u, capturing_net_log.GetSize());
2018 } 2042 }
(...skipping 17 matching lines...) Expand all
2036 scoped_refptr<net::HttpResponseHeaders> base_headers( 2060 scoped_refptr<net::HttpResponseHeaders> base_headers(
2037 new net::HttpResponseHeaders( 2061 new net::HttpResponseHeaders(
2038 net::HttpUtil::AssembleRawHeaders( 2062 net::HttpUtil::AssembleRawHeaders(
2039 base_headers_string, sizeof(base_headers_string)))); 2063 base_headers_string, sizeof(base_headers_string))));
2040 2064
2041 linked_ptr<EventResponseDelta> d1( 2065 linked_ptr<EventResponseDelta> d1(
2042 new EventResponseDelta("extid1", base::Time::FromInternalValue(2000))); 2066 new EventResponseDelta("extid1", base::Time::FromInternalValue(2000)));
2043 d1->deleted_response_headers.push_back(ResponseHeader("KEY1", "Value2")); 2067 d1->deleted_response_headers.push_back(ResponseHeader("KEY1", "Value2"));
2044 deltas.push_back(d1); 2068 deltas.push_back(d1);
2045 scoped_refptr<net::HttpResponseHeaders> new_headers1; 2069 scoped_refptr<net::HttpResponseHeaders> new_headers1;
2046 MergeOnHeadersReceivedResponses( 2070 GURL allowed_unsafe_redirect_url1;
2047 deltas, base_headers.get(), &new_headers1, &warning_set, &net_log); 2071 MergeOnHeadersReceivedResponses(deltas,
2072 base_headers.get(),
2073 &new_headers1,
2074 &allowed_unsafe_redirect_url1,
2075 &warning_set,
2076 &net_log);
2048 ASSERT_TRUE(new_headers1.get()); 2077 ASSERT_TRUE(new_headers1.get());
2078 EXPECT_TRUE(allowed_unsafe_redirect_url1.is_empty());
2049 std::multimap<std::string, std::string> expected1; 2079 std::multimap<std::string, std::string> expected1;
2050 expected1.insert(std::pair<std::string, std::string>("Key1", "Value1")); 2080 expected1.insert(std::pair<std::string, std::string>("Key1", "Value1"));
2051 expected1.insert(std::pair<std::string, std::string>("Key1", "Value3")); 2081 expected1.insert(std::pair<std::string, std::string>("Key1", "Value3"));
2052 expected1.insert(std::pair<std::string, std::string>("Key2", "Value4")); 2082 expected1.insert(std::pair<std::string, std::string>("Key2", "Value4"));
2053 void* iter = NULL; 2083 void* iter = NULL;
2054 std::string name; 2084 std::string name;
2055 std::string value; 2085 std::string value;
2056 std::multimap<std::string, std::string> actual1; 2086 std::multimap<std::string, std::string> actual1;
2057 while (new_headers1->EnumerateHeaderLines(&iter, &name, &value)) { 2087 while (new_headers1->EnumerateHeaderLines(&iter, &name, &value)) {
2058 actual1.insert(std::pair<std::string, std::string>(name, value)); 2088 actual1.insert(std::pair<std::string, std::string>(name, value));
2059 } 2089 }
2060 EXPECT_EQ(expected1, actual1); 2090 EXPECT_EQ(expected1, actual1);
2061 EXPECT_EQ(0u, warning_set.size()); 2091 EXPECT_EQ(0u, warning_set.size());
2062 EXPECT_EQ(1u, capturing_net_log.GetSize()); 2092 EXPECT_EQ(1u, capturing_net_log.GetSize());
2063 } 2093 }
2064 2094
2095 // Tests whether onHeadersReceived can initiate a redirect.
2096 // The URL merge logic is shared with onBeforeRequest, so we only need to test
2097 // whether the URLs are merged at all.
2098 TEST(ExtensionWebRequestHelpersTest,
2099 TestMergeOnHeadersReceivedResponsesRedirect) {
2100 EventResponseDeltas deltas;
2101 net::CapturingBoundNetLog capturing_net_log;
2102 net::BoundNetLog net_log = capturing_net_log.bound();
2103 ExtensionWarningSet warning_set;
2104
2105 char base_headers_string[] =
2106 "HTTP/1.0 200 OK\r\n"
2107 "\r\n";
2108 scoped_refptr<net::HttpResponseHeaders> base_headers(
2109 new net::HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders(
2110 base_headers_string, sizeof(base_headers_string))));
2111
2112 // No redirect
2113 linked_ptr<EventResponseDelta> d0(
2114 new EventResponseDelta("extid0", base::Time::FromInternalValue(0)));
2115 deltas.push_back(d0);
2116 scoped_refptr<net::HttpResponseHeaders> new_headers0;
2117 GURL allowed_unsafe_redirect_url0;
2118 MergeOnHeadersReceivedResponses(deltas,
2119 base_headers.get(),
2120 &new_headers0,
2121 &allowed_unsafe_redirect_url0,
2122 &warning_set,
2123 &net_log);
2124 EXPECT_FALSE(new_headers0.get());
2125 EXPECT_TRUE(allowed_unsafe_redirect_url0.is_empty());
2126 EXPECT_EQ(0u, warning_set.size());
2127 EXPECT_EQ(0u, capturing_net_log.GetSize());
2128
2129 // Single redirect.
2130 GURL new_url_1("http://foo.com");
2131 linked_ptr<EventResponseDelta> d1(
2132 new EventResponseDelta("extid1", base::Time::FromInternalValue(1000)));
2133 d1->new_url = GURL(new_url_1);
2134 deltas.push_back(d1);
2135 deltas.sort(&InDecreasingExtensionInstallationTimeOrder);
2136 capturing_net_log.Clear();
2137 scoped_refptr<net::HttpResponseHeaders> new_headers1;
2138 GURL allowed_unsafe_redirect_url1;
2139 MergeOnHeadersReceivedResponses(deltas,
2140 base_headers.get(),
2141 &new_headers1,
2142 &allowed_unsafe_redirect_url1,
2143 &warning_set,
2144 &net_log);
2145
2146 EXPECT_TRUE(new_headers1.get());
2147 EXPECT_TRUE(new_headers1->HasHeaderValue("Location", new_url_1.spec()));
2148 EXPECT_EQ(new_url_1, allowed_unsafe_redirect_url1);
2149 EXPECT_TRUE(warning_set.empty());
2150 EXPECT_EQ(1u, capturing_net_log.GetSize());
2151 }
2152
2065 TEST(ExtensionWebRequestHelpersTest, TestMergeOnAuthRequiredResponses) { 2153 TEST(ExtensionWebRequestHelpersTest, TestMergeOnAuthRequiredResponses) {
2066 net::CapturingBoundNetLog capturing_net_log; 2154 net::CapturingBoundNetLog capturing_net_log;
2067 net::BoundNetLog net_log = capturing_net_log.bound(); 2155 net::BoundNetLog net_log = capturing_net_log.bound();
2068 ExtensionWarningSet warning_set; 2156 ExtensionWarningSet warning_set;
2069 EventResponseDeltas deltas; 2157 EventResponseDeltas deltas;
2070 base::string16 username = base::ASCIIToUTF16("foo"); 2158 base::string16 username = base::ASCIIToUTF16("foo");
2071 base::string16 password = base::ASCIIToUTF16("bar"); 2159 base::string16 password = base::ASCIIToUTF16("bar");
2072 base::string16 password2 = base::ASCIIToUTF16("baz"); 2160 base::string16 password2 = base::ASCIIToUTF16("baz");
2073 2161
2074 // Check that we can handle if not returning credentials. 2162 // Check that we can handle if not returning credentials.
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
2135 EXPECT_TRUE(credentials_set); 2223 EXPECT_TRUE(credentials_set);
2136 EXPECT_FALSE(auth3.Empty()); 2224 EXPECT_FALSE(auth3.Empty());
2137 EXPECT_EQ(username, auth1.username()); 2225 EXPECT_EQ(username, auth1.username());
2138 EXPECT_EQ(password, auth1.password()); 2226 EXPECT_EQ(password, auth1.password());
2139 EXPECT_EQ(1u, warning_set.size()); 2227 EXPECT_EQ(1u, warning_set.size());
2140 EXPECT_TRUE(HasWarning(warning_set, "extid2")); 2228 EXPECT_TRUE(HasWarning(warning_set, "extid2"));
2141 EXPECT_EQ(3u, capturing_net_log.GetSize()); 2229 EXPECT_EQ(3u, capturing_net_log.GetSize());
2142 } 2230 }
2143 2231
2144 } // namespace extensions 2232 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/web_request/web_request_api_helpers.cc ('k') | chrome/browser/net/chrome_network_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698