1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/location.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop/message_loop.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread.h"
#include "chrome/test/chromedriver/net/net_util.h"
#include "chrome/test/chromedriver/net/url_request_context_getter.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
#include "net/server/http_server.h"
#include "net/server/http_server_request_info.h"
#include "net/socket/tcp_server_socket.h"
#include "net/url_request/url_request_context_getter.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class FetchUrlTest : public testing::Test,
public net::HttpServer::Delegate {
public:
FetchUrlTest()
: io_thread_("io"),
response_(kSendHello) {
base::Thread::Options options(base::MessageLoop::TYPE_IO, 0);
CHECK(io_thread_.StartWithOptions(options));
context_getter_ = new URLRequestContextGetter(
io_thread_.message_loop_proxy());
base::WaitableEvent event(false, false);
io_thread_.message_loop_proxy()->PostTask(
FROM_HERE,
base::Bind(&FetchUrlTest::InitOnIO,
base::Unretained(this), &event));
event.Wait();
}
~FetchUrlTest() override {
base::WaitableEvent event(false, false);
io_thread_.message_loop_proxy()->PostTask(
FROM_HERE,
base::Bind(&FetchUrlTest::DestroyServerOnIO,
base::Unretained(this), &event));
event.Wait();
}
void InitOnIO(base::WaitableEvent* event) {
scoped_ptr<net::ServerSocket> server_socket(
new net::TCPServerSocket(NULL, net::NetLog::Source()));
server_socket->ListenWithAddressAndPort("127.0.0.1", 0, 1);
server_.reset(new net::HttpServer(server_socket.Pass(), this));
net::IPEndPoint address;
CHECK_EQ(net::OK, server_->GetLocalAddress(&address));
server_url_ = base::StringPrintf("http://127.0.0.1:%d", address.port());
event->Signal();
}
void DestroyServerOnIO(base::WaitableEvent* event) {
server_.reset(NULL);
event->Signal();
}
// Overridden from net::HttpServer::Delegate:
void OnConnect(int connection_id) override {}
void OnHttpRequest(int connection_id,
const net::HttpServerRequestInfo& info) override {
switch (response_) {
case kSendHello:
server_->Send200(connection_id, "hello", "text/plain");
break;
case kSend404:
server_->Send404(connection_id);
break;
case kClose:
server_->Close(connection_id);
break;
default:
break;
}
}
void OnWebSocketRequest(int connection_id,
const net::HttpServerRequestInfo& info) override {}
void OnWebSocketMessage(int connection_id, const std::string& data) override {
}
void OnClose(int connection_id) override {}
protected:
enum ServerResponse {
kSendHello = 0,
kSend404,
kClose,
};
base::Thread io_thread_;
ServerResponse response_;
scoped_ptr<net::HttpServer> server_;
scoped_refptr<URLRequestContextGetter> context_getter_;
std::string server_url_;
};
} // namespace
TEST_F(FetchUrlTest, Http200) {
std::string response("stuff");
ASSERT_TRUE(FetchUrl(server_url_, context_getter_.get(), &response));
ASSERT_STREQ("hello", response.c_str());
}
TEST_F(FetchUrlTest, HttpNon200) {
response_ = kSend404;
std::string response("stuff");
ASSERT_FALSE(FetchUrl(server_url_, context_getter_.get(), &response));
ASSERT_STREQ("stuff", response.c_str());
}
TEST_F(FetchUrlTest, ConnectionClose) {
response_ = kClose;
std::string response("stuff");
ASSERT_FALSE(FetchUrl(server_url_, context_getter_.get(), &response));
ASSERT_STREQ("stuff", response.c_str());
}
TEST_F(FetchUrlTest, NoServer) {
std::string response("stuff");
ASSERT_FALSE(
FetchUrl("http://localhost:33333", context_getter_.get(), &response));
ASSERT_STREQ("stuff", response.c_str());
}
|