OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/google_apis/base_requests.h" | 5 #include "chrome/browser/google_apis/base_requests.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" |
9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
11 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
12 #include "chrome/browser/google_apis/auth_service.h" | 13 #include "chrome/browser/google_apis/dummy_auth_service.h" |
13 #include "chrome/browser/google_apis/request_sender.h" | 14 #include "chrome/browser/google_apis/request_sender.h" |
14 #include "chrome/browser/google_apis/task_util.h" | 15 #include "chrome/browser/google_apis/task_util.h" |
15 #include "chrome/browser/google_apis/test_util.h" | 16 #include "chrome/browser/google_apis/test_util.h" |
16 #include "chrome/test/base/testing_profile.h" | |
17 #include "net/test/embedded_test_server/embedded_test_server.h" | 17 #include "net/test/embedded_test_server/embedded_test_server.h" |
18 #include "net/test/embedded_test_server/http_request.h" | 18 #include "net/test/embedded_test_server/http_request.h" |
19 #include "net/test/embedded_test_server/http_response.h" | 19 #include "net/test/embedded_test_server/http_response.h" |
20 #include "net/url_request/url_request_test_util.h" | 20 #include "net/url_request/url_request_test_util.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 | 22 |
23 namespace google_apis { | 23 namespace google_apis { |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 const char kTestAuthToken[] = "testtoken"; | |
28 const char kTestUserAgent[] = "test-user-agent"; | 27 const char kTestUserAgent[] = "test-user-agent"; |
29 | 28 |
30 } // namespace | 29 } // namespace |
31 | 30 |
32 class BaseRequestsServerTest : public testing::Test { | 31 class BaseRequestsServerTest : public testing::Test { |
33 protected: | 32 protected: |
34 BaseRequestsServerTest() | 33 BaseRequestsServerTest() |
35 : test_server_(message_loop_.message_loop_proxy()) { | 34 : test_server_(message_loop_.message_loop_proxy()) { |
36 } | 35 } |
37 | 36 |
38 virtual void SetUp() OVERRIDE { | 37 virtual void SetUp() OVERRIDE { |
39 profile_.reset(new TestingProfile); | 38 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
40 | 39 |
41 request_context_getter_ = new net::TestURLRequestContextGetter( | 40 request_context_getter_ = new net::TestURLRequestContextGetter( |
42 message_loop_.message_loop_proxy()); | 41 message_loop_.message_loop_proxy()); |
43 | 42 |
44 request_sender_.reset(new RequestSender(profile_.get(), | 43 request_sender_.reset(new RequestSender( |
45 request_context_getter_.get(), | 44 new DummyAuthService, |
46 message_loop_.message_loop_proxy(), | 45 request_context_getter_.get(), |
47 std::vector<std::string>(), | 46 message_loop_.message_loop_proxy(), |
48 kTestUserAgent)); | 47 kTestUserAgent)); |
49 request_sender_->auth_service()->set_access_token_for_testing( | |
50 kTestAuthToken); | |
51 | 48 |
52 ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); | 49 ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); |
53 test_server_.RegisterRequestHandler( | 50 test_server_.RegisterRequestHandler( |
54 base::Bind(&test_util::HandleDownloadFileRequest, | 51 base::Bind(&test_util::HandleDownloadFileRequest, |
55 test_server_.base_url(), | 52 test_server_.base_url(), |
56 base::Unretained(&http_request_))); | 53 base::Unretained(&http_request_))); |
57 } | 54 } |
58 | 55 |
59 // Returns a temporary file path suitable for storing the cache file. | 56 // Returns a temporary file path suitable for storing the cache file. |
60 base::FilePath GetTestCachedFilePath(const base::FilePath& file_name) { | 57 base::FilePath GetTestCachedFilePath(const base::FilePath& file_name) { |
61 return profile_->GetPath().Append(file_name); | 58 return temp_dir_.path().Append(file_name); |
62 } | 59 } |
63 | 60 |
64 base::MessageLoopForIO message_loop_; // Test server needs IO thread. | 61 base::MessageLoopForIO message_loop_; // Test server needs IO thread. |
65 net::test_server::EmbeddedTestServer test_server_; | 62 net::test_server::EmbeddedTestServer test_server_; |
66 scoped_ptr<TestingProfile> profile_; | |
67 scoped_ptr<RequestSender> request_sender_; | 63 scoped_ptr<RequestSender> request_sender_; |
68 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | 64 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
| 65 base::ScopedTempDir temp_dir_; |
69 | 66 |
70 // The incoming HTTP request is saved so tests can verify the request | 67 // The incoming HTTP request is saved so tests can verify the request |
71 // parameters like HTTP method (ex. some requests should use DELETE | 68 // parameters like HTTP method (ex. some requests should use DELETE |
72 // instead of GET). | 69 // instead of GET). |
73 net::test_server::HttpRequest http_request_; | 70 net::test_server::HttpRequest http_request_; |
74 }; | 71 }; |
75 | 72 |
76 TEST_F(BaseRequestsServerTest, DownloadFileRequest_ValidFile) { | 73 TEST_F(BaseRequestsServerTest, DownloadFileRequest_ValidFile) { |
77 GDataErrorCode result_code = GDATA_OTHER_ERROR; | 74 GDataErrorCode result_code = GDATA_OTHER_ERROR; |
78 base::FilePath temp_file; | 75 base::FilePath temp_file; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 run_loop.Run(); | 123 run_loop.Run(); |
127 } | 124 } |
128 EXPECT_EQ(HTTP_NOT_FOUND, result_code); | 125 EXPECT_EQ(HTTP_NOT_FOUND, result_code); |
129 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method); | 126 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method); |
130 EXPECT_EQ("/files/gdata/no-such-file.txt", | 127 EXPECT_EQ("/files/gdata/no-such-file.txt", |
131 http_request_.relative_url); | 128 http_request_.relative_url); |
132 // Do not verify the not found message. | 129 // Do not verify the not found message. |
133 } | 130 } |
134 | 131 |
135 } // namespace google_apis | 132 } // namespace google_apis |
OLD | NEW |