| 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 "jingle/notifier/base/proxy_resolving_client_socket.h" | 5 #include "jingle/notifier/base/proxy_resolving_client_socket.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/compiler_specific.h" |
| 8 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 9 #include "net/base/mock_host_resolver.h" | 10 #include "net/base/mock_host_resolver.h" |
| 10 #include "net/base/test_completion_callback.h" | 11 #include "net/base/test_completion_callback.h" |
| 11 #include "net/socket/socket_test_util.h" | 12 #include "net/socket/socket_test_util.h" |
| 12 #include "net/url_request/url_request_context_getter.h" | 13 #include "net/url_request/url_request_context_getter.h" |
| 13 #include "net/url_request/url_request_test_util.h" | 14 #include "net/url_request/url_request_test_util.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 // TODO(sanjeevr): Move this to net_test_support. | 18 |
| 18 // Used to return a dummy context. | 19 class ProxyTestURLRequestContextGetter : public TestURLRequestContextGetter { |
| 19 class TestURLRequestContextGetter : public net::URLRequestContextGetter { | |
| 20 public: | 20 public: |
| 21 TestURLRequestContextGetter() | 21 ProxyTestURLRequestContextGetter() |
| 22 : message_loop_proxy_(base::MessageLoopProxy::current()) { | 22 : TestURLRequestContextGetter(base::MessageLoopProxy::current()), |
| 23 } | 23 set_context_members_(false) {} |
| 24 virtual ~TestURLRequestContextGetter() { } | |
| 25 | 24 |
| 26 // net::URLRequestContextGetter: | 25 // Override GetURLRequestContext to set the host resolver and proxy |
| 27 virtual net::URLRequestContext* GetURLRequestContext() { | 26 // service (used by the unit tests). |
| 28 if (!context_) | 27 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE { |
| 29 CreateURLRequestContext(); | 28 TestURLRequestContext* context = |
| 30 return context_.get(); | 29 TestURLRequestContextGetter::GetURLRequestContext(); |
| 31 } | 30 if (!set_context_members_) { |
| 32 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const { | 31 context->set_host_resolver(new net::MockHostResolver()); |
| 33 return message_loop_proxy_; | 32 context->set_proxy_service(net::ProxyService::CreateFixedFromPacResult( |
| 33 "PROXY bad:99; PROXY maybe:80; DIRECT")); |
| 34 set_context_members_ = true; |
| 35 } |
| 36 return context; |
| 34 } | 37 } |
| 35 | 38 |
| 39 protected: |
| 40 virtual ~ProxyTestURLRequestContextGetter() {} |
| 41 |
| 36 private: | 42 private: |
| 37 void CreateURLRequestContext() { | 43 bool set_context_members_; |
| 38 context_ = new TestURLRequestContext(); | 44 }; |
| 39 context_->set_host_resolver(new net::MockHostResolver()); | |
| 40 context_->set_proxy_service(net::ProxyService::CreateFixedFromPacResult( | |
| 41 "PROXY bad:99; PROXY maybe:80; DIRECT")); | |
| 42 } | |
| 43 | 45 |
| 44 scoped_refptr<net::URLRequestContext> context_; | |
| 45 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | |
| 46 }; | |
| 47 } // namespace | 46 } // namespace |
| 48 | 47 |
| 49 namespace notifier { | 48 namespace notifier { |
| 50 | 49 |
| 51 class ProxyResolvingClientSocketTest : public testing::Test { | 50 class ProxyResolvingClientSocketTest : public testing::Test { |
| 52 protected: | 51 protected: |
| 53 ProxyResolvingClientSocketTest() | 52 ProxyResolvingClientSocketTest() |
| 54 : url_request_context_getter_(new TestURLRequestContextGetter()) {} | 53 : url_request_context_getter_(new ProxyTestURLRequestContextGetter()) {} |
| 55 | 54 |
| 56 virtual ~ProxyResolvingClientSocketTest() {} | 55 virtual ~ProxyResolvingClientSocketTest() {} |
| 57 | 56 |
| 58 virtual void TearDown() { | 57 virtual void TearDown() { |
| 59 // Clear out any messages posted by ProxyResolvingClientSocket's | 58 // Clear out any messages posted by ProxyResolvingClientSocket's |
| 60 // destructor. | 59 // destructor. |
| 61 message_loop_.RunAllPending(); | 60 message_loop_.RunAllPending(); |
| 62 } | 61 } |
| 63 | 62 |
| 64 // Needed by XmppConnection. | 63 MessageLoop message_loop_; |
| 65 MessageLoopForIO message_loop_; | 64 scoped_refptr<ProxyTestURLRequestContextGetter> url_request_context_getter_; |
| 66 scoped_refptr<TestURLRequestContextGetter> url_request_context_getter_; | |
| 67 }; | 65 }; |
| 68 | 66 |
| 69 // TODO(sanjeevr): Fix this test on Linux. | 67 // TODO(sanjeevr): Fix this test on Linux. |
| 70 TEST_F(ProxyResolvingClientSocketTest, DISABLED_ConnectError) { | 68 TEST_F(ProxyResolvingClientSocketTest, DISABLED_ConnectError) { |
| 71 net::HostPortPair dest("0.0.0.0", 0); | 69 net::HostPortPair dest("0.0.0.0", 0); |
| 72 ProxyResolvingClientSocket proxy_resolving_socket( | 70 ProxyResolvingClientSocket proxy_resolving_socket( |
| 73 NULL, | 71 NULL, |
| 74 url_request_context_getter_, | 72 url_request_context_getter_, |
| 75 net::SSLConfig(), | 73 net::SSLConfig(), |
| 76 dest); | 74 dest); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 const net::ProxyRetryInfoMap& retry_info = | 121 const net::ProxyRetryInfoMap& retry_info = |
| 124 context->proxy_service()->proxy_retry_info(); | 122 context->proxy_service()->proxy_retry_info(); |
| 125 | 123 |
| 126 EXPECT_EQ(1u, retry_info.size()); | 124 EXPECT_EQ(1u, retry_info.size()); |
| 127 net::ProxyRetryInfoMap::const_iterator iter = retry_info.find("bad:99"); | 125 net::ProxyRetryInfoMap::const_iterator iter = retry_info.find("bad:99"); |
| 128 EXPECT_TRUE(iter != retry_info.end()); | 126 EXPECT_TRUE(iter != retry_info.end()); |
| 129 } | 127 } |
| 130 | 128 |
| 131 // TODO(sanjeevr): Add more unit-tests. | 129 // TODO(sanjeevr): Add more unit-tests. |
| 132 } // namespace notifier | 130 } // namespace notifier |
| OLD | NEW |