Update V8 to version 4.5.86.
[chromium-blink-merge.git] / google_apis / drive / base_requests_server_unittest.cc
blobe8da79dfd6e05f93b30718d0089fb1077e1e9b99
1 // Copyright (c) 2013 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 "google_apis/drive/base_requests.h"
7 #include "base/bind.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "google_apis/drive/dummy_auth_service.h"
14 #include "google_apis/drive/request_sender.h"
15 #include "google_apis/drive/task_util.h"
16 #include "google_apis/drive/test_util.h"
17 #include "net/test/embedded_test_server/embedded_test_server.h"
18 #include "net/test/embedded_test_server/http_request.h"
19 #include "net/test/embedded_test_server/http_response.h"
20 #include "net/url_request/url_request_test_util.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 namespace google_apis {
25 namespace {
27 const char kTestUserAgent[] = "test-user-agent";
29 } // namespace
31 class BaseRequestsServerTest : public testing::Test {
32 protected:
33 BaseRequestsServerTest() {
36 void SetUp() override {
37 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
39 request_context_getter_ = new net::TestURLRequestContextGetter(
40 message_loop_.task_runner());
42 request_sender_.reset(new RequestSender(
43 new DummyAuthService,
44 request_context_getter_.get(),
45 message_loop_.task_runner(),
46 kTestUserAgent));
48 ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady());
49 test_server_.RegisterRequestHandler(
50 base::Bind(&test_util::HandleDownloadFileRequest,
51 test_server_.base_url(),
52 base::Unretained(&http_request_)));
55 // Returns a temporary file path suitable for storing the cache file.
56 base::FilePath GetTestCachedFilePath(const base::FilePath& file_name) {
57 return temp_dir_.path().Append(file_name);
60 base::MessageLoopForIO message_loop_; // Test server needs IO thread.
61 net::test_server::EmbeddedTestServer test_server_;
62 scoped_ptr<RequestSender> request_sender_;
63 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
64 base::ScopedTempDir temp_dir_;
66 // The incoming HTTP request is saved so tests can verify the request
67 // parameters like HTTP method (ex. some requests should use DELETE
68 // instead of GET).
69 net::test_server::HttpRequest http_request_;
72 TEST_F(BaseRequestsServerTest, DownloadFileRequest_ValidFile) {
73 DriveApiErrorCode result_code = DRIVE_OTHER_ERROR;
74 base::FilePath temp_file;
76 base::RunLoop run_loop;
77 DownloadFileRequestBase* request = new DownloadFileRequestBase(
78 request_sender_.get(),
79 test_util::CreateQuitCallback(
80 &run_loop,
81 test_util::CreateCopyResultCallback(&result_code, &temp_file)),
82 GetContentCallback(),
83 ProgressCallback(),
84 test_server_.GetURL("/files/drive/testfile.txt"),
85 GetTestCachedFilePath(
86 base::FilePath::FromUTF8Unsafe("cached_testfile.txt")));
87 request_sender_->StartRequestWithAuthRetry(request);
88 run_loop.Run();
91 std::string contents;
92 base::ReadFileToString(temp_file, &contents);
93 base::DeleteFile(temp_file, false);
95 EXPECT_EQ(HTTP_SUCCESS, result_code);
96 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method);
97 EXPECT_EQ("/files/drive/testfile.txt", http_request_.relative_url);
99 const base::FilePath expected_path =
100 test_util::GetTestFilePath("drive/testfile.txt");
101 std::string expected_contents;
102 base::ReadFileToString(expected_path, &expected_contents);
103 EXPECT_EQ(expected_contents, contents);
106 TEST_F(BaseRequestsServerTest, DownloadFileRequest_NonExistentFile) {
107 DriveApiErrorCode result_code = DRIVE_OTHER_ERROR;
108 base::FilePath temp_file;
110 base::RunLoop run_loop;
111 DownloadFileRequestBase* request = new DownloadFileRequestBase(
112 request_sender_.get(),
113 test_util::CreateQuitCallback(
114 &run_loop,
115 test_util::CreateCopyResultCallback(&result_code, &temp_file)),
116 GetContentCallback(),
117 ProgressCallback(),
118 test_server_.GetURL("/files/gdata/no-such-file.txt"),
119 GetTestCachedFilePath(
120 base::FilePath::FromUTF8Unsafe("cache_no-such-file.txt")));
121 request_sender_->StartRequestWithAuthRetry(request);
122 run_loop.Run();
124 EXPECT_EQ(HTTP_NOT_FOUND, result_code);
125 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method);
126 EXPECT_EQ("/files/gdata/no-such-file.txt",
127 http_request_.relative_url);
128 // Do not verify the not found message.
131 } // namespace google_apis