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
|
// 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 "remoting/host/url_fetcher.h"
#include "base/message_loop.h"
#include "base/threading/thread.h"
#include "net/test/test_server.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_status.h"
#include "remoting/host/url_request_context.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace remoting {
class UrlFetcherTest : public testing::Test {
public:
UrlFetcherTest()
: test_server_(
net::TestServer::TYPE_HTTPS,
net::TestServer::kLocalhost,
FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))),
io_thread_("TestIOThread"),
file_thread_("TestFileThread") {
}
protected:
void SetUp() OVERRIDE {
ASSERT_TRUE(io_thread_.StartWithOptions(
base::Thread::Options(MessageLoop::TYPE_IO, 0)));
ASSERT_TRUE(file_thread_.StartWithOptions(
base::Thread::Options(MessageLoop::TYPE_IO, 0)));
context_getter_ = new URLRequestContextGetter(
message_loop_.message_loop_proxy(),
io_thread_.message_loop_proxy(),
static_cast<MessageLoopForIO*>(file_thread_.message_loop()));
ASSERT_TRUE(test_server_.Start());
}
protected:
void OnDone(const net::URLRequestStatus& status,
int response_code,
const std::string& response) {
ASSERT_EQ(MessageLoop::current(), &message_loop_);
status_ = status;
response_code_ = response_code;
response_ = response;
message_loop_.PostTask(FROM_HERE, MessageLoop::QuitClosure());
}
net::TestServer test_server_;
MessageLoopForUI message_loop_;
base::Thread io_thread_;
base::Thread file_thread_;
scoped_refptr<URLRequestContextGetter> context_getter_;
net::URLRequestStatus status_;
std::string response_;
int response_code_;
};
TEST_F(UrlFetcherTest, TestGet) {
UrlFetcher fetcher(test_server_.GetURL("default"), UrlFetcher::GET);
fetcher.SetRequestContext(context_getter_);
fetcher.Start(base::Bind(&UrlFetcherTest_TestGet_Test::OnDone,
base::Unretained(this)));
message_loop_.Run();
EXPECT_EQ(net::URLRequestStatus::SUCCESS, status_.status());
EXPECT_EQ("Default response given for path: /default", response_);
EXPECT_EQ(200, response_code_);
}
TEST_F(UrlFetcherTest, TestPost) {
const char kTestQueryData[] = "123qwe123qwe";
UrlFetcher fetcher(test_server_.GetURL("echo"), UrlFetcher::POST);
fetcher.SetRequestContext(context_getter_);
fetcher.SetUploadData("text/html", kTestQueryData);
fetcher.Start(base::Bind(&UrlFetcherTest_TestPost_Test::OnDone,
base::Unretained(this)));
message_loop_.Run();
EXPECT_EQ(net::URLRequestStatus::SUCCESS, status_.status());
EXPECT_EQ(kTestQueryData, response_);
EXPECT_EQ(200, response_code_);
}
TEST_F(UrlFetcherTest, TestFailed) {
UrlFetcher fetcher(test_server_.GetURL("auth-basic"), UrlFetcher::GET);
fetcher.SetRequestContext(context_getter_);
fetcher.Start(base::Bind(&UrlFetcherTest_TestFailed_Test::OnDone,
base::Unretained(this)));
message_loop_.Run();
EXPECT_EQ(net::URLRequestStatus::SUCCESS, status_.status());
EXPECT_EQ(401, response_code_);
}
} // namespace remoting
|