| 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/message_loop/message_loop_proxy.h" | 12 #include "base/message_loop/message_loop_proxy.h" |
| 13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
| 16 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
| 17 #include "chrome/test/chromedriver/net/net_util.h" | 17 #include "chrome/test/chromedriver/net/net_util.h" |
| 18 #include "chrome/test/chromedriver/net/url_request_context_getter.h" | 18 #include "chrome/test/chromedriver/net/url_request_context_getter.h" |
| 19 #include "net/base/ip_endpoint.h" | 19 #include "net/base/ip_endpoint.h" |
| 20 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 21 #include "net/server/http_server.h" | 21 #include "net/server/http_server.h" |
| 22 #include "net/server/http_server_request_info.h" | 22 #include "net/server/http_server_request_info.h" |
| 23 #include "net/socket/tcp_listen_socket.h" | |
| 24 #include "net/url_request/url_request_context_getter.h" | 23 #include "net/url_request/url_request_context_getter.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 25 |
| 27 namespace { | 26 namespace { |
| 28 | 27 |
| 29 class FetchUrlTest : public testing::Test, | 28 class FetchUrlTest : public testing::Test, |
| 30 public net::HttpServer::Delegate { | 29 public net::HttpServer::Delegate { |
| 31 public: | 30 public: |
| 32 FetchUrlTest() | 31 FetchUrlTest() |
| 33 : io_thread_("io"), | 32 : io_thread_("io"), |
| (...skipping 13 matching lines...) Expand all Loading... |
| 47 virtual ~FetchUrlTest() { | 46 virtual ~FetchUrlTest() { |
| 48 base::WaitableEvent event(false, false); | 47 base::WaitableEvent event(false, false); |
| 49 io_thread_.message_loop_proxy()->PostTask( | 48 io_thread_.message_loop_proxy()->PostTask( |
| 50 FROM_HERE, | 49 FROM_HERE, |
| 51 base::Bind(&FetchUrlTest::DestroyServerOnIO, | 50 base::Bind(&FetchUrlTest::DestroyServerOnIO, |
| 52 base::Unretained(this), &event)); | 51 base::Unretained(this), &event)); |
| 53 event.Wait(); | 52 event.Wait(); |
| 54 } | 53 } |
| 55 | 54 |
| 56 void InitOnIO(base::WaitableEvent* event) { | 55 void InitOnIO(base::WaitableEvent* event) { |
| 57 net::TCPListenSocketFactory factory("127.0.0.1", 0); | 56 server_.reset(net::HttpServer::Create("127.0.0.1", 0, this)); |
| 58 server_ = new net::HttpServer(factory, this); | 57 DCHECK(server_); |
| 59 net::IPEndPoint address; | 58 net::IPEndPoint address; |
| 60 CHECK_EQ(net::OK, server_->GetLocalAddress(&address)); | 59 CHECK_EQ(net::OK, server_->GetLocalAddress(&address)); |
| 61 server_url_ = base::StringPrintf("http://127.0.0.1:%d", address.port()); | 60 server_url_ = base::StringPrintf("http://127.0.0.1:%d", address.port()); |
| 62 event->Signal(); | 61 event->Signal(); |
| 63 } | 62 } |
| 64 | 63 |
| 65 void DestroyServerOnIO(base::WaitableEvent* event) { | 64 void DestroyServerOnIO(base::WaitableEvent* event) { |
| 66 server_ = NULL; | 65 server_.reset(NULL); |
| 67 event->Signal(); | 66 event->Signal(); |
| 68 } | 67 } |
| 69 | 68 |
| 70 // Overridden from net::HttpServer::Delegate: | 69 // Overridden from net::HttpServer::Delegate: |
| 71 virtual void OnHttpRequest(int connection_id, | 70 virtual void OnHttpRequest(int connection_id, |
| 72 const net::HttpServerRequestInfo& info) OVERRIDE { | 71 const net::HttpServerRequestInfo& info) OVERRIDE { |
| 73 switch (response_) { | 72 switch (response_) { |
| 74 case kSendHello: | 73 case kSendHello: |
| 75 server_->Send200(connection_id, "hello", "text/plain"); | 74 server_->Send200(connection_id, "hello", "text/plain"); |
| 76 break; | 75 break; |
| 77 case kSend404: | 76 case kSend404: |
| 78 server_->Send404(connection_id); | 77 server_->Send404(connection_id); |
| 79 break; | 78 break; |
| 80 case kClose: | 79 case kClose: |
| 81 // net::HttpServer doesn't allow us to close connection during callback. | 80 // net::HttpServer doesn't allow us to close connection during callback. |
| 82 base::MessageLoop::current()->PostTask( | 81 base::MessageLoop::current()->PostTask( |
| 83 FROM_HERE, | 82 FROM_HERE, |
| 84 base::Bind(&net::HttpServer::Close, server_, connection_id)); | 83 base::Bind(&net::HttpServer::Close, server_->AsWeakPtr(), |
| 84 connection_id)); |
| 85 break; | 85 break; |
| 86 default: | 86 default: |
| 87 break; | 87 break; |
| 88 } | 88 } |
| 89 } | 89 } |
| 90 | 90 |
| 91 virtual void OnWebSocketRequest( | 91 virtual void OnWebSocketRequest( |
| 92 int connection_id, | 92 int connection_id, |
| 93 const net::HttpServerRequestInfo& info) OVERRIDE {} | 93 const net::HttpServerRequestInfo& info) OVERRIDE {} |
| 94 virtual void OnWebSocketMessage(int connection_id, | 94 virtual void OnWebSocketMessage(int connection_id, |
| 95 const std::string& data) OVERRIDE {} | 95 const std::string& data) OVERRIDE {} |
| 96 virtual void OnClose(int connection_id) OVERRIDE {} | 96 virtual void OnClose(int connection_id) OVERRIDE {} |
| 97 | 97 |
| 98 protected: | 98 protected: |
| 99 enum ServerResponse { | 99 enum ServerResponse { |
| 100 kSendHello = 0, | 100 kSendHello = 0, |
| 101 kSend404, | 101 kSend404, |
| 102 kClose, | 102 kClose, |
| 103 }; | 103 }; |
| 104 | 104 |
| 105 base::Thread io_thread_; | 105 base::Thread io_thread_; |
| 106 ServerResponse response_; | 106 ServerResponse response_; |
| 107 scoped_refptr<net::HttpServer> server_; | 107 scoped_ptr<net::HttpServer> server_; |
| 108 scoped_refptr<URLRequestContextGetter> context_getter_; | 108 scoped_refptr<URLRequestContextGetter> context_getter_; |
| 109 std::string server_url_; | 109 std::string server_url_; |
| 110 }; | 110 }; |
| 111 | 111 |
| 112 } // namespace | 112 } // namespace |
| 113 | 113 |
| 114 TEST_F(FetchUrlTest, Http200) { | 114 TEST_F(FetchUrlTest, Http200) { |
| 115 std::string response("stuff"); | 115 std::string response("stuff"); |
| 116 ASSERT_TRUE(FetchUrl(server_url_, context_getter_.get(), &response)); | 116 ASSERT_TRUE(FetchUrl(server_url_, context_getter_.get(), &response)); |
| 117 ASSERT_STREQ("hello", response.c_str()); | 117 ASSERT_STREQ("hello", response.c_str()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 130 ASSERT_FALSE(FetchUrl(server_url_, context_getter_.get(), &response)); | 130 ASSERT_FALSE(FetchUrl(server_url_, context_getter_.get(), &response)); |
| 131 ASSERT_STREQ("stuff", response.c_str()); | 131 ASSERT_STREQ("stuff", response.c_str()); |
| 132 } | 132 } |
| 133 | 133 |
| 134 TEST_F(FetchUrlTest, NoServer) { | 134 TEST_F(FetchUrlTest, NoServer) { |
| 135 std::string response("stuff"); | 135 std::string response("stuff"); |
| 136 ASSERT_FALSE( | 136 ASSERT_FALSE( |
| 137 FetchUrl("http://localhost:33333", context_getter_.get(), &response)); | 137 FetchUrl("http://localhost:33333", context_getter_.get(), &response)); |
| 138 ASSERT_STREQ("stuff", response.c_str()); | 138 ASSERT_STREQ("stuff", response.c_str()); |
| 139 } | 139 } |
| OLD | NEW |