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"
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
{
27 const char kTestUserAgent
[] = "test-user-agent";
31 class BaseRequestsServerTest
: public testing::Test
{
33 BaseRequestsServerTest() {
36 void SetUp() override
{
37 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
39 request_context_getter_
= new net::TestURLRequestContextGetter(
40 message_loop_
.message_loop_proxy());
42 request_sender_
.reset(new RequestSender(
44 request_context_getter_
.get(),
45 message_loop_
.message_loop_proxy(),
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
69 net::test_server::HttpRequest http_request_
;
72 TEST_F(BaseRequestsServerTest
, DownloadFileRequest_ValidFile
) {
73 GDataErrorCode result_code
= GDATA_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(
81 test_util::CreateCopyResultCallback(&result_code
, &temp_file
)),
84 test_server_
.GetURL("/files/drive/testfile.txt"),
85 GetTestCachedFilePath(
86 base::FilePath::FromUTF8Unsafe("cached_testfile.txt")));
87 request_sender_
->StartRequestWithRetry(request
);
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 GDataErrorCode result_code
= GDATA_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(
115 test_util::CreateCopyResultCallback(&result_code
, &temp_file
)),
116 GetContentCallback(),
118 test_server_
.GetURL("/files/gdata/no-such-file.txt"),
119 GetTestCachedFilePath(
120 base::FilePath::FromUTF8Unsafe("cache_no-such-file.txt")));
121 request_sender_
->StartRequestWithRetry(request
);
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