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/single_thread_task_runner.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 const int kBufferSize
= 100 * 1024 * 1024; // 100 MB
21 TestHttpServer::TestHttpServer()
22 : thread_("ServerThread"),
23 all_closed_event_(false, true),
24 request_action_(kAccept
),
25 message_action_(kEchoMessage
) {
28 TestHttpServer::~TestHttpServer() {
31 bool TestHttpServer::Start() {
32 base::Thread::Options
options(base::MessageLoop::TYPE_IO
, 0);
33 bool thread_started
= thread_
.StartWithOptions(options
);
34 EXPECT_TRUE(thread_started
);
38 base::WaitableEvent
event(false, false);
39 thread_
.task_runner()->PostTask(
40 FROM_HERE
, base::Bind(&TestHttpServer::StartOnServerThread
,
41 base::Unretained(this), &success
, &event
));
46 void TestHttpServer::Stop() {
47 if (!thread_
.IsRunning())
49 base::WaitableEvent
event(false, false);
50 thread_
.task_runner()->PostTask(
51 FROM_HERE
, 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::OnConnect(int connection_id
) {
77 server_
->SetSendBufferSize(connection_id
, kBufferSize
);
78 server_
->SetReceiveBufferSize(connection_id
, kBufferSize
);
81 void TestHttpServer::OnWebSocketRequest(
83 const net::HttpServerRequestInfo
& info
) {
84 WebSocketRequestAction action
;
86 base::AutoLock
lock(action_lock_
);
87 action
= request_action_
;
89 connections_
.insert(connection_id
);
90 all_closed_event_
.Reset();
94 server_
->AcceptWebSocket(connection_id
, info
);
97 server_
->Send404(connection_id
);
100 server_
->Close(connection_id
);
105 void TestHttpServer::OnWebSocketMessage(int connection_id
,
106 const std::string
& data
) {
107 WebSocketMessageAction action
;
109 base::AutoLock
lock(action_lock_
);
110 action
= message_action_
;
114 server_
->SendOverWebSocket(connection_id
, data
);
116 case kCloseOnMessage
:
117 server_
->Close(connection_id
);
122 void TestHttpServer::OnClose(int connection_id
) {
123 connections_
.erase(connection_id
);
124 if (connections_
.empty())
125 all_closed_event_
.Signal();
128 void TestHttpServer::StartOnServerThread(bool* success
,
129 base::WaitableEvent
* event
) {
130 scoped_ptr
<net::ServerSocket
> server_socket(
131 new net::TCPServerSocket(NULL
, net::NetLog::Source()));
132 server_socket
->ListenWithAddressAndPort("127.0.0.1", 0, 1);
133 server_
.reset(new net::HttpServer(server_socket
.Pass(), this));
135 net::IPEndPoint address
;
136 int error
= server_
->GetLocalAddress(&address
);
137 EXPECT_EQ(net::OK
, error
);
138 if (error
== net::OK
) {
139 base::AutoLock
lock(url_lock_
);
140 web_socket_url_
= GURL(base::StringPrintf("ws://127.0.0.1:%d",
145 *success
= server_
.get();
149 void TestHttpServer::StopOnServerThread(base::WaitableEvent
* event
) {