• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "google_apis/drive/base_requests.h"
6 
7 #include "base/bind.h"
8 #include "base/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"
22 
23 namespace google_apis {
24 
25 namespace {
26 
27 const char kTestUserAgent[] = "test-user-agent";
28 
29 }  // namespace
30 
31 class BaseRequestsServerTest : public testing::Test {
32  protected:
BaseRequestsServerTest()33   BaseRequestsServerTest() {
34   }
35 
SetUp()36   virtual void SetUp() OVERRIDE {
37     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
38 
39     request_context_getter_ = new net::TestURLRequestContextGetter(
40         message_loop_.message_loop_proxy());
41 
42     request_sender_.reset(new RequestSender(
43         new DummyAuthService,
44         request_context_getter_.get(),
45         message_loop_.message_loop_proxy(),
46         kTestUserAgent));
47 
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_)));
53   }
54 
55   // Returns a temporary file path suitable for storing the cache file.
GetTestCachedFilePath(const base::FilePath & file_name)56   base::FilePath GetTestCachedFilePath(const base::FilePath& file_name) {
57     return temp_dir_.path().Append(file_name);
58   }
59 
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_;
65 
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_;
70 };
71 
TEST_F(BaseRequestsServerTest,DownloadFileRequest_ValidFile)72 TEST_F(BaseRequestsServerTest, DownloadFileRequest_ValidFile) {
73   GDataErrorCode result_code = GDATA_OTHER_ERROR;
74   base::FilePath temp_file;
75   {
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/gdata/testfile.txt"),
85         GetTestCachedFilePath(
86             base::FilePath::FromUTF8Unsafe("cached_testfile.txt")));
87     request_sender_->StartRequestWithRetry(request);
88     run_loop.Run();
89   }
90 
91   std::string contents;
92   base::ReadFileToString(temp_file, &contents);
93   base::DeleteFile(temp_file, false);
94 
95   EXPECT_EQ(HTTP_SUCCESS, result_code);
96   EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method);
97   EXPECT_EQ("/files/gdata/testfile.txt", http_request_.relative_url);
98 
99   const base::FilePath expected_path =
100       test_util::GetTestFilePath("gdata/testfile.txt");
101   std::string expected_contents;
102   base::ReadFileToString(expected_path, &expected_contents);
103   EXPECT_EQ(expected_contents, contents);
104 }
105 
TEST_F(BaseRequestsServerTest,DownloadFileRequest_NonExistentFile)106 TEST_F(BaseRequestsServerTest, DownloadFileRequest_NonExistentFile) {
107   GDataErrorCode result_code = GDATA_OTHER_ERROR;
108   base::FilePath temp_file;
109   {
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_->StartRequestWithRetry(request);
122     run_loop.Run();
123   }
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.
129 }
130 
131 }  // namespace google_apis
132