Update CrOS OOBE throbber to MD throbber; delete old asset
[chromium-blink-merge.git] / chrome / test / chromedriver / net / net_util_unittest.cc
blobb976373233952510bae80563ede45b3c036634a1
1 // Copyright (c) 2012 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 <string>
7 #include "base/bind.h"
8 #include "base/compiler_specific.h"
9 #include "base/location.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "base/threading/thread.h"
16 #include "chrome/test/chromedriver/net/net_util.h"
17 #include "chrome/test/chromedriver/net/url_request_context_getter.h"
18 #include "net/base/ip_endpoint.h"
19 #include "net/base/net_errors.h"
20 #include "net/server/http_server.h"
21 #include "net/server/http_server_request_info.h"
22 #include "net/socket/tcp_server_socket.h"
23 #include "net/url_request/url_request_context_getter.h"
24 #include "testing/gtest/include/gtest/gtest.h"
26 namespace {
28 class FetchUrlTest : public testing::Test,
29 public net::HttpServer::Delegate {
30 public:
31 FetchUrlTest()
32 : io_thread_("io"),
33 response_(kSendHello) {
34 base::Thread::Options options(base::MessageLoop::TYPE_IO, 0);
35 CHECK(io_thread_.StartWithOptions(options));
36 context_getter_ = new URLRequestContextGetter(io_thread_.task_runner());
37 base::WaitableEvent event(false, false);
38 io_thread_.task_runner()->PostTask(
39 FROM_HERE,
40 base::Bind(&FetchUrlTest::InitOnIO, base::Unretained(this), &event));
41 event.Wait();
44 ~FetchUrlTest() override {
45 base::WaitableEvent event(false, false);
46 io_thread_.task_runner()->PostTask(
47 FROM_HERE, base::Bind(&FetchUrlTest::DestroyServerOnIO,
48 base::Unretained(this), &event));
49 event.Wait();
52 void InitOnIO(base::WaitableEvent* event) {
53 scoped_ptr<net::ServerSocket> server_socket(
54 new net::TCPServerSocket(NULL, net::NetLog::Source()));
55 server_socket->ListenWithAddressAndPort("127.0.0.1", 0, 1);
56 server_.reset(new net::HttpServer(server_socket.Pass(), this));
57 net::IPEndPoint address;
58 CHECK_EQ(net::OK, server_->GetLocalAddress(&address));
59 server_url_ = base::StringPrintf("http://127.0.0.1:%d", address.port());
60 event->Signal();
63 void DestroyServerOnIO(base::WaitableEvent* event) {
64 server_.reset(NULL);
65 event->Signal();
68 // Overridden from net::HttpServer::Delegate:
69 void OnConnect(int connection_id) override {}
71 void OnHttpRequest(int connection_id,
72 const net::HttpServerRequestInfo& info) override {
73 switch (response_) {
74 case kSendHello:
75 server_->Send200(connection_id, "hello", "text/plain");
76 break;
77 case kSend404:
78 server_->Send404(connection_id);
79 break;
80 case kClose:
81 server_->Close(connection_id);
82 break;
83 default:
84 break;
88 void OnWebSocketRequest(int connection_id,
89 const net::HttpServerRequestInfo& info) override {}
90 void OnWebSocketMessage(int connection_id, const std::string& data) override {
92 void OnClose(int connection_id) override {}
94 protected:
95 enum ServerResponse {
96 kSendHello = 0,
97 kSend404,
98 kClose,
101 base::Thread io_thread_;
102 ServerResponse response_;
103 scoped_ptr<net::HttpServer> server_;
104 scoped_refptr<URLRequestContextGetter> context_getter_;
105 std::string server_url_;
108 } // namespace
110 TEST_F(FetchUrlTest, Http200) {
111 std::string response("stuff");
112 ASSERT_TRUE(FetchUrl(server_url_, context_getter_.get(), &response));
113 ASSERT_STREQ("hello", response.c_str());
116 TEST_F(FetchUrlTest, HttpNon200) {
117 response_ = kSend404;
118 std::string response("stuff");
119 ASSERT_FALSE(FetchUrl(server_url_, context_getter_.get(), &response));
120 ASSERT_STREQ("stuff", response.c_str());
123 TEST_F(FetchUrlTest, ConnectionClose) {
124 response_ = kClose;
125 std::string response("stuff");
126 ASSERT_FALSE(FetchUrl(server_url_, context_getter_.get(), &response));
127 ASSERT_STREQ("stuff", response.c_str());
130 TEST_F(FetchUrlTest, NoServer) {
131 std::string response("stuff");
132 ASSERT_FALSE(
133 FetchUrl("http://localhost:33333", context_getter_.get(), &response));
134 ASSERT_STREQ("stuff", response.c_str());