Skip to content

Commit

Permalink
Revert 169836 - google_apis: Add a test for DeleteDocumentOperation
Browse files Browse the repository at this point in the history
Along the way, update the existing tests to check if the method is GET,
whereas the DeleteDocumentOperation uses DELETE.

BUG=162348
TEST=none

Review URL: https://codereview.chromium.org/11308240

TBR=satorux@chromium.org
Review URL: https://codereview.chromium.org/11413211

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@169840 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
hashimoto@chromium.org committed Nov 28, 2012
1 parent 05f1faa commit 3a19f62
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 99 deletions.
133 changes: 36 additions & 97 deletions chrome/browser/google_apis/gdata_wapi_operations_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void CopyResultsFromGetDataCallbackAndQuit(
// Copies the results from DownloadActionCallback and quit the message loop.
// The contents of the download cache file are copied to a string, and the
// file is removed.
void CopyResultsFromDownloadActionCallbackAndQuit(
void CopyResultsFromDownloadActionCallback(
GDataErrorCode* out_result_code,
std::string* contents,
GDataErrorCode result_code,
Expand All @@ -52,14 +52,6 @@ void CopyResultsFromDownloadActionCallbackAndQuit(
MessageLoop::current()->Quit();
}

// Copies the result from EntryActionCallback and quit the message loop.
void CopyResultFromEntryActionCallbackAndQuit(
GDataErrorCode* out_result_code,
GDataErrorCode result_code) {
*out_result_code = result_code;
MessageLoop::current()->Quit();
}

// Returns true if |json_data| equals to JSON data in |expected_json_file_path|.
bool VerifyJsonData(const FilePath& expected_json_file_path,
const base::Value* json_data) {
Expand All @@ -72,37 +64,21 @@ bool VerifyJsonData(const FilePath& expected_json_file_path,
return base::Value::Equals(expected_data.get(), json_data);
}

// Returns a HttpResponse created from the given file path.
scoped_ptr<test_server::HttpResponse> CreateHttpResponseFromFile(
// Returns a HttpResponse created from the given JSON file path.
scoped_ptr<test_server::HttpResponse> CreateJsonResponseFromFile(
const FilePath& file_path) {
std::string content;
if (!file_util::ReadFileToString(file_path, &content))
return scoped_ptr<test_server::HttpResponse>();

std::string content_type = "text/plain";
if (EndsWith(file_path.value(), ".json", true /* case sensitive */))
content_type = "application/json";

scoped_ptr<test_server::HttpResponse> http_response(
new test_server::HttpResponse);
http_response->set_code(test_server::SUCCESS);
http_response->set_content(content);
http_response->set_content_type(content_type);
http_response->set_content_type("application/json");
return http_response.Pass();
}

// Removes |prefix| from |input| and stores the result in |output|. Returns
// true if the prefix is removed.
bool RemovePrefix(const std::string& input,
const std::string& prefix,
std::string* output) {
if (!StartsWithASCII(input, prefix, true /* case sensitive */))
return false;

*output = input.substr(prefix.size());
return true;
}

// This class sets a request context getter for testing in
// |testing_browser_process| and then clears the state when an instance of it
// goes out of scope.
Expand Down Expand Up @@ -147,9 +123,16 @@ class GDataWapiOperationsTest : public testing::Test {
static_cast<TestingBrowserProcess*>(g_browser_process)));

ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady());
test_server_.RegisterRequestHandler(
base::Bind(&GDataWapiOperationsTest::HandleDownloadRequest,
base::Unretained(this)));
test_server_.RegisterFileResponse(
"/files/chromeos/gdata/testfile.txt",
test_util::GetTestFilePath("gdata/testfile.txt"),
"text/plain",
test_server::SUCCESS);
test_server_.RegisterFileResponse(
"/files/chromeos/gdata/root_feed.json",
test_util::GetTestFilePath("gdata/root_feed.json"),
"text/plain",
test_server::SUCCESS);
test_server_.RegisterRequestHandler(
base::Bind(&GDataWapiOperationsTest::HandleResourceFeedRequest,
base::Unretained(this)));
Expand All @@ -172,47 +155,34 @@ class GDataWapiOperationsTest : public testing::Test {
return profile_->GetPath().Append(file_name);
}

// Handles a request for downloading a file. Reads a file from the test
// directory and returns the content.
scoped_ptr<test_server::HttpResponse> HandleDownloadRequest(
const test_server::HttpRequest& request) {
http_request_ = request;

const GURL absolute_url = test_server_.GetURL(request.relative_url);
std::string remaining_path;
if (!RemovePrefix(absolute_url.path(), "/files/", &remaining_path))
return scoped_ptr<test_server::HttpResponse>();

return CreateHttpResponseFromFile(
test_util::GetTestFilePath(remaining_path));
}

// Handles a request for fetching a resource feed.
scoped_ptr<test_server::HttpResponse> HandleResourceFeedRequest(
const test_server::HttpRequest& request) {
http_request_ = request;

const GURL absolute_url = test_server_.GetURL(request.relative_url);
std::string remaining_path;
if (!RemovePrefix(absolute_url.path(),
"/feeds/default/private/full/",
&remaining_path)) {
const char kFeedPrefix[] = "/feeds/default/private/full/";
if (!StartsWithASCII(request.relative_url,
kFeedPrefix,
true /* case sensitive */)) {
return scoped_ptr<test_server::HttpResponse>();
}

if (remaining_path == "-/mine") {
const GURL absolute_url = test_server_.GetURL(request.relative_url);
// Remove the feed prefix.
std::string suffix = absolute_url.path();
ReplaceFirstSubstringAfterOffset(&suffix, 0, kFeedPrefix, "");

if (suffix == "-/mine") {
// Process the default feed.
return CreateHttpResponseFromFile(
return CreateJsonResponseFromFile(
test_util::GetTestFilePath("gdata/root_feed.json"));
} else {
// Process a feed for a single resource ID.
// For now, we only support a resource feed for a particular entry.
const std::string resource_id = net::UnescapeURLComponent(
remaining_path, net::UnescapeRule::URL_SPECIAL_CHARS);
suffix, net::UnescapeRule::URL_SPECIAL_CHARS);
if (resource_id != "file:2_file_resource_id")
return scoped_ptr<test_server::HttpResponse>();

return CreateHttpResponseFromFile(
return CreateJsonResponseFromFile(
test_util::GetTestFilePath("gdata/file_entry.json"));
}

Expand All @@ -223,13 +193,11 @@ class GDataWapiOperationsTest : public testing::Test {
// Handles a request for fetching a metadata feed.
scoped_ptr<test_server::HttpResponse> HandleMetadataFeedRequest(
const test_server::HttpRequest& request) {
http_request_ = request;

const GURL absolute_url = test_server_.GetURL(request.relative_url);
if (absolute_url.path() != "/feeds/metadata/default")
return scoped_ptr<test_server::HttpResponse>();

return CreateHttpResponseFromFile(
return CreateJsonResponseFromFile(
test_util::GetTestFilePath("gdata/account_metadata.json"));
}

Expand All @@ -242,11 +210,6 @@ class GDataWapiOperationsTest : public testing::Test {
OperationRegistry operation_registry_;
scoped_ptr<GDataWapiUrlGenerator> url_generator_;
scoped_ptr<ScopedRequestContextGetterForTesting> request_context_getter_;

// The incoming HTTP request is saved so tests can verify the request
// parameters like HTTP method (ex. some operations should use DELETE
// instead of GET).
test_server::HttpRequest http_request_;
};

} // namespace
Expand All @@ -270,7 +233,6 @@ TEST_F(GDataWapiOperationsTest, GetDocumentsOperation_DefaultFeed) {
MessageLoop::current()->Run();

EXPECT_EQ(HTTP_SUCCESS, result_code);
EXPECT_EQ(test_server::METHOD_GET, http_request_.method);
ASSERT_TRUE(result_data);
EXPECT_TRUE(VerifyJsonData(
test_util::GetTestFilePath("gdata/root_feed.json"),
Expand All @@ -284,7 +246,7 @@ TEST_F(GDataWapiOperationsTest, GetDocumentsOperation_ValidFeed) {
GetDocumentsOperation* operation = new GetDocumentsOperation(
&operation_registry_,
*url_generator_,
test_server_.GetURL("/files/gdata/root_feed.json"),
test_server_.GetURL("/files/chromeos/gdata/root_feed.json"),
0, // start changestamp
"", // search string
false, // shared with me
Expand All @@ -296,7 +258,6 @@ TEST_F(GDataWapiOperationsTest, GetDocumentsOperation_ValidFeed) {
MessageLoop::current()->Run();

EXPECT_EQ(HTTP_SUCCESS, result_code);
EXPECT_EQ(test_server::METHOD_GET, http_request_.method);
ASSERT_TRUE(result_data);
EXPECT_TRUE(VerifyJsonData(
test_util::GetTestFilePath("gdata/root_feed.json"),
Expand All @@ -312,7 +273,7 @@ TEST_F(GDataWapiOperationsTest, GetDocumentsOperation_InvalidFeed) {
GetDocumentsOperation* operation = new GetDocumentsOperation(
&operation_registry_,
*url_generator_,
test_server_.GetURL("/files/gdata/testfile.txt"),
test_server_.GetURL("/files/chromeos/gdata/testfile.txt"),
0, // start changestamp
"", // search string
false, // shared with me
Expand All @@ -324,7 +285,6 @@ TEST_F(GDataWapiOperationsTest, GetDocumentsOperation_InvalidFeed) {
MessageLoop::current()->Run();

EXPECT_EQ(GDATA_PARSE_ERROR, result_code);
EXPECT_EQ(test_server::METHOD_GET, http_request_.method);
EXPECT_FALSE(result_data);
}

Expand All @@ -343,7 +303,6 @@ TEST_F(GDataWapiOperationsTest, GetDocumentEntryOperation_ValidResourceId) {
MessageLoop::current()->Run();

EXPECT_EQ(HTTP_SUCCESS, result_code);
EXPECT_EQ(test_server::METHOD_GET, http_request_.method);
ASSERT_TRUE(result_data);
EXPECT_TRUE(VerifyJsonData(
test_util::GetTestFilePath("gdata/file_entry.json"),
Expand All @@ -365,7 +324,6 @@ TEST_F(GDataWapiOperationsTest, GetDocumentEntryOperation_InvalidResourceId) {
MessageLoop::current()->Run();

EXPECT_EQ(HTTP_NOT_FOUND, result_code);
EXPECT_EQ(test_server::METHOD_GET, http_request_.method);
ASSERT_FALSE(result_data);
}

Expand All @@ -384,7 +342,6 @@ TEST_F(GDataWapiOperationsTest, GetAccountMetadataOperation) {
MessageLoop::current()->Run();

EXPECT_EQ(HTTP_SUCCESS, result_code);
EXPECT_EQ(test_server::METHOD_GET, http_request_.method);
EXPECT_TRUE(VerifyJsonData(
test_util::GetTestFilePath("gdata/account_metadata.json"),
result_data.get()));
Expand All @@ -395,19 +352,17 @@ TEST_F(GDataWapiOperationsTest, DownloadFileOperation_ValidFile) {
std::string contents;
DownloadFileOperation* operation = new DownloadFileOperation(
&operation_registry_,
base::Bind(&CopyResultsFromDownloadActionCallbackAndQuit,
base::Bind(&CopyResultsFromDownloadActionCallback,
&result_code,
&contents),
GetContentCallback(),
test_server_.GetURL("/files/gdata/testfile.txt"),
test_server_.GetURL("/files/chromeos/gdata/testfile.txt"),
FilePath::FromUTF8Unsafe("/dummy/gdata/testfile.txt"),
GetTestCachedFilePath(FilePath::FromUTF8Unsafe("cached_testfile.txt")));
operation->Start(kTestGDataAuthToken, kTestUserAgent);
MessageLoop::current()->Run();

EXPECT_EQ(HTTP_SUCCESS, result_code);
EXPECT_EQ(test_server::METHOD_GET, http_request_.method);

const FilePath expected_path =
test_util::GetTestFilePath("gdata/testfile.txt");
std::string expected_contents;
Expand All @@ -420,39 +375,23 @@ TEST_F(GDataWapiOperationsTest, DownloadFileOperation_NonExistentFile) {
std::string contents;
DownloadFileOperation* operation = new DownloadFileOperation(
&operation_registry_,
base::Bind(&CopyResultsFromDownloadActionCallbackAndQuit,
base::Bind(&CopyResultsFromDownloadActionCallback,
&result_code,
&contents),
GetContentCallback(),
test_server_.GetURL("/files/gdata/no-such-file.txt"),
test_server_.GetURL("/files/chromeos/gdata/no-such-file.txt"),
FilePath::FromUTF8Unsafe("/dummy/gdata/no-such-file.txt"),
GetTestCachedFilePath(
FilePath::FromUTF8Unsafe("cache_no-such-file.txt")));
operation->Start(kTestGDataAuthToken, kTestUserAgent);
MessageLoop::current()->Run();

EXPECT_EQ(HTTP_NOT_FOUND, result_code);
EXPECT_EQ(test_server::METHOD_GET, http_request_.method);
// Do not verify the not found message.
}

TEST_F(GDataWapiOperationsTest, DeleteDocumentOperation) {
GDataErrorCode result_code = GDATA_OTHER_ERROR;

DeleteDocumentOperation* operation = new DeleteDocumentOperation(
&operation_registry_,
base::Bind(&CopyResultFromEntryActionCallbackAndQuit,
&result_code),
test_server_.GetURL(
"/feeds/default/private/full/file:2_file_resource_id"));

operation->Start(kTestGDataAuthToken, kTestUserAgent);
MessageLoop::current()->Run();

EXPECT_EQ(HTTP_SUCCESS, result_code);
EXPECT_EQ(test_server::METHOD_DELETE, http_request_.method);
EXPECT_EQ("*", http_request_.headers["If-Match"]);
}
// TODO(satorux): Write tests for DeleteDocumentOperation.
// crbug.com/162348

// TODO(satorux): Write tests for CreateDirectoryOperation.
// crbug.com/162348
Expand Down
6 changes: 4 additions & 2 deletions chrome/browser/google_apis/test_server/http_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ enum HttpMethod {
};

// Represents a HTTP request. Since it can be big, use scoped_ptr to pass it
// instead of copying. However, the struct is copyable so tests can save and
// examine a HTTP request.
// instead of copying.
struct HttpRequest {
HttpRequest();
~HttpRequest();
Expand All @@ -38,6 +37,9 @@ struct HttpRequest {
std::map<std::string, std::string> headers;
std::string content;
bool has_content;

private:
DISALLOW_COPY_AND_ASSIGN(HttpRequest);
};

// Parses the input data and produces a valid HttpRequest object. If there is
Expand Down

0 comments on commit 3a19f62

Please sign in to comment.