Chromium Code Reviews| Index: chrome/test/webdriver/commands/cookie_commands.cc |
| diff --git a/chrome/test/webdriver/commands/cookie_commands.cc b/chrome/test/webdriver/commands/cookie_commands.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..12b3f6ea5c329e872ecc6bbf79813cb0c468bc87 |
| --- /dev/null |
| +++ b/chrome/test/webdriver/commands/cookie_commands.cc |
| @@ -0,0 +1,174 @@ |
| +// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
|
kkania
2011/02/09 16:53:13
add newline here
|
| +#include "chrome/test/webdriver/commands/cookie_commands.h" |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/scoped_ptr.h" |
| +#include "base/string_util.h" |
| +#include "base/values.h" |
| + |
|
kkania
2011/02/09 16:53:13
delete newline here
|
| +#include "chrome/test/webdriver/cookie.h" |
| +#include "chrome/test/webdriver/session.h" |
| +#include "chrome/test/webdriver/commands/response.h" |
| + |
| +namespace webdriver { |
| + |
| +bool CookieCommand::Init(Response* const response) { |
| + if (WebDriverCommand::Init(response)) { |
| + if (session_->GetURL(¤t_url_)) { |
| + return true; |
| + } |
| + SET_WEBDRIVER_ERROR(response, "Failed to query current page URL", |
| + kInternalServerError); |
| + return false; |
| + } |
| + |
| + return false; |
| +} |
| + |
| +void CookieCommand::ExecuteGet(Response* const response) { |
| + // TODO(JMikhail): Add GetJSONCookies to automation proxy since |
| + // GetCookies does not return the necessary information |
| + std::string cookies; |
| + std::vector<std::string> tokens; |
| + |
| + if (!session_->GetCookies(current_url_, &cookies)) { |
| + response->set_status(kUnknownError); |
|
kkania
2011/02/09 16:53:13
use SET_WEBDRIVER_ERROR
|
| + response->set_value(new StringValue("Failed to fetch cookies")); |
| + return; |
| + } |
| + |
| + Tokenize(cookies, ";", &tokens); |
| + ListValue* cookie_list = new ListValue(); |
| + for (std::vector<std::string>::iterator i = tokens.begin(); |
| + i != tokens.end(); ++i) { |
| + Cookie cookie(*i); |
| + if (cookie.IsValid()) { |
| + cookie_list->Append(cookie.ToDictionary()); |
| + } else { |
| + LOG(ERROR) << "Failed to parse cookie: " << *i; |
| + } |
| + } |
| + |
| + response->set_status(kSuccess); |
| + response->set_value(cookie_list); |
| +} |
| + |
| +void CookieCommand::ExecutePost(Response* const response) { |
| + std::string cookie_string; |
| + DictionaryValue* cookie_dict; |
| + scoped_ptr<Cookie> cookie; |
| + |
| + // Check to see if the cookie was passed in as a string. |
| + if (GetStringASCIIParameter("cookie", &cookie_string)) { |
|
kkania
2011/02/09 16:53:13
i don't understand your reasoning for keeping this
|
| + cookie.reset(new Cookie(cookie_string)); |
| + } else { |
| + // Check to see if the cookie was passed in as a JSON onject. |
| + if (GetDictionaryParameter(std::string("cookie"), &cookie_dict)) { |
|
kkania
2011/02/09 16:53:13
unnecessary std::string here
|
| + cookie.reset(new Cookie(*cookie_dict)); |
| + } else { |
| + // No valid cookie sent. |
| + SET_WEBDRIVER_ERROR(response, "Cookie key not found", |
| + kBadRequest); |
| + return; |
| + } |
| + } |
| + |
| + // Make sure the cookie is formated preoperly. |
| + if (!cookie->IsValid()) { |
| + SET_WEBDRIVER_ERROR(response, "Invalid cookie", |
|
kkania
2011/02/09 16:53:13
indent messed up here and below
|
| + kBadRequest); |
| + return; |
| + } |
| + |
| + if (!session_->SetCookie(current_url_, cookie->ToString())) { |
| + SET_WEBDRIVER_ERROR(response, "Failed to set cookie", |
| + kUnableToSetCookie); |
| + return; |
| + } |
| + |
| + response->set_status(kSuccess); |
| + response->set_value(new StringValue(cookie->ToString())); |
| +} |
| + |
| +void CookieCommand::ExecuteDelete(Response* const response) { |
| + std::string cookies; |
| + std::vector<std::string> tokens; |
| + |
| + if (!session_->GetCookies(current_url_, &cookies)) { |
| + SET_WEBDRIVER_ERROR(response, "Failed to fetch cookies", |
| + kUnableToSetCookie); |
| + return; |
| + } |
| + |
| + Tokenize(cookies, ":", &tokens); |
| + for (std::vector<std::string>::iterator i = tokens.begin(); |
| + i != tokens.end(); ++i) { |
| + Cookie cookie(*i); |
| + if (cookie.IsValid()) { |
| + if (!session_->DeleteCookie(current_url_, cookie.get_name())) { |
| + VLOG(1) << "Could not delete cookie: " << cookie.get_name() << "\n" |
| + << "Contents of cookie: " << cookie.ToString(); |
|
kkania
2011/02/09 16:53:13
indent messed up here
|
| + SET_WEBDRIVER_ERROR(response, "Could not delete all cookies", |
| + kInternalServerError); |
| + return; |
| + } |
| + } else { |
| + LOG(ERROR) << "Failed to parse cookie: " << *i; |
| + } |
| + } |
| + |
| + response->set_status(kSuccess); |
| + response->set_value(new StringValue("Cookies delete")); |
| +} |
| + |
| +bool NamedCookieCommand::Init(Response* const response) { |
| + if (WebDriverCommand::Init(response)) { |
| + if (!session_->GetURL(¤t_url_)) { |
| + SET_WEBDRIVER_ERROR(response, "Failed to query current page URL", |
| + kInternalServerError); |
| + return false; |
| + } |
| + |
| + // There should be at least 5 segments to match |
| + // /session/:sessionId/cookie/:name |
| + cookie_name_ = GetPathVariable(4); |
| + SET_WEBDRIVER_ERROR(response, "No cookie name specified", |
|
kkania
2011/02/09 16:53:13
i think this needs to be surrounded by:
if (cookie
|
| + kBadRequest); |
| + return false; |
| + } |
| + |
| + SET_WEBDRIVER_ERROR(response, "Failed to init Named Cookie Command", |
|
kkania
2011/02/09 16:53:13
remove this
|
| + kInternalServerError); |
| + return false; |
| +} |
| + |
| +void NamedCookieCommand::ExecuteGet(Response* const response) { |
| + std::string cookie; |
| + |
| + if (!session_->GetCookieByName(current_url_, cookie_name_, &cookie)) { |
| + SET_WEBDRIVER_ERROR(response, "Failed to fetch cookie", |
|
kkania
2011/02/09 16:53:13
fix indent
|
| + kUnknownError); |
| + return; |
| + } |
| + |
| + response->set_status(kSuccess); |
| + response->set_value(new StringValue(cookie)); |
| +} |
| + |
| +void NamedCookieCommand::ExecuteDelete(Response* const response) { |
| + if (!session_->DeleteCookie(current_url_, cookie_name_)) { |
| + SET_WEBDRIVER_ERROR(response, "Failed to delete cookie", |
|
kkania
2011/02/09 16:53:13
fix indent
|
| + kUnknownError); |
| + return; |
| + } |
| + |
| + response->set_status(kSuccess); |
| + response->set_value(new StringValue("Cookie deleted")); |
| +} |
| + |
| +} // namespace webdriver |
| + |