OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/test_http_server.h" | 5 #include "chrome/test/chromedriver/net/test_http_server.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "net/base/ip_endpoint.h" | 13 #include "net/base/ip_endpoint.h" |
14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
15 #include "net/server/http_server_request_info.h" | 15 #include "net/server/http_server_request_info.h" |
16 #include "net/socket/tcp_listen_socket.h" | 16 #include "net/socket/tcp_server_socket.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 TestHttpServer::TestHttpServer() | 19 TestHttpServer::TestHttpServer() |
20 : thread_("ServerThread"), | 20 : thread_("ServerThread"), |
21 all_closed_event_(false, true), | 21 all_closed_event_(false, true), |
22 request_action_(kAccept), | 22 request_action_(kAccept), |
23 message_action_(kEchoMessage) { | 23 message_action_(kEchoMessage) { |
24 } | 24 } |
25 | 25 |
26 TestHttpServer::~TestHttpServer() { | 26 TestHttpServer::~TestHttpServer() { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 all_closed_event_.Reset(); | 85 all_closed_event_.Reset(); |
86 | 86 |
87 switch (action) { | 87 switch (action) { |
88 case kAccept: | 88 case kAccept: |
89 server_->AcceptWebSocket(connection_id, info); | 89 server_->AcceptWebSocket(connection_id, info); |
90 break; | 90 break; |
91 case kNotFound: | 91 case kNotFound: |
92 server_->Send404(connection_id); | 92 server_->Send404(connection_id); |
93 break; | 93 break; |
94 case kClose: | 94 case kClose: |
95 // net::HttpServer doesn't allow us to close connection during callback. | 95 server_->Close(connection_id); |
96 base::MessageLoop::current()->PostTask( | |
97 FROM_HERE, | |
98 base::Bind(&net::HttpServer::Close, server_, connection_id)); | |
99 break; | 96 break; |
100 } | 97 } |
101 } | 98 } |
102 | 99 |
103 void TestHttpServer::OnWebSocketMessage(int connection_id, | 100 void TestHttpServer::OnWebSocketMessage(int connection_id, |
104 const std::string& data) { | 101 const std::string& data) { |
105 WebSocketMessageAction action; | 102 WebSocketMessageAction action; |
106 { | 103 { |
107 base::AutoLock lock(action_lock_); | 104 base::AutoLock lock(action_lock_); |
108 action = message_action_; | 105 action = message_action_; |
109 } | 106 } |
110 switch (action) { | 107 switch (action) { |
111 case kEchoMessage: | 108 case kEchoMessage: |
112 server_->SendOverWebSocket(connection_id, data); | 109 server_->SendOverWebSocket(connection_id, data); |
113 break; | 110 break; |
114 case kCloseOnMessage: | 111 case kCloseOnMessage: |
115 // net::HttpServer doesn't allow us to close connection during callback. | 112 server_->Close(connection_id); |
116 base::MessageLoop::current()->PostTask( | |
117 FROM_HERE, | |
118 base::Bind(&net::HttpServer::Close, server_, connection_id)); | |
119 break; | 113 break; |
120 } | 114 } |
121 } | 115 } |
122 | 116 |
123 void TestHttpServer::OnClose(int connection_id) { | 117 void TestHttpServer::OnClose(int connection_id) { |
124 connections_.erase(connection_id); | 118 connections_.erase(connection_id); |
125 if (connections_.empty()) | 119 if (connections_.empty()) |
126 all_closed_event_.Signal(); | 120 all_closed_event_.Signal(); |
127 } | 121 } |
128 | 122 |
129 void TestHttpServer::StartOnServerThread(bool* success, | 123 void TestHttpServer::StartOnServerThread(bool* success, |
130 base::WaitableEvent* event) { | 124 base::WaitableEvent* event) { |
131 net::TCPListenSocketFactory factory("127.0.0.1", 0); | 125 scoped_ptr<net::ServerSocket> server_socket( |
132 server_ = new net::HttpServer(factory, this); | 126 new net::TCPServerSocket(NULL, net::NetLog::Source())); |
| 127 server_socket->ListenWithAddressAndPort("127.0.0.1", 0, 1); |
| 128 server_.reset(new net::HttpServer(server_socket.Pass(), this)); |
133 | 129 |
134 net::IPEndPoint address; | 130 net::IPEndPoint address; |
135 int error = server_->GetLocalAddress(&address); | 131 int error = server_->GetLocalAddress(&address); |
136 EXPECT_EQ(net::OK, error); | 132 EXPECT_EQ(net::OK, error); |
137 if (error == net::OK) { | 133 if (error == net::OK) { |
138 base::AutoLock lock(url_lock_); | 134 base::AutoLock lock(url_lock_); |
139 web_socket_url_ = GURL(base::StringPrintf("ws://127.0.0.1:%d", | 135 web_socket_url_ = GURL(base::StringPrintf("ws://127.0.0.1:%d", |
140 address.port())); | 136 address.port())); |
141 } else { | 137 } else { |
142 server_ = NULL; | 138 server_.reset(NULL); |
143 } | 139 } |
144 *success = server_.get(); | 140 *success = server_.get(); |
145 event->Signal(); | 141 event->Signal(); |
146 } | 142 } |
147 | 143 |
148 void TestHttpServer::StopOnServerThread(base::WaitableEvent* event) { | 144 void TestHttpServer::StopOnServerThread(base::WaitableEvent* event) { |
149 if (server_.get()) | 145 server_.reset(NULL); |
150 server_ = NULL; | |
151 event->Signal(); | 146 event->Signal(); |
152 } | 147 } |
OLD | NEW |