| 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/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/message_loop/message_loop_proxy.h" | 8 #include "base/message_loop/message_loop_proxy.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 if (request.method != net::test_server::METHOD_DELETE || | 148 if (request.method != net::test_server::METHOD_DELETE || |
| 149 request.relative_url.find("/children/") == string::npos) { | 149 request.relative_url.find("/children/") == string::npos) { |
| 150 // The request is not the "Children: delete" operation. Delegate the | 150 // The request is not the "Children: delete" operation. Delegate the |
| 151 // processing to the next handler. | 151 // processing to the next handler. |
| 152 return scoped_ptr<net::test_server::HttpResponse>(); | 152 return scoped_ptr<net::test_server::HttpResponse>(); |
| 153 } | 153 } |
| 154 | 154 |
| 155 http_request_ = request; | 155 http_request_ = request; |
| 156 | 156 |
| 157 // Return the response with just "204 No Content" status code. | 157 // Return the response with just "204 No Content" status code. |
| 158 scoped_ptr<net::test_server::HttpResponse> http_response( | 158 scoped_ptr<net::test_server::BasicHttpResponse> http_response( |
| 159 new net::test_server::HttpResponse); | 159 new net::test_server::BasicHttpResponse); |
| 160 http_response->set_code(net::test_server::NO_CONTENT); | 160 http_response->set_code(net::test_server::NO_CONTENT); |
| 161 return http_response.Pass(); | 161 return http_response.PassAs<net::test_server::HttpResponse>(); |
| 162 } | 162 } |
| 163 | 163 |
| 164 // Reads the data file of |expected_data_file_path_| and returns its content | 164 // Reads the data file of |expected_data_file_path_| and returns its content |
| 165 // for the request. | 165 // for the request. |
| 166 // To use this method, it is necessary to set |expected_data_file_path_| | 166 // To use this method, it is necessary to set |expected_data_file_path_| |
| 167 // to the appropriate file path before sending the request to the server. | 167 // to the appropriate file path before sending the request to the server. |
| 168 scoped_ptr<net::test_server::HttpResponse> HandleDataFileRequest( | 168 scoped_ptr<net::test_server::HttpResponse> HandleDataFileRequest( |
| 169 const net::test_server::HttpRequest& request) { | 169 const net::test_server::HttpRequest& request) { |
| 170 if (expected_data_file_path_.empty()) { | 170 if (expected_data_file_path_.empty()) { |
| 171 // The file is not specified. Delegate the processing to the next | 171 // The file is not specified. Delegate the processing to the next |
| 172 // handler. | 172 // handler. |
| 173 return scoped_ptr<net::test_server::HttpResponse>(); | 173 return scoped_ptr<net::test_server::HttpResponse>(); |
| 174 } | 174 } |
| 175 | 175 |
| 176 http_request_ = request; | 176 http_request_ = request; |
| 177 | 177 |
| 178 // Return the response from the data file. | 178 // Return the response from the data file. |
| 179 return test_util::CreateHttpResponseFromFile(expected_data_file_path_); | 179 return test_util::CreateHttpResponseFromFile( |
| 180 expected_data_file_path_).PassAs<net::test_server::HttpResponse>(); |
| 180 } | 181 } |
| 181 | 182 |
| 182 // Returns the response based on set expected upload url. | 183 // Returns the response based on set expected upload url. |
| 183 // The response contains the url in its "Location: " header. Also, it doesn't | 184 // The response contains the url in its "Location: " header. Also, it doesn't |
| 184 // have any content. | 185 // have any content. |
| 185 // To use this method, it is necessary to set |expected_upload_path_| | 186 // To use this method, it is necessary to set |expected_upload_path_| |
| 186 // to the string representation of the url to be returned. | 187 // to the string representation of the url to be returned. |
| 187 scoped_ptr<net::test_server::HttpResponse> HandleInitiateUploadRequest( | 188 scoped_ptr<net::test_server::HttpResponse> HandleInitiateUploadRequest( |
| 188 const net::test_server::HttpRequest& request) { | 189 const net::test_server::HttpRequest& request) { |
| 189 if (request.relative_url == expected_upload_path_ || | 190 if (request.relative_url == expected_upload_path_ || |
| 190 expected_upload_path_.empty()) { | 191 expected_upload_path_.empty()) { |
| 191 // The request is for resume uploading or the expected upload url is not | 192 // The request is for resume uploading or the expected upload url is not |
| 192 // set. Delegate the processing to the next handler. | 193 // set. Delegate the processing to the next handler. |
| 193 return scoped_ptr<net::test_server::HttpResponse>(); | 194 return scoped_ptr<net::test_server::HttpResponse>(); |
| 194 } | 195 } |
| 195 | 196 |
| 196 http_request_ = request; | 197 http_request_ = request; |
| 197 | 198 |
| 198 scoped_ptr<net::test_server::HttpResponse> response( | 199 scoped_ptr<net::test_server::BasicHttpResponse> response( |
| 199 new net::test_server::HttpResponse); | 200 new net::test_server::BasicHttpResponse); |
| 200 | 201 |
| 201 // Check an ETag. | 202 // Check an ETag. |
| 202 std::map<std::string, std::string>::const_iterator found = | 203 std::map<std::string, std::string>::const_iterator found = |
| 203 request.headers.find("If-Match"); | 204 request.headers.find("If-Match"); |
| 204 if (found != request.headers.end() && | 205 if (found != request.headers.end() && |
| 205 found->second != "*" && | 206 found->second != "*" && |
| 206 found->second != kTestETag) { | 207 found->second != kTestETag) { |
| 207 response->set_code(net::test_server::PRECONDITION); | 208 response->set_code(net::test_server::PRECONDITION); |
| 208 return response.Pass(); | 209 return response.PassAs<net::test_server::HttpResponse>(); |
| 209 } | 210 } |
| 210 | 211 |
| 211 // Check if the X-Upload-Content-Length is present. If yes, store the | 212 // Check if the X-Upload-Content-Length is present. If yes, store the |
| 212 // length of the file. | 213 // length of the file. |
| 213 found = request.headers.find("X-Upload-Content-Length"); | 214 found = request.headers.find("X-Upload-Content-Length"); |
| 214 if (found == request.headers.end() || | 215 if (found == request.headers.end() || |
| 215 !base::StringToInt64(found->second, &content_length_)) { | 216 !base::StringToInt64(found->second, &content_length_)) { |
| 216 return scoped_ptr<net::test_server::HttpResponse>(); | 217 return scoped_ptr<net::test_server::HttpResponse>(); |
| 217 } | 218 } |
| 218 received_bytes_ = 0; | 219 received_bytes_ = 0; |
| 219 | 220 |
| 220 response->set_code(net::test_server::SUCCESS); | 221 response->set_code(net::test_server::SUCCESS); |
| 221 response->AddCustomHeader( | 222 response->AddCustomHeader( |
| 222 "Location", | 223 "Location", |
| 223 test_server_.base_url().Resolve(expected_upload_path_).spec()); | 224 test_server_.base_url().Resolve(expected_upload_path_).spec()); |
| 224 return response.Pass(); | 225 return response.PassAs<net::test_server::HttpResponse>(); |
| 225 } | 226 } |
| 226 | 227 |
| 227 scoped_ptr<net::test_server::HttpResponse> HandleResumeUploadRequest( | 228 scoped_ptr<net::test_server::HttpResponse> HandleResumeUploadRequest( |
| 228 const net::test_server::HttpRequest& request) { | 229 const net::test_server::HttpRequest& request) { |
| 229 if (request.relative_url != expected_upload_path_) { | 230 if (request.relative_url != expected_upload_path_) { |
| 230 // The request path is different from the expected path for uploading. | 231 // The request path is different from the expected path for uploading. |
| 231 // Delegate the processing to the next handler. | 232 // Delegate the processing to the next handler. |
| 232 return scoped_ptr<net::test_server::HttpResponse>(); | 233 return scoped_ptr<net::test_server::HttpResponse>(); |
| 233 } | 234 } |
| 234 | 235 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 252 } | 253 } |
| 253 | 254 |
| 254 EXPECT_EQ(start_position, received_bytes_); | 255 EXPECT_EQ(start_position, received_bytes_); |
| 255 EXPECT_EQ(length, content_length_); | 256 EXPECT_EQ(length, content_length_); |
| 256 | 257 |
| 257 // end_position is inclusive, but so +1 to change the range to byte size. | 258 // end_position is inclusive, but so +1 to change the range to byte size. |
| 258 received_bytes_ = end_position + 1; | 259 received_bytes_ = end_position + 1; |
| 259 } | 260 } |
| 260 | 261 |
| 261 if (received_bytes_ < content_length_) { | 262 if (received_bytes_ < content_length_) { |
| 262 scoped_ptr<net::test_server::HttpResponse> response( | 263 scoped_ptr<net::test_server::BasicHttpResponse> response( |
| 263 new net::test_server::HttpResponse); | 264 new net::test_server::BasicHttpResponse); |
| 264 // Set RESUME INCOMPLETE (308) status code. | 265 // Set RESUME INCOMPLETE (308) status code. |
| 265 response->set_code(net::test_server::RESUME_INCOMPLETE); | 266 response->set_code(net::test_server::RESUME_INCOMPLETE); |
| 266 | 267 |
| 267 // Add Range header to the response, based on the values of | 268 // Add Range header to the response, based on the values of |
| 268 // Content-Range header in the request. | 269 // Content-Range header in the request. |
| 269 // The header is annotated only when at least one byte is received. | 270 // The header is annotated only when at least one byte is received. |
| 270 if (received_bytes_ > 0) { | 271 if (received_bytes_ > 0) { |
| 271 response->AddCustomHeader( | 272 response->AddCustomHeader( |
| 272 "Range", "bytes=0-" + base::Int64ToString(received_bytes_ - 1)); | 273 "Range", "bytes=0-" + base::Int64ToString(received_bytes_ - 1)); |
| 273 } | 274 } |
| 274 | 275 |
| 275 return response.Pass(); | 276 return response.PassAs<net::test_server::HttpResponse>(); |
| 276 } | 277 } |
| 277 | 278 |
| 278 // All bytes are received. Return the "success" response with the file's | 279 // All bytes are received. Return the "success" response with the file's |
| 279 // (dummy) metadata. | 280 // (dummy) metadata. |
| 280 scoped_ptr<net::test_server::HttpResponse> response = | 281 scoped_ptr<net::test_server::BasicHttpResponse> response = |
| 281 test_util::CreateHttpResponseFromFile( | 282 test_util::CreateHttpResponseFromFile( |
| 282 test_util::GetTestFilePath("chromeos/drive/file_entry.json")); | 283 test_util::GetTestFilePath("chromeos/drive/file_entry.json")); |
| 283 | 284 |
| 284 // The response code is CREATED if it is new file uploading. | 285 // The response code is CREATED if it is new file uploading. |
| 285 if (http_request_.relative_url == kTestUploadNewFilePath) { | 286 if (http_request_.relative_url == kTestUploadNewFilePath) { |
| 286 response->set_code(net::test_server::CREATED); | 287 response->set_code(net::test_server::CREATED); |
| 287 } | 288 } |
| 288 | 289 |
| 289 return response.Pass(); | 290 return response.PassAs<net::test_server::HttpResponse>(); |
| 290 } | 291 } |
| 291 | 292 |
| 292 // Returns the response based on set expected content and its type. | 293 // Returns the response based on set expected content and its type. |
| 293 // To use this method, both |expected_content_type_| and |expected_content_| | 294 // To use this method, both |expected_content_type_| and |expected_content_| |
| 294 // must be set in advance. | 295 // must be set in advance. |
| 295 scoped_ptr<net::test_server::HttpResponse> HandleContentResponse( | 296 scoped_ptr<net::test_server::HttpResponse> HandleContentResponse( |
| 296 const net::test_server::HttpRequest& request) { | 297 const net::test_server::HttpRequest& request) { |
| 297 if (expected_content_type_.empty() || expected_content_.empty()) { | 298 if (expected_content_type_.empty() || expected_content_.empty()) { |
| 298 // Expected content is not set. Delegate the processing to the next | 299 // Expected content is not set. Delegate the processing to the next |
| 299 // handler. | 300 // handler. |
| 300 return scoped_ptr<net::test_server::HttpResponse>(); | 301 return scoped_ptr<net::test_server::HttpResponse>(); |
| 301 } | 302 } |
| 302 | 303 |
| 303 http_request_ = request; | 304 http_request_ = request; |
| 304 | 305 |
| 305 scoped_ptr<net::test_server::HttpResponse> response( | 306 scoped_ptr<net::test_server::BasicHttpResponse> response( |
| 306 new net::test_server::HttpResponse); | 307 new net::test_server::BasicHttpResponse); |
| 307 response->set_code(net::test_server::SUCCESS); | 308 response->set_code(net::test_server::SUCCESS); |
| 308 response->set_content_type(expected_content_type_); | 309 response->set_content_type(expected_content_type_); |
| 309 response->set_content(expected_content_); | 310 response->set_content(expected_content_); |
| 310 return response.Pass(); | 311 return response.PassAs<net::test_server::HttpResponse>(); |
| 311 } | 312 } |
| 312 | 313 |
| 313 // These are for the current upload file status. | 314 // These are for the current upload file status. |
| 314 int64 received_bytes_; | 315 int64 received_bytes_; |
| 315 int64 content_length_; | 316 int64 content_length_; |
| 316 }; | 317 }; |
| 317 | 318 |
| 318 TEST_F(DriveApiOperationsTest, GetAboutOperation_ValidJson) { | 319 TEST_F(DriveApiOperationsTest, GetAboutOperation_ValidJson) { |
| 319 // Set an expected data file containing valid result. | 320 // Set an expected data file containing valid result. |
| 320 expected_data_file_path_ = test_util::GetTestFilePath( | 321 expected_data_file_path_ = test_util::GetTestFilePath( |
| (...skipping 985 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1306 EXPECT_EQ("Conflicting-etag", http_request_.headers["If-Match"]); | 1307 EXPECT_EQ("Conflicting-etag", http_request_.headers["If-Match"]); |
| 1307 | 1308 |
| 1308 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method); | 1309 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method); |
| 1309 EXPECT_EQ("/upload/drive/v2/files/resource_id?uploadType=resumable", | 1310 EXPECT_EQ("/upload/drive/v2/files/resource_id?uploadType=resumable", |
| 1310 http_request_.relative_url); | 1311 http_request_.relative_url); |
| 1311 EXPECT_TRUE(http_request_.has_content); | 1312 EXPECT_TRUE(http_request_.has_content); |
| 1312 EXPECT_TRUE(http_request_.content.empty()); | 1313 EXPECT_TRUE(http_request_.content.empty()); |
| 1313 } | 1314 } |
| 1314 | 1315 |
| 1315 } // namespace google_apis | 1316 } // namespace google_apis |
| OLD | NEW |