| OLD | NEW |
| 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 "base/message_loop_proxy.h" | 5 #include "base/message_loop_proxy.h" |
| 6 #include "base/synchronization/waitable_event.h" | 6 #include "base/synchronization/waitable_event.h" |
| 7 #include "base/threading/thread.h" | 7 #include "base/threading/thread.h" |
| 8 #include "chrome/browser/sync/glue/http_bridge.h" | |
| 9 #include "content/public/test/test_browser_thread.h" | |
| 10 #include "net/test/test_server.h" | 8 #include "net/test/test_server.h" |
| 11 #include "net/url_request/test_url_fetcher_factory.h" | 9 #include "net/url_request/test_url_fetcher_factory.h" |
| 12 #include "net/url_request/url_fetcher_delegate.h" | 10 #include "net/url_request/url_fetcher_delegate.h" |
| 13 #include "net/url_request/url_request_test_util.h" | 11 #include "net/url_request/url_request_test_util.h" |
| 12 #include "sync/internal_api/public/http_bridge.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 14 |
| 16 using browser_sync::HttpBridge; | 15 using browser_sync::HttpBridge; |
| 17 using content::BrowserThread; | |
| 18 | 16 |
| 19 namespace { | 17 namespace { |
| 20 // TODO(timsteele): Should use PathService here. See Chromium Issue 3113. | 18 // TODO(timsteele): Should use PathService here. See Chromium Issue 3113. |
| 21 const FilePath::CharType kDocRoot[] = FILE_PATH_LITERAL("chrome/test/data"); | 19 const FilePath::CharType kDocRoot[] = FILE_PATH_LITERAL("chrome/test/data"); |
| 22 } | 20 } |
| 23 | 21 |
| 24 class SyncHttpBridgeTest : public testing::Test { | 22 class SyncHttpBridgeTest : public testing::Test { |
| 25 public: | 23 public: |
| 26 SyncHttpBridgeTest() | 24 SyncHttpBridgeTest() |
| 27 : test_server_(net::TestServer::TYPE_HTTP, | 25 : test_server_(net::TestServer::TYPE_HTTP, |
| 28 net::TestServer::kLocalhost, | 26 net::TestServer::kLocalhost, |
| 29 FilePath(kDocRoot)), | 27 FilePath(kDocRoot)), |
| 30 fake_default_request_context_getter_(NULL), | 28 fake_default_request_context_getter_(NULL), |
| 31 bridge_for_race_test_(NULL), | 29 bridge_for_race_test_(NULL), |
| 32 io_thread_(BrowserThread::IO) { | 30 io_thread_("IO thread") { |
| 33 } | 31 } |
| 34 | 32 |
| 35 virtual void SetUp() { | 33 virtual void SetUp() { |
| 36 io_thread_.StartIOThread(); | 34 base::Thread::Options options; |
| 35 options.message_loop_type = MessageLoop::TYPE_IO; |
| 36 io_thread_.StartWithOptions(options); |
| 37 } | 37 } |
| 38 | 38 |
| 39 virtual void TearDown() { | 39 virtual void TearDown() { |
| 40 if (fake_default_request_context_getter_) { | 40 if (fake_default_request_context_getter_) { |
| 41 GetIOThreadLoop()->ReleaseSoon(FROM_HERE, | 41 GetIOThreadLoop()->ReleaseSoon(FROM_HERE, |
| 42 fake_default_request_context_getter_); | 42 fake_default_request_context_getter_); |
| 43 fake_default_request_context_getter_ = NULL; | 43 fake_default_request_context_getter_ = NULL; |
| 44 } | 44 } |
| 45 io_thread_.Stop(); | 45 io_thread_.Stop(); |
| 46 } | 46 } |
| 47 | 47 |
| 48 HttpBridge* BuildBridge() { | 48 HttpBridge* BuildBridge() { |
| 49 if (!fake_default_request_context_getter_) { | 49 if (!fake_default_request_context_getter_) { |
| 50 fake_default_request_context_getter_ = | 50 fake_default_request_context_getter_ = |
| 51 new TestURLRequestContextGetter( | 51 new TestURLRequestContextGetter(io_thread_.message_loop_proxy()); |
| 52 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); | |
| 53 fake_default_request_context_getter_->AddRef(); | 52 fake_default_request_context_getter_->AddRef(); |
| 54 } | 53 } |
| 55 HttpBridge* bridge = new HttpBridge( | 54 HttpBridge* bridge = new HttpBridge( |
| 56 new HttpBridge::RequestContextGetter( | 55 new HttpBridge::RequestContextGetter( |
| 57 fake_default_request_context_getter_)); | 56 fake_default_request_context_getter_, |
| 57 "user agent")); |
| 58 return bridge; | 58 return bridge; |
| 59 } | 59 } |
| 60 | 60 |
| 61 static void Abort(HttpBridge* bridge) { | 61 static void Abort(HttpBridge* bridge) { |
| 62 bridge->Abort(); | 62 bridge->Abort(); |
| 63 } | 63 } |
| 64 | 64 |
| 65 // Used by AbortAndReleaseBeforeFetchCompletes to test an interesting race | 65 // Used by AbortAndReleaseBeforeFetchCompletes to test an interesting race |
| 66 // condition. | 66 // condition. |
| 67 void RunSyncThreadBridgeUseTest(base::WaitableEvent* signal_when_created, | 67 void RunSyncThreadBridgeUseTest(base::WaitableEvent* signal_when_created, |
| 68 base::WaitableEvent* signal_when_released); | 68 base::WaitableEvent* signal_when_released); |
| 69 | 69 |
| 70 static void TestSameHttpNetworkSession(MessageLoop* main_message_loop, | 70 static void TestSameHttpNetworkSession(MessageLoop* main_message_loop, |
| 71 SyncHttpBridgeTest* test) { | 71 SyncHttpBridgeTest* test) { |
| 72 scoped_refptr<HttpBridge> http_bridge(test->BuildBridge()); | 72 scoped_refptr<HttpBridge> http_bridge(test->BuildBridge()); |
| 73 EXPECT_TRUE(test->GetTestRequestContextGetter()); | 73 EXPECT_TRUE(test->GetTestRequestContextGetter()); |
| 74 net::HttpNetworkSession* test_session = | 74 net::HttpNetworkSession* test_session = |
| 75 test->GetTestRequestContextGetter()->GetURLRequestContext()-> | 75 test->GetTestRequestContextGetter()->GetURLRequestContext()-> |
| 76 http_transaction_factory()->GetSession(); | 76 http_transaction_factory()->GetSession(); |
| 77 EXPECT_EQ(test_session, | 77 EXPECT_EQ(test_session, |
| 78 http_bridge->GetRequestContextGetter()-> | 78 http_bridge->GetRequestContextGetter()-> |
| 79 GetURLRequestContext()-> | 79 GetURLRequestContext()-> |
| 80 http_transaction_factory()->GetSession()); | 80 http_transaction_factory()->GetSession()); |
| 81 main_message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 81 main_message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 82 } | 82 } |
| 83 | 83 |
| 84 MessageLoop* GetIOThreadLoop() { | 84 MessageLoop* GetIOThreadLoop() { |
| 85 return io_thread_.DeprecatedGetThreadObject()->message_loop(); | 85 return io_thread_.message_loop(); |
| 86 } | 86 } |
| 87 | 87 |
| 88 // Note this is lazy created, so don't call this before your bridge. | 88 // Note this is lazy created, so don't call this before your bridge. |
| 89 TestURLRequestContextGetter* GetTestRequestContextGetter() { | 89 TestURLRequestContextGetter* GetTestRequestContextGetter() { |
| 90 return fake_default_request_context_getter_; | 90 return fake_default_request_context_getter_; |
| 91 } | 91 } |
| 92 | 92 |
| 93 net::TestServer test_server_; | 93 net::TestServer test_server_; |
| 94 | 94 |
| 95 content::TestBrowserThread* io_thread() { return &io_thread_; } | 95 base::Thread* io_thread() { return &io_thread_; } |
| 96 | 96 |
| 97 HttpBridge* bridge_for_race_test() { return bridge_for_race_test_; } | 97 HttpBridge* bridge_for_race_test() { return bridge_for_race_test_; } |
| 98 | 98 |
| 99 private: | 99 private: |
| 100 // A make-believe "default" request context, as would be returned by | 100 // A make-believe "default" request context, as would be returned by |
| 101 // Profile::GetDefaultRequestContext(). Created lazily by BuildBridge. | 101 // Profile::GetDefaultRequestContext(). Created lazily by BuildBridge. |
| 102 TestURLRequestContextGetter* fake_default_request_context_getter_; | 102 TestURLRequestContextGetter* fake_default_request_context_getter_; |
| 103 | 103 |
| 104 HttpBridge* bridge_for_race_test_; | 104 HttpBridge* bridge_for_race_test_; |
| 105 | 105 |
| 106 // Separate thread for IO used by the HttpBridge. | 106 // Separate thread for IO used by the HttpBridge. |
| 107 content::TestBrowserThread io_thread_; | 107 base::Thread io_thread_; |
| 108 MessageLoop loop_; | 108 MessageLoop loop_; |
| 109 }; | 109 }; |
| 110 | 110 |
| 111 // An HttpBridge that doesn't actually make network requests and just calls | 111 // An HttpBridge that doesn't actually make network requests and just calls |
| 112 // back with dummy response info. | 112 // back with dummy response info. |
| 113 // TODO(tim): Instead of inheriting here we should inject a component | 113 // TODO(tim): Instead of inheriting here we should inject a component |
| 114 // responsible for the MakeAsynchronousPost bit. | 114 // responsible for the MakeAsynchronousPost bit. |
| 115 class ShuntedHttpBridge : public HttpBridge { | 115 class ShuntedHttpBridge : public HttpBridge { |
| 116 public: | 116 public: |
| 117 // If |never_finishes| is true, the simulated request never actually | 117 // If |never_finishes| is true, the simulated request never actually |
| 118 // returns. | 118 // returns. |
| 119 ShuntedHttpBridge(net::URLRequestContextGetter* baseline_context_getter, | 119 ShuntedHttpBridge(net::URLRequestContextGetter* baseline_context_getter, |
| 120 SyncHttpBridgeTest* test, bool never_finishes) | 120 SyncHttpBridgeTest* test, bool never_finishes) |
| 121 : HttpBridge(new HttpBridge::RequestContextGetter( | 121 : HttpBridge( |
| 122 baseline_context_getter)), | 122 new HttpBridge::RequestContextGetter( |
| 123 test_(test), never_finishes_(never_finishes) { } | 123 baseline_context_getter, "user agent")), |
| 124 test_(test), never_finishes_(never_finishes) { } |
| 124 protected: | 125 protected: |
| 125 virtual void MakeAsynchronousPost() { | 126 virtual void MakeAsynchronousPost() { |
| 126 ASSERT_TRUE(MessageLoop::current() == test_->GetIOThreadLoop()); | 127 ASSERT_TRUE(MessageLoop::current() == test_->GetIOThreadLoop()); |
| 127 if (never_finishes_) | 128 if (never_finishes_) |
| 128 return; | 129 return; |
| 129 | 130 |
| 130 // We don't actually want to make a request for this test, so just callback | 131 // We don't actually want to make a request for this test, so just callback |
| 131 // as if it completed. | 132 // as if it completed. |
| 132 test_->GetIOThreadLoop()->PostTask(FROM_HERE, | 133 test_->GetIOThreadLoop()->PostTask(FROM_HERE, |
| 133 base::Bind(&ShuntedHttpBridge::CallOnURLFetchComplete, this)); | 134 base::Bind(&ShuntedHttpBridge::CallOnURLFetchComplete, this)); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 149 OnURLFetchComplete(&fetcher); | 150 OnURLFetchComplete(&fetcher); |
| 150 } | 151 } |
| 151 SyncHttpBridgeTest* test_; | 152 SyncHttpBridgeTest* test_; |
| 152 bool never_finishes_; | 153 bool never_finishes_; |
| 153 }; | 154 }; |
| 154 | 155 |
| 155 void SyncHttpBridgeTest::RunSyncThreadBridgeUseTest( | 156 void SyncHttpBridgeTest::RunSyncThreadBridgeUseTest( |
| 156 base::WaitableEvent* signal_when_created, | 157 base::WaitableEvent* signal_when_created, |
| 157 base::WaitableEvent* signal_when_released) { | 158 base::WaitableEvent* signal_when_released) { |
| 158 scoped_refptr<net::URLRequestContextGetter> ctx_getter( | 159 scoped_refptr<net::URLRequestContextGetter> ctx_getter( |
| 159 new TestURLRequestContextGetter( | 160 new TestURLRequestContextGetter(io_thread_.message_loop_proxy())); |
| 160 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))); | |
| 161 { | 161 { |
| 162 scoped_refptr<ShuntedHttpBridge> bridge(new ShuntedHttpBridge( | 162 scoped_refptr<ShuntedHttpBridge> bridge(new ShuntedHttpBridge( |
| 163 ctx_getter, this, true)); | 163 ctx_getter, this, true)); |
| 164 bridge->SetUserAgent("bob"); | |
| 165 bridge->SetURL("http://www.google.com", 9999); | 164 bridge->SetURL("http://www.google.com", 9999); |
| 166 bridge->SetPostPayload("text/plain", 2, " "); | 165 bridge->SetPostPayload("text/plain", 2, " "); |
| 167 bridge_for_race_test_ = bridge; | 166 bridge_for_race_test_ = bridge; |
| 168 signal_when_created->Signal(); | 167 signal_when_created->Signal(); |
| 169 | 168 |
| 170 int os_error = 0; | 169 int os_error = 0; |
| 171 int response_code = 0; | 170 int response_code = 0; |
| 172 bridge->MakeSynchronousPost(&os_error, &response_code); | 171 bridge->MakeSynchronousPost(&os_error, &response_code); |
| 173 bridge_for_race_test_ = NULL; | 172 bridge_for_race_test_ = NULL; |
| 174 } | 173 } |
| 175 signal_when_released->Signal(); | 174 signal_when_released->Signal(); |
| 176 } | 175 } |
| 177 | 176 |
| 178 TEST_F(SyncHttpBridgeTest, TestUsesSameHttpNetworkSession) { | 177 TEST_F(SyncHttpBridgeTest, TestUsesSameHttpNetworkSession) { |
| 179 // Run this test on the IO thread because we can only call | 178 // Run this test on the IO thread because we can only call |
| 180 // URLRequestContextGetter::GetURLRequestContext on the IO thread. | 179 // URLRequestContextGetter::GetURLRequestContext on the IO thread. |
| 181 BrowserThread::PostTask( | 180 io_thread()->message_loop()->PostTask( |
| 182 BrowserThread::IO, FROM_HERE, | 181 FROM_HERE, |
| 183 base::Bind(&SyncHttpBridgeTest::TestSameHttpNetworkSession, | 182 base::Bind(&SyncHttpBridgeTest::TestSameHttpNetworkSession, |
| 184 MessageLoop::current(), this)); | 183 MessageLoop::current(), this)); |
| 185 MessageLoop::current()->Run(); | 184 MessageLoop::current()->Run(); |
| 186 } | 185 } |
| 187 | 186 |
| 188 // Test the HttpBridge without actually making any network requests. | 187 // Test the HttpBridge without actually making any network requests. |
| 189 TEST_F(SyncHttpBridgeTest, TestMakeSynchronousPostShunted) { | 188 TEST_F(SyncHttpBridgeTest, TestMakeSynchronousPostShunted) { |
| 190 scoped_refptr<net::URLRequestContextGetter> ctx_getter( | 189 scoped_refptr<net::URLRequestContextGetter> ctx_getter( |
| 191 new TestURLRequestContextGetter( | 190 new TestURLRequestContextGetter(io_thread()->message_loop_proxy())); |
| 192 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))); | |
| 193 scoped_refptr<HttpBridge> http_bridge(new ShuntedHttpBridge( | 191 scoped_refptr<HttpBridge> http_bridge(new ShuntedHttpBridge( |
| 194 ctx_getter, this, false)); | 192 ctx_getter, this, false)); |
| 195 http_bridge->SetUserAgent("bob"); | |
| 196 http_bridge->SetURL("http://www.google.com", 9999); | 193 http_bridge->SetURL("http://www.google.com", 9999); |
| 197 http_bridge->SetPostPayload("text/plain", 2, " "); | 194 http_bridge->SetPostPayload("text/plain", 2, " "); |
| 198 | 195 |
| 199 int os_error = 0; | 196 int os_error = 0; |
| 200 int response_code = 0; | 197 int response_code = 0; |
| 201 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 198 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
| 202 EXPECT_TRUE(success); | 199 EXPECT_TRUE(success); |
| 203 EXPECT_EQ(200, response_code); | 200 EXPECT_EQ(200, response_code); |
| 204 EXPECT_EQ(0, os_error); | 201 EXPECT_EQ(0, os_error); |
| 205 | 202 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 225 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 222 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
| 226 EXPECT_TRUE(success); | 223 EXPECT_TRUE(success); |
| 227 EXPECT_EQ(200, response_code); | 224 EXPECT_EQ(200, response_code); |
| 228 EXPECT_EQ(0, os_error); | 225 EXPECT_EQ(0, os_error); |
| 229 | 226 |
| 230 EXPECT_EQ(payload.length() + 1, | 227 EXPECT_EQ(payload.length() + 1, |
| 231 static_cast<size_t>(http_bridge->GetResponseContentLength())); | 228 static_cast<size_t>(http_bridge->GetResponseContentLength())); |
| 232 EXPECT_EQ(payload, std::string(http_bridge->GetResponseContent())); | 229 EXPECT_EQ(payload, std::string(http_bridge->GetResponseContent())); |
| 233 } | 230 } |
| 234 | 231 |
| 235 // Full round-trip test of the HttpBridge, using custom UA string | 232 // Full round-trip test of the HttpBridge. |
| 236 TEST_F(SyncHttpBridgeTest, TestMakeSynchronousPostLiveComprehensive) { | 233 TEST_F(SyncHttpBridgeTest, TestMakeSynchronousPostLiveComprehensive) { |
| 237 ASSERT_TRUE(test_server_.Start()); | 234 ASSERT_TRUE(test_server_.Start()); |
| 238 | 235 |
| 239 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | 236 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); |
| 240 | 237 |
| 241 GURL echo_header = test_server_.GetURL("echoall"); | 238 GURL echo_header = test_server_.GetURL("echoall"); |
| 242 http_bridge->SetUserAgent("bob"); | |
| 243 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); | 239 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); |
| 244 | 240 |
| 245 std::string test_payload = "###TEST PAYLOAD###"; | 241 std::string test_payload = "###TEST PAYLOAD###"; |
| 246 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, | 242 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, |
| 247 test_payload.c_str()); | 243 test_payload.c_str()); |
| 248 | 244 |
| 249 int os_error = 0; | 245 int os_error = 0; |
| 250 int response_code = 0; | 246 int response_code = 0; |
| 251 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 247 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
| 252 EXPECT_TRUE(success); | 248 EXPECT_TRUE(success); |
| 253 EXPECT_EQ(200, response_code); | 249 EXPECT_EQ(200, response_code); |
| 254 EXPECT_EQ(0, os_error); | 250 EXPECT_EQ(0, os_error); |
| 255 | 251 |
| 256 std::string response(http_bridge->GetResponseContent(), | 252 std::string response(http_bridge->GetResponseContent(), |
| 257 http_bridge->GetResponseContentLength()); | 253 http_bridge->GetResponseContentLength()); |
| 258 EXPECT_EQ(std::string::npos, response.find("Cookie:")); | 254 EXPECT_EQ(std::string::npos, response.find("Cookie:")); |
| 259 EXPECT_NE(std::string::npos, response.find("User-Agent: bob")); | 255 EXPECT_NE(std::string::npos, response.find("User-Agent: user agent")); |
| 260 EXPECT_NE(std::string::npos, response.find(test_payload.c_str())); | 256 EXPECT_NE(std::string::npos, response.find(test_payload.c_str())); |
| 261 } | 257 } |
| 262 | 258 |
| 263 TEST_F(SyncHttpBridgeTest, TestExtraRequestHeaders) { | 259 TEST_F(SyncHttpBridgeTest, TestExtraRequestHeaders) { |
| 264 ASSERT_TRUE(test_server_.Start()); | 260 ASSERT_TRUE(test_server_.Start()); |
| 265 | 261 |
| 266 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | 262 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); |
| 267 | 263 |
| 268 GURL echo_header = test_server_.GetURL("echoall"); | 264 GURL echo_header = test_server_.GetURL("echoall"); |
| 269 | 265 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 EXPECT_TRUE(success); | 302 EXPECT_TRUE(success); |
| 307 EXPECT_EQ(200, response_code); | 303 EXPECT_EQ(200, response_code); |
| 308 EXPECT_EQ(0, os_error); | 304 EXPECT_EQ(0, os_error); |
| 309 | 305 |
| 310 EXPECT_EQ(http_bridge->GetResponseHeaderValue("Content-type"), "text/html"); | 306 EXPECT_EQ(http_bridge->GetResponseHeaderValue("Content-type"), "text/html"); |
| 311 EXPECT_TRUE(http_bridge->GetResponseHeaderValue("invalid-header").empty()); | 307 EXPECT_TRUE(http_bridge->GetResponseHeaderValue("invalid-header").empty()); |
| 312 } | 308 } |
| 313 | 309 |
| 314 TEST_F(SyncHttpBridgeTest, Abort) { | 310 TEST_F(SyncHttpBridgeTest, Abort) { |
| 315 scoped_refptr<net::URLRequestContextGetter> ctx_getter( | 311 scoped_refptr<net::URLRequestContextGetter> ctx_getter( |
| 316 new TestURLRequestContextGetter( | 312 new TestURLRequestContextGetter(io_thread()->message_loop_proxy())); |
| 317 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))); | |
| 318 scoped_refptr<ShuntedHttpBridge> http_bridge(new ShuntedHttpBridge( | 313 scoped_refptr<ShuntedHttpBridge> http_bridge(new ShuntedHttpBridge( |
| 319 ctx_getter, this, true)); | 314 ctx_getter, this, true)); |
| 320 http_bridge->SetUserAgent("bob"); | |
| 321 http_bridge->SetURL("http://www.google.com", 9999); | 315 http_bridge->SetURL("http://www.google.com", 9999); |
| 322 http_bridge->SetPostPayload("text/plain", 2, " "); | 316 http_bridge->SetPostPayload("text/plain", 2, " "); |
| 323 | 317 |
| 324 int os_error = 0; | 318 int os_error = 0; |
| 325 int response_code = 0; | 319 int response_code = 0; |
| 326 | 320 |
| 327 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 321 io_thread()->message_loop_proxy()->PostTask( |
| 328 base::Bind(&SyncHttpBridgeTest::Abort, http_bridge)); | 322 FROM_HERE, |
| 323 base::Bind(&SyncHttpBridgeTest::Abort, http_bridge)); |
| 329 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 324 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
| 330 EXPECT_FALSE(success); | 325 EXPECT_FALSE(success); |
| 331 EXPECT_EQ(net::ERR_ABORTED, os_error); | 326 EXPECT_EQ(net::ERR_ABORTED, os_error); |
| 332 } | 327 } |
| 333 | 328 |
| 334 TEST_F(SyncHttpBridgeTest, AbortLate) { | 329 TEST_F(SyncHttpBridgeTest, AbortLate) { |
| 335 scoped_refptr<net::URLRequestContextGetter> ctx_getter( | 330 scoped_refptr<net::URLRequestContextGetter> ctx_getter( |
| 336 new TestURLRequestContextGetter( | 331 new TestURLRequestContextGetter(io_thread()->message_loop_proxy())); |
| 337 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))); | |
| 338 scoped_refptr<ShuntedHttpBridge> http_bridge(new ShuntedHttpBridge( | 332 scoped_refptr<ShuntedHttpBridge> http_bridge(new ShuntedHttpBridge( |
| 339 ctx_getter, this, false)); | 333 ctx_getter, this, false)); |
| 340 http_bridge->SetUserAgent("bob"); | |
| 341 http_bridge->SetURL("http://www.google.com", 9999); | 334 http_bridge->SetURL("http://www.google.com", 9999); |
| 342 http_bridge->SetPostPayload("text/plain", 2, " "); | 335 http_bridge->SetPostPayload("text/plain", 2, " "); |
| 343 | 336 |
| 344 int os_error = 0; | 337 int os_error = 0; |
| 345 int response_code = 0; | 338 int response_code = 0; |
| 346 | 339 |
| 347 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 340 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
| 348 ASSERT_TRUE(success); | 341 ASSERT_TRUE(success); |
| 349 http_bridge->Abort(); | 342 http_bridge->Abort(); |
| 350 // Ensures no double-free of URLFetcher, etc. | 343 // Ensures no double-free of URLFetcher, etc. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 362 base::WaitableEvent signal_when_created(false, false); | 355 base::WaitableEvent signal_when_created(false, false); |
| 363 base::WaitableEvent signal_when_released(false, false); | 356 base::WaitableEvent signal_when_released(false, false); |
| 364 sync_thread.message_loop()->PostTask(FROM_HERE, | 357 sync_thread.message_loop()->PostTask(FROM_HERE, |
| 365 base::Bind(&SyncHttpBridgeTest::RunSyncThreadBridgeUseTest, | 358 base::Bind(&SyncHttpBridgeTest::RunSyncThreadBridgeUseTest, |
| 366 base::Unretained(this), | 359 base::Unretained(this), |
| 367 &signal_when_created, | 360 &signal_when_created, |
| 368 &signal_when_released)); | 361 &signal_when_released)); |
| 369 | 362 |
| 370 // Stop IO so we can control order of operations. | 363 // Stop IO so we can control order of operations. |
| 371 base::WaitableEvent io_waiter(false, false); | 364 base::WaitableEvent io_waiter(false, false); |
| 372 ASSERT_TRUE(BrowserThread::PostTask( | 365 ASSERT_TRUE(io_thread()->message_loop_proxy()->PostTask( |
| 373 BrowserThread::IO, FROM_HERE, | 366 FROM_HERE, |
| 374 base::Bind(&base::WaitableEvent::Wait, base::Unretained(&io_waiter)))); | 367 base::Bind(&base::WaitableEvent::Wait, base::Unretained(&io_waiter)))); |
| 375 | 368 |
| 376 signal_when_created.Wait(); // Wait till we have a bridge to abort. | 369 signal_when_created.Wait(); // Wait till we have a bridge to abort. |
| 377 ASSERT_TRUE(bridge_for_race_test()); | 370 ASSERT_TRUE(bridge_for_race_test()); |
| 378 | 371 |
| 379 // Schedule the fetch completion callback (but don't run it yet). Don't take | 372 // Schedule the fetch completion callback (but don't run it yet). Don't take |
| 380 // a reference to the bridge to mimic URLFetcher's handling of the delegate. | 373 // a reference to the bridge to mimic URLFetcher's handling of the delegate. |
| 381 net::URLFetcherDelegate* delegate = | 374 net::URLFetcherDelegate* delegate = |
| 382 static_cast<net::URLFetcherDelegate*>(bridge_for_race_test()); | 375 static_cast<net::URLFetcherDelegate*>(bridge_for_race_test()); |
| 383 net::ResponseCookies cookies; | 376 net::ResponseCookies cookies; |
| 384 std::string response_content = "success!"; | 377 std::string response_content = "success!"; |
| 385 net::TestURLFetcher fetcher(0, GURL(), NULL); | 378 net::TestURLFetcher fetcher(0, GURL(), NULL); |
| 386 fetcher.set_url(GURL("www.google.com")); | 379 fetcher.set_url(GURL("www.google.com")); |
| 387 fetcher.set_response_code(200); | 380 fetcher.set_response_code(200); |
| 388 fetcher.set_cookies(cookies); | 381 fetcher.set_cookies(cookies); |
| 389 fetcher.SetResponseString(response_content); | 382 fetcher.SetResponseString(response_content); |
| 390 ASSERT_TRUE(BrowserThread::PostTask( | 383 ASSERT_TRUE(io_thread()->message_loop_proxy()->PostTask( |
| 391 BrowserThread::IO, FROM_HERE, | 384 FROM_HERE, |
| 392 base::Bind(&net::URLFetcherDelegate::OnURLFetchComplete, | 385 base::Bind(&net::URLFetcherDelegate::OnURLFetchComplete, |
| 393 base::Unretained(delegate), &fetcher))); | 386 base::Unretained(delegate), &fetcher))); |
| 394 | 387 |
| 395 // Abort the fetch. This should be smart enough to handle the case where | 388 // Abort the fetch. This should be smart enough to handle the case where |
| 396 // the bridge is destroyed before the callback scheduled above completes. | 389 // the bridge is destroyed before the callback scheduled above completes. |
| 397 bridge_for_race_test()->Abort(); | 390 bridge_for_race_test()->Abort(); |
| 398 | 391 |
| 399 // Wait until the sync thread releases its ref on the bridge. | 392 // Wait until the sync thread releases its ref on the bridge. |
| 400 signal_when_released.Wait(); | 393 signal_when_released.Wait(); |
| 401 ASSERT_FALSE(bridge_for_race_test()); | 394 ASSERT_FALSE(bridge_for_race_test()); |
| 402 | 395 |
| 403 // Unleash the hounds. The fetch completion callback should fire first, and | 396 // Unleash the hounds. The fetch completion callback should fire first, and |
| 404 // succeed even though we Release()d the bridge above because the call to | 397 // succeed even though we Release()d the bridge above because the call to |
| 405 // Abort should have held a reference. | 398 // Abort should have held a reference. |
| 406 io_waiter.Signal(); | 399 io_waiter.Signal(); |
| 407 | 400 |
| 408 // Done. | 401 // Done. |
| 409 sync_thread.Stop(); | 402 sync_thread.Stop(); |
| 410 io_thread()->Stop(); | 403 io_thread()->Stop(); |
| 411 } | 404 } |
| OLD | NEW |