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

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

Issue 1081313002: Drive: Add response handling to BatchUploadRequst class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed. 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
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"
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 http_request_ = request; 403 http_request_ = request;
404 404
405 const GURL absolute_url = test_server_.GetURL(request.relative_url); 405 const GURL absolute_url = test_server_.GetURL(request.relative_url);
406 std::string id; 406 std::string id;
407 if (absolute_url.path() != "/upload/drive") 407 if (absolute_url.path() != "/upload/drive")
408 return scoped_ptr<net::test_server::HttpResponse>(); 408 return scoped_ptr<net::test_server::HttpResponse>();
409 409
410 scoped_ptr<net::test_server::BasicHttpResponse> response( 410 scoped_ptr<net::test_server::BasicHttpResponse> response(
411 new net::test_server::BasicHttpResponse); 411 new net::test_server::BasicHttpResponse);
412 response->set_code(net::HTTP_OK); 412 response->set_code(net::HTTP_OK);
413 response->set_content_type("multipart/mixed; boundary=BOUNDARY");
414 response->set_content(
415 "--BOUNDARY\r\n"
416 "Content-Type: application/http\r\n"
417 "\r\n"
418 "HTTP/1.1 200 OK\r\n"
419 "Content-Type: application/json; charset=UTF-8\r\n"
420 "\r\n"
421 "{\r\n"
422 " \"kind\": \"drive#file\",\r\n"
423 " \"id\": \"file_id_1\"\r\n"
424 "}\r\n"
425 "\r\n"
426 "--BOUNDARY\r\n"
427 "Content-Type: application/http\r\n"
428 "\r\n"
429 "HTTP/1.1 503 Service Unavailable\r\n"
430 "Content-Type: application/json; charset=UTF-8\r\n"
431 "\r\n"
432 "{}\r\n"
433 "\r\n"
434 "--BOUNDARY--\r\n");
413 return response.Pass(); 435 return response.Pass();
414 } 436 }
415 437
416 // These are for the current upload file status. 438 // These are for the current upload file status.
417 int64 received_bytes_; 439 int64 received_bytes_;
418 int64 content_length_; 440 int64 content_length_;
419 }; 441 };
420 442
421 TEST_F(DriveApiRequestsTest, DriveApiDataRequest_Fields) { 443 TEST_F(DriveApiRequestsTest, DriveApiDataRequest_Fields) {
422 // Make sure that "fields" query param is supported by using its subclass, 444 // Make sure that "fields" query param is supported by using its subclass,
(...skipping 1486 matching lines...) Expand 10 before | Expand all | Expand 10 after
1909 1931
1910 TEST_F(DriveApiRequestsTest, BatchUploadRequest) { 1932 TEST_F(DriveApiRequestsTest, BatchUploadRequest) {
1911 // Preapre constants. 1933 // Preapre constants.
1912 const char kTestContentType[] = "text/plain"; 1934 const char kTestContentType[] = "text/plain";
1913 const std::string kTestContent(10, 'a'); 1935 const std::string kTestContent(10, 'a');
1914 const base::FilePath kTestFilePath = 1936 const base::FilePath kTestFilePath =
1915 temp_dir_.path().AppendASCII("upload_file.txt"); 1937 temp_dir_.path().AppendASCII("upload_file.txt");
1916 ASSERT_TRUE(test_util::WriteStringToFile(kTestFilePath, kTestContent)); 1938 ASSERT_TRUE(test_util::WriteStringToFile(kTestFilePath, kTestContent));
1917 1939
1918 // Create batch request. 1940 // Create batch request.
1919 drive::BatchUploadRequest* const request = new drive::BatchUploadRequest( 1941 drive::BatchUploadRequest* const request =
1920 request_sender_.get(), *url_generator_); 1942 new drive::BatchUploadRequest(request_sender_.get(), *url_generator_);
1921 request->SetBoundaryForTesting("OUTERBOUNDARY"); 1943 request->SetBoundaryForTesting("OUTERBOUNDARY");
1922 request_sender_->StartRequestWithRetry(request); 1944 request_sender_->StartRequestWithRetry(request);
1923 1945
1924 // Create child request. 1946 // Create child request.
1925 DriveApiErrorCode error = DRIVE_OTHER_ERROR; 1947 DriveApiErrorCode errors[] = {DRIVE_OTHER_ERROR, DRIVE_OTHER_ERROR};
1926 scoped_ptr<FileResource> file_resource; 1948 scoped_ptr<FileResource> file_resources[2];
1927 base::RunLoop run_loop[2]; 1949 base::RunLoop run_loop[2];
1928 for (int i = 0; i < 2; ++i) { 1950 for (int i = 0; i < 2; ++i) {
1929 const FileResourceCallback callback = test_util::CreateQuitCallback( 1951 const FileResourceCallback callback = test_util::CreateQuitCallback(
1930 &run_loop[i], 1952 &run_loop[i],
1931 test_util::CreateCopyResultCallback(&error, &file_resource)); 1953 test_util::CreateCopyResultCallback(&errors[i], &file_resources[i]));
1932 drive::MultipartUploadNewFileRequest* const child_request = 1954 drive::MultipartUploadNewFileRequest* const child_request =
1933 new drive::MultipartUploadNewFileRequest( 1955 new drive::MultipartUploadNewFileRequest(
1934 request_sender_.get(), 1956 request_sender_.get(), base::StringPrintf("new file title %d", i),
1935 base::StringPrintf("new file title %d", i), 1957 "parent_resource_id", kTestContentType, kTestContent.size(),
1936 "parent_resource_id", 1958 base::Time(), base::Time(), kTestFilePath, drive::Properties(),
1937 kTestContentType, 1959 *url_generator_, callback, ProgressCallback());
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"); 1960 child_request->SetBoundaryForTesting("INNERBOUNDARY");
1947 request->AddRequest(child_request); 1961 request->AddRequest(child_request);
1948 } 1962 }
1949 request->Commit(); 1963 request->Commit();
1950 run_loop[0].Run(); 1964 run_loop[0].Run();
1951 run_loop[1].Run(); 1965 run_loop[1].Run();
1952 1966
1953 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method); 1967 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method);
1954 EXPECT_EQ("batch", http_request_.headers["X-Goog-Upload-Protocol"]); 1968 EXPECT_EQ("batch", http_request_.headers["X-Goog-Upload-Protocol"]);
1955 EXPECT_EQ("multipart/mixed; boundary=OUTERBOUNDARY", 1969 EXPECT_EQ("multipart/mixed; boundary=OUTERBOUNDARY",
1956 http_request_.headers["Content-Type"]); 1970 http_request_.headers["Content-Type"]);
1957 EXPECT_EQ("--OUTERBOUNDARY\n" 1971 EXPECT_EQ(
1958 "Content-Type: application/http\n" 1972 "--OUTERBOUNDARY\n"
1959 "\n" 1973 "Content-Type: application/http\n"
1960 "POST /upload/drive/v2/files HTTP/1.1\n" 1974 "\n"
1961 "Host: 127.0.0.1\n" 1975 "POST /upload/drive/v2/files HTTP/1.1\n"
1962 "X-Goog-Upload-Protocol: multipart\n" 1976 "Host: 127.0.0.1\n"
1963 "Content-Type: multipart/related; boundary=INNERBOUNDARY\n" 1977 "X-Goog-Upload-Protocol: multipart\n"
1964 "\n" 1978 "Content-Type: multipart/related; boundary=INNERBOUNDARY\n"
1965 "--INNERBOUNDARY\n" 1979 "\n"
1966 "Content-Type: application/json\n" 1980 "--INNERBOUNDARY\n"
1967 "\n" 1981 "Content-Type: application/json\n"
1968 "{\"parents\":[{\"id\":\"parent_resource_id\"," 1982 "\n"
1969 "\"kind\":\"drive#fileLink\"}],\"title\":\"new file title 0\"}\n" 1983 "{\"parents\":[{\"id\":\"parent_resource_id\","
1970 "--INNERBOUNDARY\n" 1984 "\"kind\":\"drive#fileLink\"}],\"title\":\"new file title 0\"}\n"
1971 "Content-Type: text/plain\n" 1985 "--INNERBOUNDARY\n"
1972 "\n" 1986 "Content-Type: text/plain\n"
1973 "aaaaaaaaaa\n" 1987 "\n"
1974 "--INNERBOUNDARY--\n" 1988 "aaaaaaaaaa\n"
1975 "--OUTERBOUNDARY\n" 1989 "--INNERBOUNDARY--\n"
1976 "Content-Type: application/http\n" 1990 "--OUTERBOUNDARY\n"
1977 "\n" 1991 "Content-Type: application/http\n"
1978 "POST /upload/drive/v2/files HTTP/1.1\n" 1992 "\n"
1979 "Host: 127.0.0.1\n" 1993 "POST /upload/drive/v2/files HTTP/1.1\n"
1980 "X-Goog-Upload-Protocol: multipart\n" 1994 "Host: 127.0.0.1\n"
1981 "Content-Type: multipart/related; boundary=INNERBOUNDARY\n" 1995 "X-Goog-Upload-Protocol: multipart\n"
1982 "\n" 1996 "Content-Type: multipart/related; boundary=INNERBOUNDARY\n"
1983 "--INNERBOUNDARY\n" 1997 "\n"
1984 "Content-Type: application/json\n" 1998 "--INNERBOUNDARY\n"
1985 "\n" 1999 "Content-Type: application/json\n"
1986 "{\"parents\":[{\"id\":\"parent_resource_id\"," 2000 "\n"
1987 "\"kind\":\"drive#fileLink\"}],\"title\":\"new file title 1\"}\n" 2001 "{\"parents\":[{\"id\":\"parent_resource_id\","
1988 "--INNERBOUNDARY\n" 2002 "\"kind\":\"drive#fileLink\"}],\"title\":\"new file title 1\"}\n"
1989 "Content-Type: text/plain\n" 2003 "--INNERBOUNDARY\n"
1990 "\n" 2004 "Content-Type: text/plain\n"
1991 "aaaaaaaaaa\n" 2005 "\n"
1992 "--INNERBOUNDARY--\n" 2006 "aaaaaaaaaa\n"
1993 "--OUTERBOUNDARY--", 2007 "--INNERBOUNDARY--\n"
1994 http_request_.content); 2008 "--OUTERBOUNDARY--",
2009 http_request_.content);
2010 EXPECT_EQ(HTTP_SUCCESS, errors[0]);
2011 ASSERT_TRUE(file_resources[0]);
2012 EXPECT_EQ("file_id_1", file_resources[0]->file_id());
2013 ASSERT_FALSE(file_resources[1]);
2014 EXPECT_EQ(DRIVE_PARSE_ERROR, errors[1]);
1995 } 2015 }
1996 2016
1997 TEST_F(DriveApiRequestsTest, EmptyBatchUploadRequest) { 2017 TEST_F(DriveApiRequestsTest, EmptyBatchUploadRequest) {
1998 scoped_ptr<drive::BatchUploadRequest> request(new drive::BatchUploadRequest( 2018 scoped_ptr<drive::BatchUploadRequest> request(new drive::BatchUploadRequest(
1999 request_sender_.get(), *url_generator_)); 2019 request_sender_.get(), *url_generator_));
2000 EXPECT_DEATH(request->Commit(), ""); 2020 EXPECT_DEATH(request->Commit(), "");
2001 } 2021 }
2002 2022
2023 TEST(ParseMultipartResponseTest, Empty) {
2024 std::vector<drive::MultipartHttpResponse> parts;
2025 EXPECT_FALSE(drive::ParseMultipartResponse("BOUNDARY", "", &parts));
2026 EXPECT_FALSE(drive::ParseMultipartResponse("", "CONTENT", &parts));
2027 }
2028
2029 TEST(ParseMultipartResponseTest, Basic) {
2030 std::vector<drive::MultipartHttpResponse> parts;
2031 ASSERT_TRUE(drive::ParseMultipartResponse("BOUNDARY",
2032 "--BOUNDARY\r\n"
2033 "Content-Type: application/http\r\n"
2034 "\r\n"
2035 "HTTP/1.1 200 OK\r\n"
2036 "Header: value\r\n"
2037 "\r\n"
2038 "{}\r\n"
2039 "--BOUNDARY\r\n"
2040 "Content-Type: application/http\r\n"
2041 "\r\n"
2042 "HTTP/1.1 404 Not Found\r\n"
2043 "Header: value\r\n"
2044 "--BOUNDARY--",
2045 &parts));
2046 ASSERT_EQ(2u, parts.size());
2047 EXPECT_EQ(HTTP_SUCCESS, parts[0].code);
2048 EXPECT_EQ("{}\r\n", parts[0].body);
2049 EXPECT_EQ(HTTP_NOT_FOUND, parts[1].code);
2050 EXPECT_EQ("", parts[1].body);
2051 }
2052
2053 TEST(ParseMultipartResponseTest, InvalidStatusLine) {
2054 std::vector<drive::MultipartHttpResponse> parts;
2055 ASSERT_TRUE(drive::ParseMultipartResponse("BOUNDARY",
2056 "--BOUNDARY\r\n"
2057 "Content-Type: application/http\r\n"
2058 "\r\n"
2059 "InvalidStatusLine 200 \r\n"
2060 "Header: value\r\n"
2061 "\r\n"
2062 "{}\r\n"
2063 "--BOUNDARY--",
2064 &parts));
2065 ASSERT_EQ(1u, parts.size());
2066 EXPECT_EQ(DRIVE_PARSE_ERROR, parts[0].code);
2067 EXPECT_EQ("{}\r\n", parts[0].body);
2068 }
2069
2070 TEST(ParseMultipartResponseTest, BoundaryInTheBodyAndPreamble) {
2071 std::vector<drive::MultipartHttpResponse> parts;
2072 ASSERT_TRUE(drive::ParseMultipartResponse("BOUNDARY",
2073 "BOUNDARY\r\n"
2074 "PREUMBLE\r\n"
2075 "--BOUNDARY\r\n"
2076 "Content-Type: application/http\r\n"
2077 "\r\n"
2078 "HTTP/1.1 200 OK\r\n"
2079 "Header: value\r\n"
2080 "\r\n"
2081 "{BOUNDARY}\r\n"
2082 "--BOUNDARY--",
2083 &parts));
2084 ASSERT_EQ(1u, parts.size());
2085 EXPECT_EQ(HTTP_SUCCESS, parts[0].code);
2086 EXPECT_EQ("{BOUNDARY}\r\n", parts[0].body);
2087 }
2003 } // namespace google_apis 2088 } // namespace google_apis
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698