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.
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"
28 class FetchUrlTest
: public testing::Test
,
29 public net::HttpServer::Delegate
{
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(
40 base::Bind(&FetchUrlTest::InitOnIO
, base::Unretained(this), &event
));
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
));
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());
63 void DestroyServerOnIO(base::WaitableEvent
* event
) {
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
{
75 server_
->Send200(connection_id
, "hello", "text/plain");
78 server_
->Send404(connection_id
);
81 server_
->Close(connection_id
);
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
{}
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_
;
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
) {
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");
133 FetchUrl("http://localhost:33333", context_getter_
.get(), &response
));
134 ASSERT_STREQ("stuff", response
.c_str());