| 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 "chrome/test/chromedriver/net/net_util.h" | 5 #include "chrome/test/chromedriver/net/net_util.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
| 16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 17 #include "base/synchronization/waitable_event.h" | 17 #include "base/synchronization/waitable_event.h" |
| 18 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
| 19 #include "chrome/test/chromedriver/net/url_request_context_getter.h" | 19 #include "chrome/test/chromedriver/net/url_request_context_getter.h" |
| 20 #include "net/base/ip_endpoint.h" | 20 #include "net/base/ip_endpoint.h" |
| 21 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
| 22 #include "net/log/net_log_source.h" |
| 22 #include "net/server/http_server.h" | 23 #include "net/server/http_server.h" |
| 23 #include "net/server/http_server_request_info.h" | 24 #include "net/server/http_server_request_info.h" |
| 24 #include "net/socket/tcp_server_socket.h" | 25 #include "net/socket/tcp_server_socket.h" |
| 25 #include "net/url_request/url_request_context_getter.h" | 26 #include "net/url_request/url_request_context_getter.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 28 |
| 28 namespace { | 29 namespace { |
| 29 | 30 |
| 30 class FetchUrlTest : public testing::Test, | 31 class FetchUrlTest : public testing::Test, |
| 31 public net::HttpServer::Delegate { | 32 public net::HttpServer::Delegate { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 48 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 49 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 49 base::WaitableEvent::InitialState::NOT_SIGNALED); | 50 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 50 io_thread_.task_runner()->PostTask( | 51 io_thread_.task_runner()->PostTask( |
| 51 FROM_HERE, base::Bind(&FetchUrlTest::DestroyServerOnIO, | 52 FROM_HERE, base::Bind(&FetchUrlTest::DestroyServerOnIO, |
| 52 base::Unretained(this), &event)); | 53 base::Unretained(this), &event)); |
| 53 event.Wait(); | 54 event.Wait(); |
| 54 } | 55 } |
| 55 | 56 |
| 56 void InitOnIO(base::WaitableEvent* event) { | 57 void InitOnIO(base::WaitableEvent* event) { |
| 57 std::unique_ptr<net::ServerSocket> server_socket( | 58 std::unique_ptr<net::ServerSocket> server_socket( |
| 58 new net::TCPServerSocket(NULL, net::NetLog::Source())); | 59 new net::TCPServerSocket(NULL, net::NetLogSource())); |
| 59 server_socket->ListenWithAddressAndPort("127.0.0.1", 0, 1); | 60 server_socket->ListenWithAddressAndPort("127.0.0.1", 0, 1); |
| 60 server_.reset(new net::HttpServer(std::move(server_socket), this)); | 61 server_.reset(new net::HttpServer(std::move(server_socket), this)); |
| 61 net::IPEndPoint address; | 62 net::IPEndPoint address; |
| 62 CHECK_EQ(net::OK, server_->GetLocalAddress(&address)); | 63 CHECK_EQ(net::OK, server_->GetLocalAddress(&address)); |
| 63 server_url_ = base::StringPrintf("http://127.0.0.1:%d", address.port()); | 64 server_url_ = base::StringPrintf("http://127.0.0.1:%d", address.port()); |
| 64 event->Signal(); | 65 event->Signal(); |
| 65 } | 66 } |
| 66 | 67 |
| 67 void DestroyServerOnIO(base::WaitableEvent* event) { | 68 void DestroyServerOnIO(base::WaitableEvent* event) { |
| 68 server_.reset(NULL); | 69 server_.reset(NULL); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 ASSERT_FALSE(FetchUrl(server_url_, context_getter_.get(), &response)); | 131 ASSERT_FALSE(FetchUrl(server_url_, context_getter_.get(), &response)); |
| 131 ASSERT_STREQ("stuff", response.c_str()); | 132 ASSERT_STREQ("stuff", response.c_str()); |
| 132 } | 133 } |
| 133 | 134 |
| 134 TEST_F(FetchUrlTest, NoServer) { | 135 TEST_F(FetchUrlTest, NoServer) { |
| 135 std::string response("stuff"); | 136 std::string response("stuff"); |
| 136 ASSERT_FALSE( | 137 ASSERT_FALSE( |
| 137 FetchUrl("http://localhost:33333", context_getter_.get(), &response)); | 138 FetchUrl("http://localhost:33333", context_getter_.get(), &response)); |
| 138 ASSERT_STREQ("stuff", response.c_str()); | 139 ASSERT_STREQ("stuff", response.c_str()); |
| 139 } | 140 } |
| OLD | NEW |