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 "net/test/test_server.h" | 8 #include "net/test/test_server.h" |
9 #include "net/url_request/test_url_fetcher_factory.h" | 9 #include "net/url_request/test_url_fetcher_factory.h" |
10 #include "net/url_request/url_fetcher_delegate.h" | 10 #include "net/url_request/url_fetcher_delegate.h" |
(...skipping 30 matching lines...) Expand all Loading... |
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(io_thread_.message_loop_proxy()); | 51 new net::TestURLRequestContextGetter(io_thread_.message_loop_proxy()); |
52 fake_default_request_context_getter_->AddRef(); | 52 fake_default_request_context_getter_->AddRef(); |
53 } | 53 } |
54 HttpBridge* bridge = new HttpBridge( | 54 HttpBridge* bridge = new HttpBridge( |
55 new HttpBridge::RequestContextGetter( | 55 new HttpBridge::RequestContextGetter( |
56 fake_default_request_context_getter_, | 56 fake_default_request_context_getter_, |
57 "user agent")); | 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) { |
(...skipping 17 matching lines...) Expand all Loading... |
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_.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 net::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 base::Thread* 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 net::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 base::Thread 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. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 OnURLFetchComplete(&fetcher); | 150 OnURLFetchComplete(&fetcher); |
151 } | 151 } |
152 SyncHttpBridgeTest* test_; | 152 SyncHttpBridgeTest* test_; |
153 bool never_finishes_; | 153 bool never_finishes_; |
154 }; | 154 }; |
155 | 155 |
156 void SyncHttpBridgeTest::RunSyncThreadBridgeUseTest( | 156 void SyncHttpBridgeTest::RunSyncThreadBridgeUseTest( |
157 base::WaitableEvent* signal_when_created, | 157 base::WaitableEvent* signal_when_created, |
158 base::WaitableEvent* signal_when_released) { | 158 base::WaitableEvent* signal_when_released) { |
159 scoped_refptr<net::URLRequestContextGetter> ctx_getter( | 159 scoped_refptr<net::URLRequestContextGetter> ctx_getter( |
160 new TestURLRequestContextGetter(io_thread_.message_loop_proxy())); | 160 new net::TestURLRequestContextGetter(io_thread_.message_loop_proxy())); |
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->SetURL("http://www.google.com", 9999); | 164 bridge->SetURL("http://www.google.com", 9999); |
165 bridge->SetPostPayload("text/plain", 2, " "); | 165 bridge->SetPostPayload("text/plain", 2, " "); |
166 bridge_for_race_test_ = bridge; | 166 bridge_for_race_test_ = bridge; |
167 signal_when_created->Signal(); | 167 signal_when_created->Signal(); |
168 | 168 |
169 int os_error = 0; | 169 int os_error = 0; |
170 int response_code = 0; | 170 int response_code = 0; |
171 bridge->MakeSynchronousPost(&os_error, &response_code); | 171 bridge->MakeSynchronousPost(&os_error, &response_code); |
172 bridge_for_race_test_ = NULL; | 172 bridge_for_race_test_ = NULL; |
173 } | 173 } |
174 signal_when_released->Signal(); | 174 signal_when_released->Signal(); |
175 } | 175 } |
176 | 176 |
177 TEST_F(SyncHttpBridgeTest, TestUsesSameHttpNetworkSession) { | 177 TEST_F(SyncHttpBridgeTest, TestUsesSameHttpNetworkSession) { |
178 // 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 |
179 // URLRequestContextGetter::GetURLRequestContext on the IO thread. | 179 // URLRequestContextGetter::GetURLRequestContext on the IO thread. |
180 io_thread()->message_loop()->PostTask( | 180 io_thread()->message_loop()->PostTask( |
181 FROM_HERE, | 181 FROM_HERE, |
182 base::Bind(&SyncHttpBridgeTest::TestSameHttpNetworkSession, | 182 base::Bind(&SyncHttpBridgeTest::TestSameHttpNetworkSession, |
183 MessageLoop::current(), this)); | 183 MessageLoop::current(), this)); |
184 MessageLoop::current()->Run(); | 184 MessageLoop::current()->Run(); |
185 } | 185 } |
186 | 186 |
187 // Test the HttpBridge without actually making any network requests. | 187 // Test the HttpBridge without actually making any network requests. |
188 TEST_F(SyncHttpBridgeTest, TestMakeSynchronousPostShunted) { | 188 TEST_F(SyncHttpBridgeTest, TestMakeSynchronousPostShunted) { |
189 scoped_refptr<net::URLRequestContextGetter> ctx_getter( | 189 scoped_refptr<net::URLRequestContextGetter> ctx_getter( |
190 new TestURLRequestContextGetter(io_thread()->message_loop_proxy())); | 190 new net::TestURLRequestContextGetter(io_thread()->message_loop_proxy())); |
191 scoped_refptr<HttpBridge> http_bridge(new ShuntedHttpBridge( | 191 scoped_refptr<HttpBridge> http_bridge(new ShuntedHttpBridge( |
192 ctx_getter, this, false)); | 192 ctx_getter, this, false)); |
193 http_bridge->SetURL("http://www.google.com", 9999); | 193 http_bridge->SetURL("http://www.google.com", 9999); |
194 http_bridge->SetPostPayload("text/plain", 2, " "); | 194 http_bridge->SetPostPayload("text/plain", 2, " "); |
195 | 195 |
196 int os_error = 0; | 196 int os_error = 0; |
197 int response_code = 0; | 197 int response_code = 0; |
198 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 198 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
199 EXPECT_TRUE(success); | 199 EXPECT_TRUE(success); |
200 EXPECT_EQ(200, response_code); | 200 EXPECT_EQ(200, response_code); |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 EXPECT_TRUE(success); | 302 EXPECT_TRUE(success); |
303 EXPECT_EQ(200, response_code); | 303 EXPECT_EQ(200, response_code); |
304 EXPECT_EQ(0, os_error); | 304 EXPECT_EQ(0, os_error); |
305 | 305 |
306 EXPECT_EQ(http_bridge->GetResponseHeaderValue("Content-type"), "text/html"); | 306 EXPECT_EQ(http_bridge->GetResponseHeaderValue("Content-type"), "text/html"); |
307 EXPECT_TRUE(http_bridge->GetResponseHeaderValue("invalid-header").empty()); | 307 EXPECT_TRUE(http_bridge->GetResponseHeaderValue("invalid-header").empty()); |
308 } | 308 } |
309 | 309 |
310 TEST_F(SyncHttpBridgeTest, Abort) { | 310 TEST_F(SyncHttpBridgeTest, Abort) { |
311 scoped_refptr<net::URLRequestContextGetter> ctx_getter( | 311 scoped_refptr<net::URLRequestContextGetter> ctx_getter( |
312 new TestURLRequestContextGetter(io_thread()->message_loop_proxy())); | 312 new net::TestURLRequestContextGetter(io_thread()->message_loop_proxy())); |
313 scoped_refptr<ShuntedHttpBridge> http_bridge(new ShuntedHttpBridge( | 313 scoped_refptr<ShuntedHttpBridge> http_bridge(new ShuntedHttpBridge( |
314 ctx_getter, this, true)); | 314 ctx_getter, this, true)); |
315 http_bridge->SetURL("http://www.google.com", 9999); | 315 http_bridge->SetURL("http://www.google.com", 9999); |
316 http_bridge->SetPostPayload("text/plain", 2, " "); | 316 http_bridge->SetPostPayload("text/plain", 2, " "); |
317 | 317 |
318 int os_error = 0; | 318 int os_error = 0; |
319 int response_code = 0; | 319 int response_code = 0; |
320 | 320 |
321 io_thread()->message_loop_proxy()->PostTask( | 321 io_thread()->message_loop_proxy()->PostTask( |
322 FROM_HERE, | 322 FROM_HERE, |
323 base::Bind(&SyncHttpBridgeTest::Abort, http_bridge)); | 323 base::Bind(&SyncHttpBridgeTest::Abort, http_bridge)); |
324 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 324 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
325 EXPECT_FALSE(success); | 325 EXPECT_FALSE(success); |
326 EXPECT_EQ(net::ERR_ABORTED, os_error); | 326 EXPECT_EQ(net::ERR_ABORTED, os_error); |
327 } | 327 } |
328 | 328 |
329 TEST_F(SyncHttpBridgeTest, AbortLate) { | 329 TEST_F(SyncHttpBridgeTest, AbortLate) { |
330 scoped_refptr<net::URLRequestContextGetter> ctx_getter( | 330 scoped_refptr<net::URLRequestContextGetter> ctx_getter( |
331 new TestURLRequestContextGetter(io_thread()->message_loop_proxy())); | 331 new net::TestURLRequestContextGetter(io_thread()->message_loop_proxy())); |
332 scoped_refptr<ShuntedHttpBridge> http_bridge(new ShuntedHttpBridge( | 332 scoped_refptr<ShuntedHttpBridge> http_bridge(new ShuntedHttpBridge( |
333 ctx_getter, this, false)); | 333 ctx_getter, this, false)); |
334 http_bridge->SetURL("http://www.google.com", 9999); | 334 http_bridge->SetURL("http://www.google.com", 9999); |
335 http_bridge->SetPostPayload("text/plain", 2, " "); | 335 http_bridge->SetPostPayload("text/plain", 2, " "); |
336 | 336 |
337 int os_error = 0; | 337 int os_error = 0; |
338 int response_code = 0; | 338 int response_code = 0; |
339 | 339 |
340 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 340 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
341 ASSERT_TRUE(success); | 341 ASSERT_TRUE(success); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 // 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 |
398 // Abort should have held a reference. | 398 // Abort should have held a reference. |
399 io_waiter.Signal(); | 399 io_waiter.Signal(); |
400 | 400 |
401 // Done. | 401 // Done. |
402 sync_thread.Stop(); | 402 sync_thread.Stop(); |
403 io_thread()->Stop(); | 403 io_thread()->Stop(); |
404 } | 404 } |
405 | 405 |
406 } // namespace syncer | 406 } // namespace syncer |
OLD | NEW |