| 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 #if defined(BROWSER_SYNC) | 5 #if defined(BROWSER_SYNC) |
| 6 | 6 |
| 7 #include "base/thread.h" | 7 #include "base/thread.h" |
| 8 #include "chrome/browser/chrome_thread.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 } |
| 27 private: | 28 private: |
| 28 scoped_refptr<URLRequestContext> context_; | 29 scoped_refptr<URLRequestContext> context_; |
| 29 }; | 30 }; |
| 30 | 31 |
| 31 class HttpBridgeTest : public testing::Test { | 32 class HttpBridgeTest : public testing::Test { |
| 32 public: | 33 public: |
| 33 HttpBridgeTest() | 34 HttpBridgeTest() |
| 34 : fake_default_request_context_getter_(NULL), | 35 : fake_default_request_context_getter_(NULL), |
| 35 io_thread_("HttpBridgeTest IO thread") { | 36 io_thread_(ChromeThread::IO) { |
| 36 } | 37 } |
| 37 | 38 |
| 38 virtual void SetUp() { | 39 virtual void SetUp() { |
| 39 base::Thread::Options options; | 40 base::Thread::Options options; |
| 40 options.message_loop_type = MessageLoop::TYPE_IO; | 41 options.message_loop_type = MessageLoop::TYPE_IO; |
| 41 io_thread_.StartWithOptions(options); | 42 io_thread_.StartWithOptions(options); |
| 42 } | 43 } |
| 43 | 44 |
| 44 virtual void TearDown() { | 45 virtual void TearDown() { |
| 45 io_thread_loop()->ReleaseSoon(FROM_HERE, | 46 io_thread_loop()->ReleaseSoon(FROM_HERE, |
| 46 fake_default_request_context_getter_); | 47 fake_default_request_context_getter_); |
| 47 io_thread_.Stop(); | 48 io_thread_.Stop(); |
| 48 fake_default_request_context_getter_ = NULL; | 49 fake_default_request_context_getter_ = NULL; |
| 49 } | 50 } |
| 50 | 51 |
| 51 HttpBridge* BuildBridge() { | 52 HttpBridge* BuildBridge() { |
| 52 if (!fake_default_request_context_getter_) { | 53 if (!fake_default_request_context_getter_) { |
| 53 fake_default_request_context_getter_ = new TestURLRequestContextGetter(); | 54 fake_default_request_context_getter_ = new TestURLRequestContextGetter(); |
| 54 fake_default_request_context_getter_->AddRef(); | 55 fake_default_request_context_getter_->AddRef(); |
| 55 } | 56 } |
| 56 HttpBridge* bridge = new HttpBridge( | 57 HttpBridge* bridge = new HttpBridge( |
| 57 new HttpBridge::RequestContextGetter( | 58 new HttpBridge::RequestContextGetter( |
| 58 fake_default_request_context_getter_), | 59 fake_default_request_context_getter_)); |
| 59 io_thread_loop()); | |
| 60 bridge->use_io_loop_for_testing_ = true; | |
| 61 return bridge; | 60 return bridge; |
| 62 } | 61 } |
| 63 | 62 |
| 64 MessageLoop* io_thread_loop() { return io_thread_.message_loop(); } | 63 MessageLoop* io_thread_loop() { return io_thread_.message_loop(); } |
| 65 | 64 |
| 66 // Note this is lazy created, so don't call this before your bridge. | 65 // Note this is lazy created, so don't call this before your bridge. |
| 67 TestURLRequestContextGetter* GetTestRequestContextGetter() { | 66 TestURLRequestContextGetter* GetTestRequestContextGetter() { |
| 68 return fake_default_request_context_getter_; | 67 return fake_default_request_context_getter_; |
| 69 } | 68 } |
| 70 | 69 |
| 71 private: | 70 private: |
| 72 // A make-believe "default" request context, as would be returned by | 71 // A make-believe "default" request context, as would be returned by |
| 73 // Profile::GetDefaultRequestContext(). Created lazily by BuildBridge. | 72 // Profile::GetDefaultRequestContext(). Created lazily by BuildBridge. |
| 74 TestURLRequestContextGetter* fake_default_request_context_getter_; | 73 TestURLRequestContextGetter* fake_default_request_context_getter_; |
| 75 | 74 |
| 76 // Separate thread for IO used by the HttpBridge. | 75 // Separate thread for IO used by the HttpBridge. |
| 77 base::Thread io_thread_; | 76 ChromeThread io_thread_; |
| 77 |
| 78 }; | 78 }; |
| 79 | 79 |
| 80 // An HttpBridge that doesn't actually make network requests and just calls | 80 // An HttpBridge that doesn't actually make network requests and just calls |
| 81 // back with dummy response info. | 81 // back with dummy response info. |
| 82 class ShuntedHttpBridge : public HttpBridge { | 82 class ShuntedHttpBridge : public HttpBridge { |
| 83 public: | 83 public: |
| 84 ShuntedHttpBridge(URLRequestContextGetter* baseline_context_getter, | 84 ShuntedHttpBridge(URLRequestContextGetter* baseline_context_getter, |
| 85 MessageLoop* io_loop, HttpBridgeTest* test) | 85 HttpBridgeTest* test) |
| 86 : HttpBridge(new HttpBridge::RequestContextGetter( | 86 : HttpBridge(new HttpBridge::RequestContextGetter( |
| 87 baseline_context_getter), | 87 baseline_context_getter)), |
| 88 io_loop), test_(test) { } | 88 test_(test) { } |
| 89 protected: | 89 protected: |
| 90 virtual void MakeAsynchronousPost() { | 90 virtual void MakeAsynchronousPost() { |
| 91 ASSERT_TRUE(MessageLoop::current() == test_->io_thread_loop()); | 91 ASSERT_TRUE(MessageLoop::current() == test_->io_thread_loop()); |
| 92 // We don't actually want to make a request for this test, so just callback | 92 // We don't actually want to make a request for this test, so just callback |
| 93 // as if it completed. | 93 // as if it completed. |
| 94 test_->io_thread_loop()->PostTask(FROM_HERE, | 94 test_->io_thread_loop()->PostTask(FROM_HERE, |
| 95 NewRunnableMethod(this, &ShuntedHttpBridge::CallOnURLFetchComplete)); | 95 NewRunnableMethod(this, &ShuntedHttpBridge::CallOnURLFetchComplete)); |
| 96 } | 96 } |
| 97 private: | 97 private: |
| 98 void CallOnURLFetchComplete() { | 98 void CallOnURLFetchComplete() { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 117 http_bridge->GetRequestContextGetter()-> | 117 http_bridge->GetRequestContextGetter()-> |
| 118 GetURLRequestContext()-> | 118 GetURLRequestContext()-> |
| 119 http_transaction_factory()->GetSession()); | 119 http_transaction_factory()->GetSession()); |
| 120 } | 120 } |
| 121 | 121 |
| 122 // Test the HttpBridge without actually making any network requests. | 122 // Test the HttpBridge without actually making any network requests. |
| 123 TEST_F(HttpBridgeTest, TestMakeSynchronousPostShunted) { | 123 TEST_F(HttpBridgeTest, TestMakeSynchronousPostShunted) { |
| 124 scoped_refptr<URLRequestContextGetter> ctx_getter( | 124 scoped_refptr<URLRequestContextGetter> ctx_getter( |
| 125 new TestURLRequestContextGetter()); | 125 new TestURLRequestContextGetter()); |
| 126 scoped_refptr<HttpBridge> http_bridge(new ShuntedHttpBridge( | 126 scoped_refptr<HttpBridge> http_bridge(new ShuntedHttpBridge( |
| 127 ctx_getter, io_thread_loop(), this)); | 127 ctx_getter, this)); |
| 128 http_bridge->SetUserAgent("bob"); | 128 http_bridge->SetUserAgent("bob"); |
| 129 http_bridge->SetURL("http://www.google.com", 9999); | 129 http_bridge->SetURL("http://www.google.com", 9999); |
| 130 http_bridge->SetPostPayload("text/plain", 2, " "); | 130 http_bridge->SetPostPayload("text/plain", 2, " "); |
| 131 | 131 |
| 132 int os_error = 0; | 132 int os_error = 0; |
| 133 int response_code = 0; | 133 int response_code = 0; |
| 134 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 134 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
| 135 EXPECT_TRUE(success); | 135 EXPECT_TRUE(success); |
| 136 EXPECT_EQ(200, response_code); | 136 EXPECT_EQ(200, response_code); |
| 137 EXPECT_EQ(0, os_error); | 137 EXPECT_EQ(0, os_error); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 EXPECT_EQ(0, os_error); | 219 EXPECT_EQ(0, os_error); |
| 220 | 220 |
| 221 std::string response(http_bridge->GetResponseContent(), | 221 std::string response(http_bridge->GetResponseContent(), |
| 222 http_bridge->GetResponseContentLength()); | 222 http_bridge->GetResponseContentLength()); |
| 223 | 223 |
| 224 EXPECT_NE(std::string::npos, response.find("fnord")); | 224 EXPECT_NE(std::string::npos, response.find("fnord")); |
| 225 EXPECT_NE(std::string::npos, response.find(test_payload.c_str())); | 225 EXPECT_NE(std::string::npos, response.find(test_payload.c_str())); |
| 226 } | 226 } |
| 227 | 227 |
| 228 #endif // defined(BROWSER_SYNC) | 228 #endif // defined(BROWSER_SYNC) |
| OLD | NEW |