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
3 // found in the LICENSE file.
5 #include "chrome/test/chromedriver/net/test_http_server.h"
8 #include "base/location.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/time/time.h"
13 #include "net/base/ip_endpoint.h"
14 #include "net/base/net_errors.h"
15 #include "net/server/http_server_request_info.h"
16 #include "net/socket/tcp_server_socket.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 TestHttpServer::TestHttpServer()
20 : thread_("ServerThread"),
21 all_closed_event_(false, true),
22 request_action_(kAccept
),
23 message_action_(kEchoMessage
) {
26 TestHttpServer::~TestHttpServer() {
29 bool TestHttpServer::Start() {
30 base::Thread::Options
options(base::MessageLoop::TYPE_IO
, 0);
31 bool thread_started
= thread_
.StartWithOptions(options
);
32 EXPECT_TRUE(thread_started
);
36 base::WaitableEvent
event(false, false);
37 thread_
.message_loop_proxy()->PostTask(
39 base::Bind(&TestHttpServer::StartOnServerThread
,
40 base::Unretained(this), &success
, &event
));
45 void TestHttpServer::Stop() {
46 if (!thread_
.IsRunning())
48 base::WaitableEvent
event(false, false);
49 thread_
.message_loop_proxy()->PostTask(
51 base::Bind(&TestHttpServer::StopOnServerThread
,
52 base::Unretained(this), &event
));
57 bool TestHttpServer::WaitForConnectionsToClose() {
58 return all_closed_event_
.TimedWait(base::TimeDelta::FromSeconds(10));
61 void TestHttpServer::SetRequestAction(WebSocketRequestAction action
) {
62 base::AutoLock
lock(action_lock_
);
63 request_action_
= action
;
66 void TestHttpServer::SetMessageAction(WebSocketMessageAction action
) {
67 base::AutoLock
lock(action_lock_
);
68 message_action_
= action
;
71 GURL
TestHttpServer::web_socket_url() const {
72 base::AutoLock
lock(url_lock_
);
73 return web_socket_url_
;
76 void TestHttpServer::OnWebSocketRequest(
78 const net::HttpServerRequestInfo
& info
) {
79 WebSocketRequestAction action
;
81 base::AutoLock
lock(action_lock_
);
82 action
= request_action_
;
84 connections_
.insert(connection_id
);
85 all_closed_event_
.Reset();
89 server_
->AcceptWebSocket(connection_id
, info
);
92 server_
->Send404(connection_id
);
95 server_
->Close(connection_id
);
100 void TestHttpServer::OnWebSocketMessage(int connection_id
,
101 const std::string
& data
) {
102 WebSocketMessageAction action
;
104 base::AutoLock
lock(action_lock_
);
105 action
= message_action_
;
109 server_
->SendOverWebSocket(connection_id
, data
);
111 case kCloseOnMessage
:
112 server_
->Close(connection_id
);
117 void TestHttpServer::OnClose(int connection_id
) {
118 connections_
.erase(connection_id
);
119 if (connections_
.empty())
120 all_closed_event_
.Signal();
123 void TestHttpServer::StartOnServerThread(bool* success
,
124 base::WaitableEvent
* event
) {
125 scoped_ptr
<net::ServerSocket
> server_socket(
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));
130 net::IPEndPoint address
;
131 int error
= server_
->GetLocalAddress(&address
);
132 EXPECT_EQ(net::OK
, error
);
133 if (error
== net::OK
) {
134 base::AutoLock
lock(url_lock_
);
135 web_socket_url_
= GURL(base::StringPrintf("ws://127.0.0.1:%d",
140 *success
= server_
.get();
144 void TestHttpServer::StopOnServerThread(base::WaitableEvent
* event
) {