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_listen_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 // net::HttpServer doesn't allow us to close connection during callback.
96 base::MessageLoop::current()->PostTask(
98 base::Bind(&net::HttpServer::Close
, server_
, connection_id
));
103 void TestHttpServer::OnWebSocketMessage(int connection_id
,
104 const std::string
& data
) {
105 WebSocketMessageAction action
;
107 base::AutoLock
lock(action_lock_
);
108 action
= message_action_
;
112 server_
->SendOverWebSocket(connection_id
, data
);
114 case kCloseOnMessage
:
115 // net::HttpServer doesn't allow us to close connection during callback.
116 base::MessageLoop::current()->PostTask(
118 base::Bind(&net::HttpServer::Close
, server_
, connection_id
));
123 void TestHttpServer::OnClose(int connection_id
) {
124 connections_
.erase(connection_id
);
125 if (connections_
.empty())
126 all_closed_event_
.Signal();
129 void TestHttpServer::StartOnServerThread(bool* success
,
130 base::WaitableEvent
* event
) {
131 net::TCPListenSocketFactory
factory("127.0.0.1", 0);
132 server_
= new net::HttpServer(factory
, this);
134 net::IPEndPoint address
;
135 int error
= server_
->GetLocalAddress(&address
);
136 EXPECT_EQ(net::OK
, error
);
137 if (error
== net::OK
) {
138 base::AutoLock
lock(url_lock_
);
139 web_socket_url_
= GURL(base::StringPrintf("ws://127.0.0.1:%d",
144 *success
= server_
.get();
148 void TestHttpServer::StopOnServerThread(base::WaitableEvent
* event
) {