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" | |
13 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
14 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
15 #include "base/synchronization/waitable_event.h" | 14 #include "base/synchronization/waitable_event.h" |
16 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
17 #include "chrome/test/chromedriver/net/net_util.h" | 16 #include "chrome/test/chromedriver/net/net_util.h" |
18 #include "chrome/test/chromedriver/net/url_request_context_getter.h" | 17 #include "chrome/test/chromedriver/net/url_request_context_getter.h" |
19 #include "net/base/ip_endpoint.h" | 18 #include "net/base/ip_endpoint.h" |
20 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
21 #include "net/server/http_server.h" | 20 #include "net/server/http_server.h" |
22 #include "net/server/http_server_request_info.h" | 21 #include "net/server/http_server_request_info.h" |
23 #include "net/socket/tcp_server_socket.h" | 22 #include "net/socket/tcp_server_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"), |
34 response_(kSendHello) { | 33 response_(kSendHello) { |
35 base::Thread::Options options(base::MessageLoop::TYPE_IO, 0); | 34 base::Thread::Options options(base::MessageLoop::TYPE_IO, 0); |
36 CHECK(io_thread_.StartWithOptions(options)); | 35 CHECK(io_thread_.StartWithOptions(options)); |
37 context_getter_ = new URLRequestContextGetter( | 36 context_getter_ = new URLRequestContextGetter(io_thread_.task_runner()); |
38 io_thread_.message_loop_proxy()); | |
39 base::WaitableEvent event(false, false); | 37 base::WaitableEvent event(false, false); |
40 io_thread_.message_loop_proxy()->PostTask( | 38 io_thread_.task_runner()->PostTask( |
41 FROM_HERE, | 39 FROM_HERE, |
42 base::Bind(&FetchUrlTest::InitOnIO, | 40 base::Bind(&FetchUrlTest::InitOnIO, base::Unretained(this), &event)); |
43 base::Unretained(this), &event)); | |
44 event.Wait(); | 41 event.Wait(); |
45 } | 42 } |
46 | 43 |
47 ~FetchUrlTest() override { | 44 ~FetchUrlTest() override { |
48 base::WaitableEvent event(false, false); | 45 base::WaitableEvent event(false, false); |
49 io_thread_.message_loop_proxy()->PostTask( | 46 io_thread_.task_runner()->PostTask( |
50 FROM_HERE, | 47 FROM_HERE, base::Bind(&FetchUrlTest::DestroyServerOnIO, |
51 base::Bind(&FetchUrlTest::DestroyServerOnIO, | 48 base::Unretained(this), &event)); |
52 base::Unretained(this), &event)); | |
53 event.Wait(); | 49 event.Wait(); |
54 } | 50 } |
55 | 51 |
56 void InitOnIO(base::WaitableEvent* event) { | 52 void InitOnIO(base::WaitableEvent* event) { |
57 scoped_ptr<net::ServerSocket> server_socket( | 53 scoped_ptr<net::ServerSocket> server_socket( |
58 new net::TCPServerSocket(NULL, net::NetLog::Source())); | 54 new net::TCPServerSocket(NULL, net::NetLog::Source())); |
59 server_socket->ListenWithAddressAndPort("127.0.0.1", 0, 1); | 55 server_socket->ListenWithAddressAndPort("127.0.0.1", 0, 1); |
60 server_.reset(new net::HttpServer(server_socket.Pass(), this)); | 56 server_.reset(new net::HttpServer(server_socket.Pass(), this)); |
61 net::IPEndPoint address; | 57 net::IPEndPoint address; |
62 CHECK_EQ(net::OK, server_->GetLocalAddress(&address)); | 58 CHECK_EQ(net::OK, server_->GetLocalAddress(&address)); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 ASSERT_FALSE(FetchUrl(server_url_, context_getter_.get(), &response)); | 126 ASSERT_FALSE(FetchUrl(server_url_, context_getter_.get(), &response)); |
131 ASSERT_STREQ("stuff", response.c_str()); | 127 ASSERT_STREQ("stuff", response.c_str()); |
132 } | 128 } |
133 | 129 |
134 TEST_F(FetchUrlTest, NoServer) { | 130 TEST_F(FetchUrlTest, NoServer) { |
135 std::string response("stuff"); | 131 std::string response("stuff"); |
136 ASSERT_FALSE( | 132 ASSERT_FALSE( |
137 FetchUrl("http://localhost:33333", context_getter_.get(), &response)); | 133 FetchUrl("http://localhost:33333", context_getter_.get(), &response)); |
138 ASSERT_STREQ("stuff", response.c_str()); | 134 ASSERT_STREQ("stuff", response.c_str()); |
139 } | 135 } |
OLD | NEW |