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 "base/message_loop_proxy.h" |
5 #include "base/thread.h" | 6 #include "base/thread.h" |
6 #include "chrome/browser/chrome_thread.h" | 7 #include "chrome/browser/chrome_thread.h" |
7 #include "chrome/browser/net/test_url_fetcher_factory.h" | 8 #include "chrome/browser/net/test_url_fetcher_factory.h" |
8 #include "chrome/browser/sync/glue/http_bridge.h" | 9 #include "chrome/browser/sync/glue/http_bridge.h" |
9 #include "net/url_request/url_request_unittest.h" | 10 #include "net/url_request/url_request_unittest.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 | 12 |
12 using browser_sync::HttpBridge; | 13 using browser_sync::HttpBridge; |
13 | 14 |
14 namespace { | 15 namespace { |
15 // TODO(timsteele): Should use PathService here. See Chromium Issue 3113. | 16 // TODO(timsteele): Should use PathService here. See Chromium Issue 3113. |
16 const wchar_t kDocRoot[] = L"chrome/test/data"; | 17 const wchar_t kDocRoot[] = L"chrome/test/data"; |
17 } | 18 } |
18 | 19 |
19 // Lazy getter for TestURLRequestContext instances. | 20 // Lazy getter for TestURLRequestContext instances. |
20 class TestURLRequestContextGetter : public URLRequestContextGetter { | 21 class TestURLRequestContextGetter : public URLRequestContextGetter { |
21 public: | 22 public: |
22 virtual URLRequestContext* GetURLRequestContext() { | 23 virtual URLRequestContext* GetURLRequestContext() { |
23 if (!context_) | 24 if (!context_) |
24 context_ = new TestURLRequestContext; | 25 context_ = new TestURLRequestContext; |
25 return context_; | 26 return context_; |
26 } | 27 } |
| 28 virtual scoped_refptr<MessageLoopProxy> GetIOMessageLoopProxy() { |
| 29 return ChromeThread::GetMessageLoopProxyForThread(ChromeThread::IO); |
| 30 } |
| 31 |
27 private: | 32 private: |
28 ~TestURLRequestContextGetter() {} | 33 ~TestURLRequestContextGetter() {} |
29 | 34 |
30 scoped_refptr<URLRequestContext> context_; | 35 scoped_refptr<URLRequestContext> context_; |
31 }; | 36 }; |
32 | 37 |
33 class HttpBridgeTest : public testing::Test { | 38 class HttpBridgeTest : public testing::Test { |
34 public: | 39 public: |
35 HttpBridgeTest() | 40 HttpBridgeTest() |
36 : fake_default_request_context_getter_(NULL), | 41 : fake_default_request_context_getter_(NULL), |
(...skipping 17 matching lines...) Expand all Loading... |
54 if (!fake_default_request_context_getter_) { | 59 if (!fake_default_request_context_getter_) { |
55 fake_default_request_context_getter_ = new TestURLRequestContextGetter(); | 60 fake_default_request_context_getter_ = new TestURLRequestContextGetter(); |
56 fake_default_request_context_getter_->AddRef(); | 61 fake_default_request_context_getter_->AddRef(); |
57 } | 62 } |
58 HttpBridge* bridge = new HttpBridge( | 63 HttpBridge* bridge = new HttpBridge( |
59 new HttpBridge::RequestContextGetter( | 64 new HttpBridge::RequestContextGetter( |
60 fake_default_request_context_getter_)); | 65 fake_default_request_context_getter_)); |
61 return bridge; | 66 return bridge; |
62 } | 67 } |
63 | 68 |
| 69 static void TestSameHttpNetworkSession(MessageLoop* main_message_loop, |
| 70 HttpBridgeTest* test) { |
| 71 scoped_refptr<HttpBridge> http_bridge(test->BuildBridge()); |
| 72 EXPECT_TRUE(test->GetTestRequestContextGetter()); |
| 73 net::HttpNetworkSession* test_session = |
| 74 test->GetTestRequestContextGetter()->GetURLRequestContext()-> |
| 75 http_transaction_factory()->GetSession(); |
| 76 EXPECT_EQ(test_session, |
| 77 http_bridge->GetRequestContextGetter()-> |
| 78 GetURLRequestContext()-> |
| 79 http_transaction_factory()->GetSession()); |
| 80 main_message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask); |
| 81 } |
| 82 |
64 MessageLoop* io_thread_loop() { return io_thread_.message_loop(); } | 83 MessageLoop* io_thread_loop() { return io_thread_.message_loop(); } |
65 | 84 |
66 // Note this is lazy created, so don't call this before your bridge. | 85 // Note this is lazy created, so don't call this before your bridge. |
67 TestURLRequestContextGetter* GetTestRequestContextGetter() { | 86 TestURLRequestContextGetter* GetTestRequestContextGetter() { |
68 return fake_default_request_context_getter_; | 87 return fake_default_request_context_getter_; |
69 } | 88 } |
70 | 89 |
71 private: | 90 private: |
72 // A make-believe "default" request context, as would be returned by | 91 // A make-believe "default" request context, as would be returned by |
73 // Profile::GetDefaultRequestContext(). Created lazily by BuildBridge. | 92 // Profile::GetDefaultRequestContext(). Created lazily by BuildBridge. |
74 TestURLRequestContextGetter* fake_default_request_context_getter_; | 93 TestURLRequestContextGetter* fake_default_request_context_getter_; |
75 | 94 |
76 // Separate thread for IO used by the HttpBridge. | 95 // Separate thread for IO used by the HttpBridge. |
77 ChromeThread io_thread_; | 96 ChromeThread io_thread_; |
78 | 97 MessageLoop loop_; |
79 }; | 98 }; |
80 | 99 |
81 class DummyURLFetcher : public TestURLFetcher { | 100 class DummyURLFetcher : public TestURLFetcher { |
82 public: | 101 public: |
83 DummyURLFetcher() : TestURLFetcher(GURL(), POST, NULL) {} | 102 DummyURLFetcher() : TestURLFetcher(GURL(), POST, NULL) {} |
84 | 103 |
85 net::HttpResponseHeaders* response_headers() const { | 104 net::HttpResponseHeaders* response_headers() const { |
86 return NULL; | 105 return NULL; |
87 } | 106 } |
88 }; | 107 }; |
(...skipping 25 matching lines...) Expand all Loading... |
114 | 133 |
115 std::string response_content = "success!"; | 134 std::string response_content = "success!"; |
116 DummyURLFetcher fetcher; | 135 DummyURLFetcher fetcher; |
117 OnURLFetchComplete(&fetcher, GURL("www.google.com"), URLRequestStatus(), | 136 OnURLFetchComplete(&fetcher, GURL("www.google.com"), URLRequestStatus(), |
118 200, cookies, response_content); | 137 200, cookies, response_content); |
119 } | 138 } |
120 HttpBridgeTest* test_; | 139 HttpBridgeTest* test_; |
121 }; | 140 }; |
122 | 141 |
123 TEST_F(HttpBridgeTest, TestUsesSameHttpNetworkSession) { | 142 TEST_F(HttpBridgeTest, TestUsesSameHttpNetworkSession) { |
124 scoped_refptr<HttpBridge> http_bridge(this->BuildBridge()); | 143 // Run this test on the IO thread because we can only call |
125 EXPECT_TRUE(GetTestRequestContextGetter()); | 144 // URLRequestContextGetter::GetURLRequestContext on the IO thread. |
126 net::HttpNetworkSession* test_session = | 145 ChromeThread::PostTask( |
127 GetTestRequestContextGetter()->GetURLRequestContext()-> | 146 ChromeThread::IO, FROM_HERE, |
128 http_transaction_factory()->GetSession(); | 147 NewRunnableFunction(&HttpBridgeTest::TestSameHttpNetworkSession, |
129 EXPECT_EQ(test_session, | 148 MessageLoop::current(), this)); |
130 http_bridge->GetRequestContextGetter()-> | 149 MessageLoop::current()->Run(); |
131 GetURLRequestContext()-> | |
132 http_transaction_factory()->GetSession()); | |
133 } | 150 } |
134 | 151 |
135 // Test the HttpBridge without actually making any network requests. | 152 // Test the HttpBridge without actually making any network requests. |
136 TEST_F(HttpBridgeTest, TestMakeSynchronousPostShunted) { | 153 TEST_F(HttpBridgeTest, TestMakeSynchronousPostShunted) { |
137 scoped_refptr<URLRequestContextGetter> ctx_getter( | 154 scoped_refptr<URLRequestContextGetter> ctx_getter( |
138 new TestURLRequestContextGetter()); | 155 new TestURLRequestContextGetter()); |
139 scoped_refptr<HttpBridge> http_bridge(new ShuntedHttpBridge( | 156 scoped_refptr<HttpBridge> http_bridge(new ShuntedHttpBridge( |
140 ctx_getter, this)); | 157 ctx_getter, this)); |
141 http_bridge->SetUserAgent("bob"); | 158 http_bridge->SetUserAgent("bob"); |
142 http_bridge->SetURL("http://www.google.com", 9999); | 159 http_bridge->SetURL("http://www.google.com", 9999); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 int os_error = 0; | 271 int os_error = 0; |
255 int response_code = 0; | 272 int response_code = 0; |
256 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 273 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
257 EXPECT_TRUE(success); | 274 EXPECT_TRUE(success); |
258 EXPECT_EQ(200, response_code); | 275 EXPECT_EQ(200, response_code); |
259 EXPECT_EQ(0, os_error); | 276 EXPECT_EQ(0, os_error); |
260 | 277 |
261 EXPECT_EQ(http_bridge->GetResponseHeaderValue("Content-type"), "text/html"); | 278 EXPECT_EQ(http_bridge->GetResponseHeaderValue("Content-type"), "text/html"); |
262 EXPECT_TRUE(http_bridge->GetResponseHeaderValue("invalid-header").empty()); | 279 EXPECT_TRUE(http_bridge->GetResponseHeaderValue("invalid-header").empty()); |
263 } | 280 } |
OLD | NEW |