Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(676)

Side by Side Diff: google_apis/drive/drive_api_requests_unittest.cc

Issue 1084523002: Files.app: Implement sending batch requests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix memory leak in the test. Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « google_apis/drive/drive_api_requests.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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 return response.Pass();
414 }
415
397 // These are for the current upload file status. 416 // These are for the current upload file status.
398 int64 received_bytes_; 417 int64 received_bytes_;
399 int64 content_length_; 418 int64 content_length_;
400 }; 419 };
401 420
402 TEST_F(DriveApiRequestsTest, DriveApiDataRequest_Fields) { 421 TEST_F(DriveApiRequestsTest, DriveApiDataRequest_Fields) {
403 // Make sure that "fields" query param is supported by using its subclass, 422 // Make sure that "fields" query param is supported by using its subclass,
404 // AboutGetRequest. 423 // AboutGetRequest.
405 424
406 // Set an expected data file containing valid result. 425 // Set an expected data file containing valid result.
(...skipping 1474 matching lines...) Expand 10 before | Expand all | Expand 10 after
1881 1900
1882 expected.reset(base::JSONReader::Read( 1901 expected.reset(base::JSONReader::Read(
1883 "{\"role\":\"writer\", \"type\":\"domain\",\"value\":\"example.com\"}")); 1902 "{\"role\":\"writer\", \"type\":\"domain\",\"value\":\"example.com\"}"));
1884 ASSERT_TRUE(expected); 1903 ASSERT_TRUE(expected);
1885 1904
1886 result.reset(base::JSONReader::Read(http_request_.content)); 1905 result.reset(base::JSONReader::Read(http_request_.content));
1887 EXPECT_TRUE(http_request_.has_content); 1906 EXPECT_TRUE(http_request_.has_content);
1888 EXPECT_TRUE(base::Value::Equals(expected.get(), result.get())); 1907 EXPECT_TRUE(base::Value::Equals(expected.get(), result.get()));
1889 } 1908 }
1890 1909
1910 TEST_F(DriveApiRequestsTest, BatchUploadRequest) {
1911 // Preapre constants.
1912 const char kTestContentType[] = "text/plain";
1913 const std::string kTestContent(10, 'a');
1914 const base::FilePath kTestFilePath =
1915 temp_dir_.path().AppendASCII("upload_file.txt");
1916 ASSERT_TRUE(test_util::WriteStringToFile(kTestFilePath, kTestContent));
1917
1918 // Create batch request.
1919 drive::BatchUploadRequest* const request = new drive::BatchUploadRequest(
1920 request_sender_.get(), *url_generator_);
1921 request->SetBoundaryForTesting("OUTERBOUNDARY");
1922 request_sender_->StartRequestWithRetry(request);
1923
1924 // Create child request.
1925 DriveApiErrorCode error = DRIVE_OTHER_ERROR;
1926 scoped_ptr<FileResource> file_resource;
1927 base::RunLoop run_loop[2];
1928 for (int i = 0; i < 2; ++i) {
1929 const FileResourceCallback callback = test_util::CreateQuitCallback(
1930 &run_loop[i],
1931 test_util::CreateCopyResultCallback(&error, &file_resource));
1932 drive::MultipartUploadNewFileRequest* const child_request =
1933 new drive::MultipartUploadNewFileRequest(
1934 request_sender_.get(),
1935 base::StringPrintf("new file title %d", i),
1936 "parent_resource_id",
1937 kTestContentType,
1938 kTestContent.size(),
1939 base::Time(),
1940 base::Time(),
1941 kTestFilePath,
1942 drive::Properties(),
1943 *url_generator_,
1944 callback,
1945 ProgressCallback());
1946 child_request->SetBoundaryForTesting("INNERBOUNDARY");
1947 request->AddRequest(child_request);
1948 }
1949 request->Commit();
1950 run_loop[0].Run();
1951 run_loop[1].Run();
1952
1953 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method);
1954 EXPECT_EQ("batch", http_request_.headers["X-Goog-Upload-Protocol"]);
1955 EXPECT_EQ("multipart/mixed; boundary=OUTERBOUNDARY",
1956 http_request_.headers["Content-Type"]);
1957 EXPECT_EQ("--OUTERBOUNDARY\n"
1958 "Content-Type: application/http\n"
1959 "\n"
1960 "POST /upload/drive/v2/files HTTP/1.1\n"
1961 "Host: 127.0.0.1\n"
1962 "X-Goog-Upload-Protocol: multipart\n"
1963 "Content-Type: multipart/related; boundary=INNERBOUNDARY\n"
1964 "\n"
1965 "--INNERBOUNDARY\n"
1966 "Content-Type: application/json\n"
1967 "\n"
1968 "{\"parents\":[{\"id\":\"parent_resource_id\","
1969 "\"kind\":\"drive#fileLink\"}],\"title\":\"new file title 0\"}\n"
1970 "--INNERBOUNDARY\n"
1971 "Content-Type: text/plain\n"
1972 "\n"
1973 "aaaaaaaaaa\n"
1974 "--INNERBOUNDARY--\n"
1975 "--OUTERBOUNDARY\n"
1976 "Content-Type: application/http\n"
1977 "\n"
1978 "POST /upload/drive/v2/files HTTP/1.1\n"
1979 "Host: 127.0.0.1\n"
1980 "X-Goog-Upload-Protocol: multipart\n"
1981 "Content-Type: multipart/related; boundary=INNERBOUNDARY\n"
1982 "\n"
1983 "--INNERBOUNDARY\n"
1984 "Content-Type: application/json\n"
1985 "\n"
1986 "{\"parents\":[{\"id\":\"parent_resource_id\","
1987 "\"kind\":\"drive#fileLink\"}],\"title\":\"new file title 1\"}\n"
1988 "--INNERBOUNDARY\n"
1989 "Content-Type: text/plain\n"
1990 "\n"
1991 "aaaaaaaaaa\n"
1992 "--INNERBOUNDARY--\n"
1993 "--OUTERBOUNDARY--",
1994 http_request_.content);
1995 }
1996
1997 TEST_F(DriveApiRequestsTest, EmptyBatchUploadRequest) {
1998 scoped_ptr<drive::BatchUploadRequest> request(new drive::BatchUploadRequest(
1999 request_sender_.get(), *url_generator_));
2000 EXPECT_DEATH(request->Commit(), "Check failed");
Nico 2015/04/15 14:25:51 Aha, that's because official builds compile out st
2001 }
2002
1891 } // namespace google_apis 2003 } // namespace google_apis
OLDNEW
« no previous file with comments | « google_apis/drive/drive_api_requests.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698