Index: net/url_request/url_request_unittest.cc |
diff --git a/net/url_request/url_request_unittest.cc b/net/url_request/url_request_unittest.cc |
index cfe3b7184b4cf218821a84dab7d941d10c0508c3..ba4d52a56994039b0015b7db4a3fc76b08eeae61 100644 |
--- a/net/url_request/url_request_unittest.cc |
+++ b/net/url_request/url_request_unittest.cc |
@@ -2543,6 +2543,126 @@ TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
} |
} |
+TEST_F(URLRequestTest, FirstPartyOnlyCookiesEnabled) { |
+ LocalHttpTestServer test_server; |
+ ASSERT_TRUE(test_server.Start()); |
+ |
+ // Set up a 'First-Party-Only' cookie (on '127.0.0.1', as that's where |
+ // LocalHttpTestServer points). |
+ { |
+ TestNetworkDelegate network_delegate; |
+ network_delegate.set_first_party_only_cookies_enabled(true); |
+ default_context_.set_network_delegate(&network_delegate); |
+ |
+ TestDelegate d; |
+ scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
+ test_server.GetURL( |
+ "set-cookie?FirstPartyCookieToSet=1;First-Party-Only"), |
+ DEFAULT_PRIORITY, &d, NULL)); |
+ req->Start(); |
+ base::RunLoop().Run(); |
+ EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
+ EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
+ EXPECT_EQ(1, network_delegate.set_cookie_count()); |
+ } |
+ |
+ // Verify that the cookie is sent for first-party requests. |
+ { |
+ TestNetworkDelegate network_delegate; |
+ network_delegate.set_first_party_only_cookies_enabled(true); |
+ default_context_.set_network_delegate(&network_delegate); |
+ TestDelegate d; |
+ scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
+ test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
+ req->set_first_party_for_cookies(test_server.GetURL("")); |
+ req->Start(); |
+ base::RunLoop().Run(); |
+ |
+ EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") != |
+ std::string::npos); |
+ EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
+ EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
+ } |
+ |
+ // Verify that the cookie is not-sent for non-first-party requests. |
+ { |
+ TestNetworkDelegate network_delegate; |
+ network_delegate.set_first_party_only_cookies_enabled(true); |
+ default_context_.set_network_delegate(&network_delegate); |
+ TestDelegate d; |
+ scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
+ test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
+ req->set_first_party_for_cookies(GURL("http://third-party.test/")); |
+ req->Start(); |
+ base::RunLoop().Run(); |
+ |
+ EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") == |
+ std::string::npos); |
+ EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
+ EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
+ } |
+} |
+ |
+TEST_F(URLRequestTest, FirstPartyOnlyCookiesDisabled) { |
+ LocalHttpTestServer test_server; |
+ ASSERT_TRUE(test_server.Start()); |
+ |
+ // Set up a 'First-Party-Only' cookie (on '127.0.0.1', as that's where |
+ // LocalHttpTestServer points). |
+ { |
+ TestNetworkDelegate network_delegate; |
+ network_delegate.set_first_party_only_cookies_enabled(false); |
+ default_context_.set_network_delegate(&network_delegate); |
+ |
+ TestDelegate d; |
+ scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
+ test_server.GetURL( |
+ "set-cookie?FirstPartyCookieToSet=1;First-Party-Only"), |
+ DEFAULT_PRIORITY, &d, NULL)); |
+ req->Start(); |
+ base::RunLoop().Run(); |
+ EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
+ EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
+ EXPECT_EQ(1, network_delegate.set_cookie_count()); |
+ } |
+ |
+ // Verify that the cookie is sent for first-party requests. |
+ { |
+ TestNetworkDelegate network_delegate; |
+ network_delegate.set_first_party_only_cookies_enabled(false); |
+ default_context_.set_network_delegate(&network_delegate); |
+ TestDelegate d; |
+ scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
+ test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
+ req->set_first_party_for_cookies(test_server.GetURL("")); |
+ req->Start(); |
+ base::RunLoop().Run(); |
+ |
+ EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") != |
+ std::string::npos); |
+ EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
+ EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
+ } |
+ |
+ // Verify that the cookie is also sent for non-first-party requests. |
+ { |
+ TestNetworkDelegate network_delegate; |
+ network_delegate.set_first_party_only_cookies_enabled(false); |
+ default_context_.set_network_delegate(&network_delegate); |
+ TestDelegate d; |
+ scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
+ test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
+ req->set_first_party_for_cookies(GURL("http://third-party.test/")); |
+ req->Start(); |
+ base::RunLoop().Run(); |
+ |
+ EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") != |
+ std::string::npos); |
+ EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
+ EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
+ } |
+} |
+ |
// FixedDateNetworkDelegate swaps out the server's HTTP Date response header |
// value for the |fixed_date| argument given to the constructor. |
class FixedDateNetworkDelegate : public TestNetworkDelegate { |