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