Componentize AccountReconcilor.
[chromium-blink-merge.git] / chrome / test / chromedriver / net / test_http_server.cc
blob740a33d78e4409a67ed7c108867693049fb6c109
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/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);
33 if (!thread_started)
34 return false;
35 bool success;
36 base::WaitableEvent event(false, false);
37 thread_.message_loop_proxy()->PostTask(
38 FROM_HERE,
39 base::Bind(&TestHttpServer::StartOnServerThread,
40 base::Unretained(this), &success, &event));
41 event.Wait();
42 return success;
45 void TestHttpServer::Stop() {
46 if (!thread_.IsRunning())
47 return;
48 base::WaitableEvent event(false, false);
49 thread_.message_loop_proxy()->PostTask(
50 FROM_HERE,
51 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::OnWebSocketRequest(
77 int connection_id,
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();
87 switch (action) {
88 case kAccept:
89 server_->AcceptWebSocket(connection_id, info);
90 break;
91 case kNotFound:
92 server_->Send404(connection_id);
93 break;
94 case kClose:
95 // net::HttpServer doesn't allow us to close connection during callback.
96 base::MessageLoop::current()->PostTask(
97 FROM_HERE,
98 base::Bind(&net::HttpServer::Close, server_, connection_id));
99 break;
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_;
110 switch (action) {
111 case kEchoMessage:
112 server_->SendOverWebSocket(connection_id, data);
113 break;
114 case kCloseOnMessage:
115 // net::HttpServer doesn't allow us to close connection during callback.
116 base::MessageLoop::current()->PostTask(
117 FROM_HERE,
118 base::Bind(&net::HttpServer::Close, server_, connection_id));
119 break;
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",
140 address.port()));
141 } else {
142 server_ = NULL;
144 *success = server_.get();
145 event->Signal();
148 void TestHttpServer::StopOnServerThread(base::WaitableEvent* event) {
149 if (server_.get())
150 server_ = NULL;
151 event->Signal();