OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/test/webdriver/commands/cookie_commands.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/scoped_ptr.h" |
| 11 #include "base/string_util.h" |
| 12 #include "base/values.h" |
| 13 #include "chrome/test/webdriver/cookie.h" |
| 14 #include "chrome/test/webdriver/session.h" |
| 15 #include "chrome/test/webdriver/session_manager.h" |
| 16 #include "chrome/test/webdriver/commands/response.h" |
| 17 |
| 18 namespace webdriver { |
| 19 |
| 20 bool CookieCommand::Init(Response* const response) { |
| 21 if (WebDriverCommand::Init(response)) { |
| 22 if (session_->GetURL(¤t_url_)) { |
| 23 return true; |
| 24 } |
| 25 SET_WEBDRIVER_ERROR(response, "Failed to query current page URL", |
| 26 kInternalServerError); |
| 27 return false; |
| 28 } |
| 29 |
| 30 return false; |
| 31 } |
| 32 |
| 33 void CookieCommand::ExecuteGet(Response* const response) { |
| 34 // TODO(JMikhail): Add GetJSONCookies to automation proxy since |
| 35 // GetCookies does not return the necessary information |
| 36 std::string cookies; |
| 37 std::vector<std::string> tokens; |
| 38 |
| 39 if (!session_->GetCookies(current_url_, &cookies)) { |
| 40 SET_WEBDRIVER_ERROR(response, "Failed to fetch cookies", |
| 41 kUnknownError); |
| 42 return; |
| 43 } |
| 44 |
| 45 Tokenize(cookies, ";", &tokens); |
| 46 ListValue* cookie_list = new ListValue(); |
| 47 for (std::vector<std::string>::iterator i = tokens.begin(); |
| 48 i != tokens.end(); ++i) { |
| 49 Cookie cookie(*i); |
| 50 if (cookie.IsValid()) { |
| 51 cookie_list->Append(cookie.ToDictionary()); |
| 52 } else { |
| 53 LOG(ERROR) << "Failed to parse cookie: " << *i; |
| 54 } |
| 55 } |
| 56 |
| 57 response->set_status(kSuccess); |
| 58 response->set_value(cookie_list); |
| 59 } |
| 60 |
| 61 void CookieCommand::ExecutePost(Response* const response) { |
| 62 std::string cookie_string; |
| 63 DictionaryValue* cookie_dict; |
| 64 scoped_ptr<Cookie> cookie; |
| 65 |
| 66 // Check to see if the cookie was passed in as a JSON onject. |
| 67 if (GetDictionaryParameter("cookie", &cookie_dict)) { |
| 68 cookie.reset(new Cookie(*cookie_dict)); |
| 69 } else { |
| 70 // No valid cookie sent. |
| 71 SET_WEBDRIVER_ERROR(response, "Cookie key not found", |
| 72 kBadRequest); |
| 73 return; |
| 74 } |
| 75 |
| 76 // Make sure the cookie is formated preoperly. |
| 77 if (!cookie->IsValid()) { |
| 78 SET_WEBDRIVER_ERROR(response, "Invalid cookie", |
| 79 kBadRequest); |
| 80 return; |
| 81 } |
| 82 |
| 83 if (!session_->SetCookie(current_url_, cookie->ToString())) { |
| 84 SET_WEBDRIVER_ERROR(response, "Failed to set cookie", |
| 85 kUnableToSetCookie); |
| 86 return; |
| 87 } |
| 88 |
| 89 response->set_status(kSuccess); |
| 90 response->set_value(new StringValue(cookie->ToString())); |
| 91 } |
| 92 |
| 93 void CookieCommand::ExecuteDelete(Response* const response) { |
| 94 std::string cookies; |
| 95 std::vector<std::string> tokens; |
| 96 |
| 97 if (!session_->GetCookies(current_url_, &cookies)) { |
| 98 SET_WEBDRIVER_ERROR(response, "Failed to fetch cookies", |
| 99 kUnableToSetCookie); |
| 100 return; |
| 101 } |
| 102 |
| 103 Tokenize(cookies, ":", &tokens); |
| 104 for (std::vector<std::string>::iterator i = tokens.begin(); |
| 105 i != tokens.end(); ++i) { |
| 106 Cookie cookie(*i); |
| 107 if (cookie.IsValid()) { |
| 108 if (!session_->DeleteCookie(current_url_, cookie.get_name())) { |
| 109 VLOG(1) << "Could not delete cookie: " << cookie.get_name() << "\n" |
| 110 << "Contents of cookie: " << cookie.ToString(); |
| 111 SET_WEBDRIVER_ERROR(response, "Could not delete all cookies", |
| 112 kInternalServerError); |
| 113 return; |
| 114 } |
| 115 } else { |
| 116 LOG(ERROR) << "Failed to parse cookie: " << *i; |
| 117 } |
| 118 } |
| 119 |
| 120 response->set_status(kSuccess); |
| 121 response->set_value(new StringValue("Cookies delete")); |
| 122 } |
| 123 |
| 124 bool NamedCookieCommand::Init(Response* const response) { |
| 125 if (WebDriverCommand::Init(response)) { |
| 126 if (!session_->GetURL(¤t_url_)) { |
| 127 SET_WEBDRIVER_ERROR(response, "Failed to query current page URL", |
| 128 kInternalServerError); |
| 129 return false; |
| 130 } |
| 131 |
| 132 // There should be at least 5 segments to match |
| 133 // /session/:sessionId/cookie/:name |
| 134 cookie_name_ = GetPathVariable(4); |
| 135 SET_WEBDRIVER_ERROR(response, "No cookie name specified", |
| 136 kBadRequest); |
| 137 return false; |
| 138 } |
| 139 |
| 140 SET_WEBDRIVER_ERROR(response, "Failed to init Named Cookie Command", |
| 141 kInternalServerError); |
| 142 return false; |
| 143 } |
| 144 |
| 145 void NamedCookieCommand::ExecuteGet(Response* const response) { |
| 146 std::string cookie; |
| 147 |
| 148 if (!session_->GetCookieByName(current_url_, cookie_name_, &cookie)) { |
| 149 SET_WEBDRIVER_ERROR(response, "Failed to fetch cookie", |
| 150 kUnknownError); |
| 151 return; |
| 152 } |
| 153 |
| 154 response->set_status(kSuccess); |
| 155 response->set_value(new StringValue(cookie)); |
| 156 } |
| 157 |
| 158 void NamedCookieCommand::ExecuteDelete(Response* const response) { |
| 159 if (!session_->DeleteCookie(current_url_, cookie_name_)) { |
| 160 SET_WEBDRIVER_ERROR(response, "Failed to delete cookie", |
| 161 kUnknownError); |
| 162 return; |
| 163 } |
| 164 |
| 165 response->set_status(kSuccess); |
| 166 response->set_value(new StringValue("Cookie deleted")); |
| 167 } |
| 168 |
| 169 } // namespace webdriver |
| 170 |
OLD | NEW |