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 // Note that ';' separates cookies while ':' separates name/value pairs. |
| 46 Tokenize(cookies, ";", &tokens); |
| 47 scoped_ptr<ListValue> cookie_list(new ListValue()); |
| 48 for (std::vector<std::string>::iterator i = tokens.begin(); |
| 49 i != tokens.end(); ++i) { |
| 50 Cookie cookie(*i); |
| 51 if (cookie.valid()) { |
| 52 cookie_list->Append(cookie.ToDictionary()); |
| 53 } else { |
| 54 LOG(ERROR) << "Failed to parse cookie: " << *i; |
| 55 SET_WEBDRIVER_ERROR(response, "Could not get all cookies", |
| 56 kInternalServerError); |
| 57 return; |
| 58 } |
| 59 } |
| 60 |
| 61 response->set_status(kSuccess); |
| 62 response->set_value(cookie_list.release()); |
| 63 } |
| 64 |
| 65 void CookieCommand::ExecutePost(Response* const response) { |
| 66 std::string cookie_string; |
| 67 DictionaryValue* cookie_dict; |
| 68 |
| 69 // Check to see if the cookie was passed in as a JSON onject. |
| 70 if (!GetDictionaryParameter("cookie", &cookie_dict)) { |
| 71 // No valid cookie sent. |
| 72 SET_WEBDRIVER_ERROR(response, "Cookie key not found", |
| 73 kBadRequest); |
| 74 return; |
| 75 } |
| 76 Cookie cookie(*cookie_dict); |
| 77 |
| 78 // Make sure the cookie is formated preoperly. |
| 79 if (!cookie.valid()) { |
| 80 SET_WEBDRIVER_ERROR(response, "Invalid cookie", |
| 81 kBadRequest); |
| 82 return; |
| 83 } |
| 84 |
| 85 if (!session_->SetCookie(current_url_, cookie.ToString())) { |
| 86 SET_WEBDRIVER_ERROR(response, "Failed to set cookie", |
| 87 kUnableToSetCookie); |
| 88 return; |
| 89 } |
| 90 |
| 91 response->set_status(kSuccess); |
| 92 response->set_value(new StringValue(cookie.ToString())); |
| 93 } |
| 94 |
| 95 void CookieCommand::ExecuteDelete(Response* const response) { |
| 96 std::string cookies; |
| 97 std::vector<std::string> tokens; |
| 98 |
| 99 if (!session_->GetCookies(current_url_, &cookies)) { |
| 100 SET_WEBDRIVER_ERROR(response, "Failed to fetch cookies", |
| 101 kUnableToSetCookie); |
| 102 return; |
| 103 } |
| 104 |
| 105 Tokenize(cookies, ":", &tokens); |
| 106 for (std::vector<std::string>::iterator i = tokens.begin(); |
| 107 i != tokens.end(); ++i) { |
| 108 Cookie cookie(*i); |
| 109 if (cookie.valid()) { |
| 110 if (!session_->DeleteCookie(current_url_, cookie.name())) { |
| 111 VLOG(1) << "Could not delete cookie: " << cookie.name() << "\n" |
| 112 << "Contents of cookie: " << cookie.ToString(); |
| 113 SET_WEBDRIVER_ERROR(response, "Could not delete all cookies", |
| 114 kInternalServerError); |
| 115 return; |
| 116 } |
| 117 } else { |
| 118 LOG(ERROR) << "Failed to parse cookie: " << *i; |
| 119 SET_WEBDRIVER_ERROR(response, "Could not delete all cookies", |
| 120 kInternalServerError); |
| 121 return; |
| 122 } |
| 123 } |
| 124 |
| 125 response->set_status(kSuccess); |
| 126 } |
| 127 |
| 128 bool NamedCookieCommand::Init(Response* const response) { |
| 129 if (WebDriverCommand::Init(response)) { |
| 130 if (!session_->GetURL(¤t_url_)) { |
| 131 SET_WEBDRIVER_ERROR(response, "Failed to query current page URL", |
| 132 kInternalServerError); |
| 133 return false; |
| 134 } |
| 135 |
| 136 // There should be at least 5 segments to match |
| 137 // /session/:sessionId/cookie/:name |
| 138 cookie_name_ = GetPathVariable(4); |
| 139 if (cookie_name_ == "") { |
| 140 SET_WEBDRIVER_ERROR(response, "No cookie name specified", |
| 141 kBadRequest); |
| 142 return false; |
| 143 } |
| 144 |
| 145 return true; |
| 146 } |
| 147 |
| 148 return false; |
| 149 } |
| 150 |
| 151 void NamedCookieCommand::ExecuteGet(Response* const response) { |
| 152 std::string cookie; |
| 153 |
| 154 if (!session_->GetCookieByName(current_url_, cookie_name_, &cookie)) { |
| 155 SET_WEBDRIVER_ERROR(response, "Failed to fetch cookie", |
| 156 kUnknownError); |
| 157 return; |
| 158 } |
| 159 |
| 160 response->set_status(kSuccess); |
| 161 response->set_value(new StringValue(cookie)); |
| 162 } |
| 163 |
| 164 void NamedCookieCommand::ExecuteDelete(Response* const response) { |
| 165 if (!session_->DeleteCookie(current_url_, cookie_name_)) { |
| 166 SET_WEBDRIVER_ERROR(response, "Failed to delete cookie", |
| 167 kUnknownError); |
| 168 return; |
| 169 } |
| 170 |
| 171 response->set_status(kSuccess); |
| 172 } |
| 173 |
| 174 } // namespace webdriver |
| 175 |
OLD | NEW |