Update CrOS OOBE throbber to MD throbber; delete old asset
[chromium-blink-merge.git] / chrome / test / chromedriver / net / test_http_server.cc
bloba3d8e38dcbadbd92c80d77b36457c3e61695ec02
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"
7 #include "base/bind.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);
35 if (!thread_started)
36 return false;
37 bool success;
38 base::WaitableEvent event(false, false);
39 thread_.task_runner()->PostTask(
40 FROM_HERE, base::Bind(&TestHttpServer::StartOnServerThread,
41 base::Unretained(this), &success, &event));
42 event.Wait();
43 return success;
46 void TestHttpServer::Stop() {
47 if (!thread_.IsRunning())
48 return;
49 base::WaitableEvent event(false, false);
50 thread_.task_runner()->PostTask(
51 FROM_HERE, base::Bind(&TestHttpServer::StopOnServerThread,
52 base::Unretained(this), &event));
53 event.Wait();
54 thread_.Stop();
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(
82 int connection_id,
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();
92 switch (action) {
93 case kAccept:
94 server_->AcceptWebSocket(connection_id, info);
95 break;
96 case kNotFound:
97 server_->Send404(connection_id);
98 break;
99 case kClose:
100 server_->Close(connection_id);
101 break;
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_;
112 switch (action) {
113 case kEchoMessage:
114 server_->SendOverWebSocket(connection_id, data);
115 break;
116 case kCloseOnMessage:
117 server_->Close(connection_id);
118 break;
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",
141 address.port()));
142 } else {
143 server_.reset(NULL);
145 *success = server_.get();
146 event->Signal();
149 void TestHttpServer::StopOnServerThread(base::WaitableEvent* event) {
150 server_.reset(NULL);
151 event->Signal();