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

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: Fix compile error on windows. Created 5 years, 7 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(
2026 "multipart/mixed; boundary=BOUNDARY", "", &parts));
2027 EXPECT_FALSE(drive::ParseMultipartResponse("multipart/mixed; boundary=",
2028 "CONTENT", &parts));
2029 }
2030
2031 TEST(ParseMultipartResponseTest, Basic) {
2032 std::vector<drive::MultipartHttpResponse> parts;
2033 ASSERT_TRUE(
2034 drive::ParseMultipartResponse("multipart/mixed; boundary=BOUNDARY",
2035 "--BOUNDARY\r\n"
2036 "Content-Type: application/http\r\n"
2037 "\r\n"
2038 "HTTP/1.1 200 OK\r\n"
2039 "Header: value\r\n"
2040 "\r\n"
2041 "First line\r\n"
2042 "Second line\r\n"
2043 "--BOUNDARY\r\n"
2044 "Content-Type: application/http\r\n"
2045 "\r\n"
2046 "HTTP/1.1 404 Not Found\r\n"
2047 "Header: value\r\n"
2048 "--BOUNDARY--",
2049 &parts));
2050 ASSERT_EQ(2u, parts.size());
2051 EXPECT_EQ(HTTP_SUCCESS, parts[0].code);
2052 EXPECT_EQ("First line\r\nSecond line", parts[0].body);
2053 EXPECT_EQ(HTTP_NOT_FOUND, parts[1].code);
2054 EXPECT_EQ("", parts[1].body);
2055 }
2056
2057 TEST(ParseMultipartResponseTest, InvalidStatusLine) {
2058 std::vector<drive::MultipartHttpResponse> parts;
2059 ASSERT_TRUE(
2060 drive::ParseMultipartResponse("multipart/mixed; boundary=BOUNDARY",
2061 "--BOUNDARY\r\n"
2062 "Content-Type: application/http\r\n"
2063 "\r\n"
2064 "InvalidStatusLine 200 \r\n"
2065 "Header: value\r\n"
2066 "\r\n"
2067 "{}\r\n"
2068 "--BOUNDARY--",
2069 &parts));
2070 ASSERT_EQ(1u, parts.size());
2071 EXPECT_EQ(DRIVE_PARSE_ERROR, parts[0].code);
2072 EXPECT_EQ("{}", parts[0].body);
2073 }
2074
2075 TEST(ParseMultipartResponseTest, BoundaryInTheBodyAndPreamble) {
2076 std::vector<drive::MultipartHttpResponse> parts;
2077 ASSERT_TRUE(
2078 drive::ParseMultipartResponse("multipart/mixed; boundary=BOUNDARY",
2079 "BOUNDARY\r\n"
2080 "PREUMBLE\r\n"
2081 "--BOUNDARY\r\n"
2082 "Content-Type: application/http\r\n"
2083 "\r\n"
2084 "HTTP/1.1 200 OK\r\n"
2085 "Header: value\r\n"
2086 "\r\n"
2087 "{--BOUNDARY}\r\n"
2088 "--BOUNDARY--",
2089 &parts));
2090 ASSERT_EQ(1u, parts.size());
2091 EXPECT_EQ(HTTP_SUCCESS, parts[0].code);
2092 EXPECT_EQ("{--BOUNDARY}", parts[0].body);
2093 }
2094
2095 TEST(ParseMultipartResponseTest, QuatedBoundary) {
2096 std::vector<drive::MultipartHttpResponse> parts;
2097 ASSERT_TRUE(
2098 drive::ParseMultipartResponse("multipart/mixed; boundary=\"BOUNDARY\"",
2099 "--BOUNDARY\r\n"
2100 "Content-Type: application/http\r\n"
2101 "\r\n"
2102 "HTTP/1.1 200 OK\r\n"
2103 "Header: value\r\n"
2104 "\r\n"
2105 "BODY\r\n"
2106 "--BOUNDARY--",
2107 &parts));
2108 ASSERT_EQ(1u, parts.size());
2109 EXPECT_EQ(HTTP_SUCCESS, parts[0].code);
2110 EXPECT_EQ("BODY", parts[0].body);
2111 }
2112
2113 TEST(ParseMultipartResponseTest, BoundaryWithTransportPadding) {
2114 std::vector<drive::MultipartHttpResponse> parts;
2115 ASSERT_TRUE(
2116 drive::ParseMultipartResponse("multipart/mixed; boundary=BOUNDARY",
2117 "--BOUNDARY \t\r\n"
2118 "Content-Type: application/http\r\n"
2119 "\r\n"
2120 "HTTP/1.1 200 OK\r\n"
2121 "Header: value\r\n"
2122 "\r\n"
2123 "BODY\r\n"
2124 "--BOUNDARY-- \t",
2125 &parts));
2126 ASSERT_EQ(1u, parts.size());
2127 EXPECT_EQ(HTTP_SUCCESS, parts[0].code);
2128 EXPECT_EQ("BODY", parts[0].body);
2129 }
2003 } // namespace google_apis 2130 } // namespace google_apis
OLDNEW
« no previous file with comments | « google_apis/drive/drive_api_requests.cc ('k') | google_apis/drive/drive_api_url_generator_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698