OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "remoting/host/url_fetcher.h" |
| 6 |
| 7 #include "base/message_loop.h" |
| 8 #include "base/threading/thread.h" |
| 9 #include "net/test/test_server.h" |
| 10 #include "net/url_request/url_request.h" |
| 11 #include "net/url_request/url_request_status.h" |
| 12 #include "remoting/host/url_request_context.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace remoting { |
| 16 |
| 17 class UrlFetcherTest : public testing::Test { |
| 18 public: |
| 19 UrlFetcherTest() |
| 20 : test_server_( |
| 21 net::TestServer::TYPE_HTTPS, |
| 22 net::TestServer::kLocalhost, |
| 23 FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))), |
| 24 io_thread_("TestIOThread"), |
| 25 file_thread_("TestFileThread") { |
| 26 } |
| 27 |
| 28 protected: |
| 29 void SetUp() OVERRIDE { |
| 30 ASSERT_TRUE(io_thread_.StartWithOptions( |
| 31 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
| 32 ASSERT_TRUE(file_thread_.StartWithOptions( |
| 33 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
| 34 context_getter_ = new URLRequestContextGetter( |
| 35 message_loop_.message_loop_proxy(), |
| 36 io_thread_.message_loop_proxy(), |
| 37 static_cast<MessageLoopForIO*>(file_thread_.message_loop())); |
| 38 ASSERT_TRUE(test_server_.Start()); |
| 39 } |
| 40 |
| 41 protected: |
| 42 void OnDone(const net::URLRequestStatus& status, |
| 43 int response_code, |
| 44 const std::string& response) { |
| 45 ASSERT_EQ(MessageLoop::current(), &message_loop_); |
| 46 status_ = status; |
| 47 response_code_ = response_code; |
| 48 response_ = response; |
| 49 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 50 } |
| 51 |
| 52 net::TestServer test_server_; |
| 53 MessageLoopForUI message_loop_; |
| 54 base::Thread io_thread_; |
| 55 base::Thread file_thread_; |
| 56 scoped_refptr<URLRequestContextGetter> context_getter_; |
| 57 net::URLRequestStatus status_; |
| 58 std::string response_; |
| 59 int response_code_; |
| 60 }; |
| 61 |
| 62 TEST_F(UrlFetcherTest, TestGet) { |
| 63 UrlFetcher fetcher(test_server_.GetURL("default"), UrlFetcher::GET); |
| 64 fetcher.SetRequestContext(context_getter_); |
| 65 fetcher.Start(base::Bind(&UrlFetcherTest_TestGet_Test::OnDone, |
| 66 base::Unretained(this))); |
| 67 message_loop_.Run(); |
| 68 EXPECT_EQ(net::URLRequestStatus::SUCCESS, status_.status()); |
| 69 EXPECT_EQ("Default response given for path: /default", response_); |
| 70 EXPECT_EQ(200, response_code_); |
| 71 } |
| 72 |
| 73 TEST_F(UrlFetcherTest, TestPost) { |
| 74 const char kTestQueryData[] = "123qwe123qwe"; |
| 75 UrlFetcher fetcher(test_server_.GetURL("echo"), UrlFetcher::POST); |
| 76 fetcher.SetRequestContext(context_getter_); |
| 77 fetcher.SetUploadData("text/html", kTestQueryData); |
| 78 fetcher.Start(base::Bind(&UrlFetcherTest_TestPost_Test::OnDone, |
| 79 base::Unretained(this))); |
| 80 message_loop_.Run(); |
| 81 EXPECT_EQ(net::URLRequestStatus::SUCCESS, status_.status()); |
| 82 EXPECT_EQ(kTestQueryData, response_); |
| 83 EXPECT_EQ(200, response_code_); |
| 84 } |
| 85 |
| 86 TEST_F(UrlFetcherTest, TestFailed) { |
| 87 UrlFetcher fetcher(test_server_.GetURL("auth-basic"), UrlFetcher::GET); |
| 88 fetcher.SetRequestContext(context_getter_); |
| 89 fetcher.Start(base::Bind(&UrlFetcherTest_TestFailed_Test::OnDone, |
| 90 base::Unretained(this))); |
| 91 message_loop_.Run(); |
| 92 EXPECT_EQ(net::URLRequestStatus::SUCCESS, status_.status()); |
| 93 EXPECT_EQ(401, response_code_); |
| 94 } |
| 95 |
| 96 } // namespace remoting |
OLD | NEW |