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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/stringprintf.h" |
13 #include "base/values.h" | 14 #include "base/values.h" |
14 #include "google_apis/drive/drive_api_parser.h" | 15 #include "google_apis/drive/drive_api_parser.h" |
15 #include "google_apis/drive/drive_api_requests.h" | 16 #include "google_apis/drive/drive_api_requests.h" |
16 #include "google_apis/drive/drive_api_url_generator.h" | 17 #include "google_apis/drive/drive_api_url_generator.h" |
17 #include "google_apis/drive/dummy_auth_service.h" | 18 #include "google_apis/drive/dummy_auth_service.h" |
18 #include "google_apis/drive/request_sender.h" | 19 #include "google_apis/drive/request_sender.h" |
19 #include "google_apis/drive/test_util.h" | 20 #include "google_apis/drive/test_util.h" |
20 #include "net/test/embedded_test_server/embedded_test_server.h" | 21 #include "net/test/embedded_test_server/embedded_test_server.h" |
21 #include "net/test/embedded_test_server/http_request.h" | 22 #include "net/test/embedded_test_server/http_request.h" |
22 #include "net/test/embedded_test_server/http_response.h" | 23 #include "net/test/embedded_test_server/http_response.h" |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 base::Unretained(this))); | 94 base::Unretained(this))); |
94 test_server_.RegisterRequestHandler( | 95 test_server_.RegisterRequestHandler( |
95 base::Bind(&DriveApiRequestsTest::HandleInitiateUploadRequest, | 96 base::Bind(&DriveApiRequestsTest::HandleInitiateUploadRequest, |
96 base::Unretained(this))); | 97 base::Unretained(this))); |
97 test_server_.RegisterRequestHandler( | 98 test_server_.RegisterRequestHandler( |
98 base::Bind(&DriveApiRequestsTest::HandleContentResponse, | 99 base::Bind(&DriveApiRequestsTest::HandleContentResponse, |
99 base::Unretained(this))); | 100 base::Unretained(this))); |
100 test_server_.RegisterRequestHandler( | 101 test_server_.RegisterRequestHandler( |
101 base::Bind(&DriveApiRequestsTest::HandleDownloadRequest, | 102 base::Bind(&DriveApiRequestsTest::HandleDownloadRequest, |
102 base::Unretained(this))); | 103 base::Unretained(this))); |
| 104 test_server_.RegisterRequestHandler( |
| 105 base::Bind(&DriveApiRequestsTest::HandleBatchUploadRequest, |
| 106 base::Unretained(this))); |
103 | 107 |
104 GURL test_base_url = test_util::GetBaseUrlForTesting(test_server_.port()); | 108 GURL test_base_url = test_util::GetBaseUrlForTesting(test_server_.port()); |
105 url_generator_.reset( | 109 url_generator_.reset( |
106 new DriveApiUrlGenerator(test_base_url, test_base_url)); | 110 new DriveApiUrlGenerator(test_base_url, test_base_url)); |
107 | 111 |
108 // Reset the server's expected behavior just in case. | 112 // Reset the server's expected behavior just in case. |
109 ResetExpectedResponse(); | 113 ResetExpectedResponse(); |
110 received_bytes_ = 0; | 114 received_bytes_ = 0; |
111 content_length_ = 0; | 115 content_length_ = 0; |
112 | 116 |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 | 391 |
388 // For testing, returns a text with |id| repeated 3 times. | 392 // For testing, returns a text with |id| repeated 3 times. |
389 scoped_ptr<net::test_server::BasicHttpResponse> response( | 393 scoped_ptr<net::test_server::BasicHttpResponse> response( |
390 new net::test_server::BasicHttpResponse); | 394 new net::test_server::BasicHttpResponse); |
391 response->set_code(net::HTTP_OK); | 395 response->set_code(net::HTTP_OK); |
392 response->set_content(id + id + id); | 396 response->set_content(id + id + id); |
393 response->set_content_type("text/plain"); | 397 response->set_content_type("text/plain"); |
394 return response.Pass(); | 398 return response.Pass(); |
395 } | 399 } |
396 | 400 |
| 401 scoped_ptr<net::test_server::HttpResponse> HandleBatchUploadRequest( |
| 402 const net::test_server::HttpRequest& request) { |
| 403 http_request_ = request; |
| 404 |
| 405 const GURL absolute_url = test_server_.GetURL(request.relative_url); |
| 406 std::string id; |
| 407 if (absolute_url.path() != "/upload/drive") |
| 408 return scoped_ptr<net::test_server::HttpResponse>(); |
| 409 |
| 410 scoped_ptr<net::test_server::BasicHttpResponse> response( |
| 411 new net::test_server::BasicHttpResponse); |
| 412 response->set_code(net::HTTP_OK); |
| 413 response->set_content("{}"); |
| 414 response->set_content_type("text/plain"); |
| 415 // response->set_content("{\"kind\": \"drive#file\", \"id\": \"file_id\"}"); |
| 416 return response.Pass(); |
| 417 } |
| 418 |
397 // These are for the current upload file status. | 419 // These are for the current upload file status. |
398 int64 received_bytes_; | 420 int64 received_bytes_; |
399 int64 content_length_; | 421 int64 content_length_; |
400 }; | 422 }; |
401 | 423 |
402 TEST_F(DriveApiRequestsTest, DriveApiDataRequest_Fields) { | 424 TEST_F(DriveApiRequestsTest, DriveApiDataRequest_Fields) { |
403 // Make sure that "fields" query param is supported by using its subclass, | 425 // Make sure that "fields" query param is supported by using its subclass, |
404 // AboutGetRequest. | 426 // AboutGetRequest. |
405 | 427 |
406 // Set an expected data file containing valid result. | 428 // Set an expected data file containing valid result. |
(...skipping 1474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1881 | 1903 |
1882 expected.reset(base::JSONReader::Read( | 1904 expected.reset(base::JSONReader::Read( |
1883 "{\"role\":\"writer\", \"type\":\"domain\",\"value\":\"example.com\"}")); | 1905 "{\"role\":\"writer\", \"type\":\"domain\",\"value\":\"example.com\"}")); |
1884 ASSERT_TRUE(expected); | 1906 ASSERT_TRUE(expected); |
1885 | 1907 |
1886 result.reset(base::JSONReader::Read(http_request_.content)); | 1908 result.reset(base::JSONReader::Read(http_request_.content)); |
1887 EXPECT_TRUE(http_request_.has_content); | 1909 EXPECT_TRUE(http_request_.has_content); |
1888 EXPECT_TRUE(base::Value::Equals(expected.get(), result.get())); | 1910 EXPECT_TRUE(base::Value::Equals(expected.get(), result.get())); |
1889 } | 1911 } |
1890 | 1912 |
| 1913 TEST_F(DriveApiRequestsTest, BatchUploadRequest) { |
| 1914 // Preapre constants. |
| 1915 const char kTestContentType[] = "text/plain"; |
| 1916 const std::string kTestContent(10, 'a'); |
| 1917 const base::FilePath kTestFilePath = |
| 1918 temp_dir_.path().AppendASCII("upload_file.txt"); |
| 1919 ASSERT_TRUE(test_util::WriteStringToFile(kTestFilePath, kTestContent)); |
| 1920 |
| 1921 // Create batch request. |
| 1922 drive::BatchUploadRequest* const request = new drive::BatchUploadRequest( |
| 1923 request_sender_.get(), *url_generator_); |
| 1924 request->SetBoundaryForTesting("OUTERBOUNDARY"); |
| 1925 request_sender_->StartRequestWithRetry(request); |
| 1926 |
| 1927 // Create child request. |
| 1928 DriveApiErrorCode error = DRIVE_OTHER_ERROR; |
| 1929 scoped_ptr<FileResource> file_resource; |
| 1930 base::RunLoop run_loop[2]; |
| 1931 for (int i = 0; i < 2; ++i) { |
| 1932 const FileResourceCallback callback = test_util::CreateQuitCallback( |
| 1933 &run_loop[i], |
| 1934 test_util::CreateCopyResultCallback(&error, &file_resource)); |
| 1935 drive::MultipartUploadNewFileRequest* const child_request = |
| 1936 new drive::MultipartUploadNewFileRequest( |
| 1937 request_sender_.get(), |
| 1938 base::StringPrintf("new file title %d", i), |
| 1939 "parent_resource_id", |
| 1940 kTestContentType, |
| 1941 kTestContent.size(), |
| 1942 base::Time(), |
| 1943 base::Time(), |
| 1944 kTestFilePath, |
| 1945 drive::Properties(), |
| 1946 *url_generator_, |
| 1947 callback, |
| 1948 ProgressCallback()); |
| 1949 child_request->SetBoundaryForTesting("INNERBOUNDARY"); |
| 1950 request->AddRequest(child_request); |
| 1951 } |
| 1952 request->Commit(); |
| 1953 run_loop[0].Run(); |
| 1954 run_loop[1].Run(); |
| 1955 |
| 1956 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method); |
| 1957 EXPECT_EQ("batch", http_request_.headers["X-Goog-Upload-Protocol"]); |
| 1958 EXPECT_EQ("multipart/mixed; boundary=OUTERBOUNDARY", |
| 1959 http_request_.headers["Content-Type"]); |
| 1960 EXPECT_EQ("--OUTERBOUNDARY\n" |
| 1961 "Content-Type: application/http\n" |
| 1962 "\n" |
| 1963 "POST /upload/drive/v2/files HTTP/1.1\n" |
| 1964 "Host: www.googleapis.com\n" |
| 1965 "X-Goog-Upload-Protocol: multipart\n" |
| 1966 "Content-Type: multipart/related; boundary=INNERBOUNDARY\n" |
| 1967 "\n" |
| 1968 "--INNERBOUNDARY\n" |
| 1969 "Content-Type: application/json\n" |
| 1970 "\n" |
| 1971 "{\"parents\":[{\"id\":\"parent_resource_id\"," |
| 1972 "\"kind\":\"drive#fileLink\"}],\"title\":\"new file title 0\"}\n" |
| 1973 "--INNERBOUNDARY\n" |
| 1974 "Content-Type: text/plain\n" |
| 1975 "\n" |
| 1976 "aaaaaaaaaa\n" |
| 1977 "--INNERBOUNDARY--\n" |
| 1978 "--OUTERBOUNDARY\n" |
| 1979 "Content-Type: application/http\n" |
| 1980 "\n" |
| 1981 "POST /upload/drive/v2/files HTTP/1.1\n" |
| 1982 "Host: www.googleapis.com\n" |
| 1983 "X-Goog-Upload-Protocol: multipart\n" |
| 1984 "Content-Type: multipart/related; boundary=INNERBOUNDARY\n" |
| 1985 "\n" |
| 1986 "--INNERBOUNDARY\n" |
| 1987 "Content-Type: application/json\n" |
| 1988 "\n" |
| 1989 "{\"parents\":[{\"id\":\"parent_resource_id\"," |
| 1990 "\"kind\":\"drive#fileLink\"}],\"title\":\"new file title 1\"}\n" |
| 1991 "--INNERBOUNDARY\n" |
| 1992 "Content-Type: text/plain\n" |
| 1993 "\n" |
| 1994 "aaaaaaaaaa\n" |
| 1995 "--INNERBOUNDARY--\n" |
| 1996 "--OUTERBOUNDARY--", |
| 1997 http_request_.content); |
| 1998 } |
| 1999 |
1891 } // namespace google_apis | 2000 } // namespace google_apis |
OLD | NEW |