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 "chrome/browser/google_apis/base_requests.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 "chrome/browser/google_apis/dummy_auth_service.h"
14 #include "chrome/browser/google_apis/request_sender.h"
15 #include "chrome/browser/google_apis/task_util.h"
16 #include "chrome/browser/google_apis/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()
34 : test_server_(message_loop_
.message_loop_proxy()) {
37 virtual void SetUp() OVERRIDE
{
38 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
40 request_context_getter_
= new net::TestURLRequestContextGetter(
41 message_loop_
.message_loop_proxy());
43 request_sender_
.reset(new RequestSender(
45 request_context_getter_
.get(),
46 message_loop_
.message_loop_proxy(),
49 ASSERT_TRUE(test_server_
.InitializeAndWaitUntilReady());
50 test_server_
.RegisterRequestHandler(
51 base::Bind(&test_util::HandleDownloadFileRequest
,
52 test_server_
.base_url(),
53 base::Unretained(&http_request_
)));
56 // Returns a temporary file path suitable for storing the cache file.
57 base::FilePath
GetTestCachedFilePath(const base::FilePath
& file_name
) {
58 return temp_dir_
.path().Append(file_name
);
61 base::MessageLoopForIO message_loop_
; // Test server needs IO thread.
62 net::test_server::EmbeddedTestServer test_server_
;
63 scoped_ptr
<RequestSender
> request_sender_
;
64 scoped_refptr
<net::TestURLRequestContextGetter
> request_context_getter_
;
65 base::ScopedTempDir temp_dir_
;
67 // The incoming HTTP request is saved so tests can verify the request
68 // parameters like HTTP method (ex. some requests should use DELETE
70 net::test_server::HttpRequest http_request_
;
73 TEST_F(BaseRequestsServerTest
, DownloadFileRequest_ValidFile
) {
74 GDataErrorCode result_code
= GDATA_OTHER_ERROR
;
75 base::FilePath temp_file
;
77 base::RunLoop run_loop
;
78 DownloadFileRequestBase
* request
= new DownloadFileRequestBase(
79 request_sender_
.get(),
80 test_util::CreateQuitCallback(
82 test_util::CreateCopyResultCallback(&result_code
, &temp_file
)),
85 test_server_
.GetURL("/files/gdata/testfile.txt"),
86 GetTestCachedFilePath(
87 base::FilePath::FromUTF8Unsafe("cached_testfile.txt")));
88 request_sender_
->StartRequestWithRetry(request
);
93 base::ReadFileToString(temp_file
, &contents
);
94 base::DeleteFile(temp_file
, false);
96 EXPECT_EQ(HTTP_SUCCESS
, result_code
);
97 EXPECT_EQ(net::test_server::METHOD_GET
, http_request_
.method
);
98 EXPECT_EQ("/files/gdata/testfile.txt", http_request_
.relative_url
);
100 const base::FilePath expected_path
=
101 test_util::GetTestFilePath("gdata/testfile.txt");
102 std::string expected_contents
;
103 base::ReadFileToString(expected_path
, &expected_contents
);
104 EXPECT_EQ(expected_contents
, contents
);
107 TEST_F(BaseRequestsServerTest
, DownloadFileRequest_NonExistentFile
) {
108 GDataErrorCode result_code
= GDATA_OTHER_ERROR
;
109 base::FilePath temp_file
;
111 base::RunLoop run_loop
;
112 DownloadFileRequestBase
* request
= new DownloadFileRequestBase(
113 request_sender_
.get(),
114 test_util::CreateQuitCallback(
116 test_util::CreateCopyResultCallback(&result_code
, &temp_file
)),
117 GetContentCallback(),
119 test_server_
.GetURL("/files/gdata/no-such-file.txt"),
120 GetTestCachedFilePath(
121 base::FilePath::FromUTF8Unsafe("cache_no-such-file.txt")));
122 request_sender_
->StartRequestWithRetry(request
);
125 EXPECT_EQ(HTTP_NOT_FOUND
, result_code
);
126 EXPECT_EQ(net::test_server::METHOD_GET
, http_request_
.method
);
127 EXPECT_EQ("/files/gdata/no-such-file.txt",
128 http_request_
.relative_url
);
129 // Do not verify the not found message.
132 } // namespace google_apis