Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Side by Side Diff: chrome/test/webdriver/commands/cookie_commands.cc

Issue 6330012: Cookie commands for the webdriver protocol (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: kdsodkoskdoskdokosdksdkso Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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.
kkania 2011/02/09 16:53:13 add newline here
4 #include "chrome/test/webdriver/commands/cookie_commands.h"
5
6 #include <string>
7 #include <vector>
8
9 #include "base/scoped_ptr.h"
10 #include "base/string_util.h"
11 #include "base/values.h"
12
kkania 2011/02/09 16:53:13 delete newline here
13 #include "chrome/test/webdriver/cookie.h"
14 #include "chrome/test/webdriver/session.h"
15 #include "chrome/test/webdriver/commands/response.h"
16
17 namespace webdriver {
18
19 bool CookieCommand::Init(Response* const response) {
20 if (WebDriverCommand::Init(response)) {
21 if (session_->GetURL(&current_url_)) {
22 return true;
23 }
24 SET_WEBDRIVER_ERROR(response, "Failed to query current page URL",
25 kInternalServerError);
26 return false;
27 }
28
29 return false;
30 }
31
32 void CookieCommand::ExecuteGet(Response* const response) {
33 // TODO(JMikhail): Add GetJSONCookies to automation proxy since
34 // GetCookies does not return the necessary information
35 std::string cookies;
36 std::vector<std::string> tokens;
37
38 if (!session_->GetCookies(current_url_, &cookies)) {
39 response->set_status(kUnknownError);
kkania 2011/02/09 16:53:13 use SET_WEBDRIVER_ERROR
40 response->set_value(new StringValue("Failed to fetch cookies"));
41 return;
42 }
43
44 Tokenize(cookies, ";", &tokens);
45 ListValue* cookie_list = new ListValue();
46 for (std::vector<std::string>::iterator i = tokens.begin();
47 i != tokens.end(); ++i) {
48 Cookie cookie(*i);
49 if (cookie.IsValid()) {
50 cookie_list->Append(cookie.ToDictionary());
51 } else {
52 LOG(ERROR) << "Failed to parse cookie: " << *i;
53 }
54 }
55
56 response->set_status(kSuccess);
57 response->set_value(cookie_list);
58 }
59
60 void CookieCommand::ExecutePost(Response* const response) {
61 std::string cookie_string;
62 DictionaryValue* cookie_dict;
63 scoped_ptr<Cookie> cookie;
64
65 // Check to see if the cookie was passed in as a string.
66 if (GetStringASCIIParameter("cookie", &cookie_string)) {
kkania 2011/02/09 16:53:13 i don't understand your reasoning for keeping this
67 cookie.reset(new Cookie(cookie_string));
68 } else {
69 // Check to see if the cookie was passed in as a JSON onject.
70 if (GetDictionaryParameter(std::string("cookie"), &cookie_dict)) {
kkania 2011/02/09 16:53:13 unnecessary std::string here
71 cookie.reset(new Cookie(*cookie_dict));
72 } else {
73 // No valid cookie sent.
74 SET_WEBDRIVER_ERROR(response, "Cookie key not found",
75 kBadRequest);
76 return;
77 }
78 }
79
80 // Make sure the cookie is formated preoperly.
81 if (!cookie->IsValid()) {
82 SET_WEBDRIVER_ERROR(response, "Invalid cookie",
kkania 2011/02/09 16:53:13 indent messed up here and below
83 kBadRequest);
84 return;
85 }
86
87 if (!session_->SetCookie(current_url_, cookie->ToString())) {
88 SET_WEBDRIVER_ERROR(response, "Failed to set cookie",
89 kUnableToSetCookie);
90 return;
91 }
92
93 response->set_status(kSuccess);
94 response->set_value(new StringValue(cookie->ToString()));
95 }
96
97 void CookieCommand::ExecuteDelete(Response* const response) {
98 std::string cookies;
99 std::vector<std::string> tokens;
100
101 if (!session_->GetCookies(current_url_, &cookies)) {
102 SET_WEBDRIVER_ERROR(response, "Failed to fetch cookies",
103 kUnableToSetCookie);
104 return;
105 }
106
107 Tokenize(cookies, ":", &tokens);
108 for (std::vector<std::string>::iterator i = tokens.begin();
109 i != tokens.end(); ++i) {
110 Cookie cookie(*i);
111 if (cookie.IsValid()) {
112 if (!session_->DeleteCookie(current_url_, cookie.get_name())) {
113 VLOG(1) << "Could not delete cookie: " << cookie.get_name() << "\n"
114 << "Contents of cookie: " << cookie.ToString();
kkania 2011/02/09 16:53:13 indent messed up here
115 SET_WEBDRIVER_ERROR(response, "Could not delete all cookies",
116 kInternalServerError);
117 return;
118 }
119 } else {
120 LOG(ERROR) << "Failed to parse cookie: " << *i;
121 }
122 }
123
124 response->set_status(kSuccess);
125 response->set_value(new StringValue("Cookies delete"));
126 }
127
128 bool NamedCookieCommand::Init(Response* const response) {
129 if (WebDriverCommand::Init(response)) {
130 if (!session_->GetURL(&current_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 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
140 kBadRequest);
141 return false;
142 }
143
144 SET_WEBDRIVER_ERROR(response, "Failed to init Named Cookie Command",
kkania 2011/02/09 16:53:13 remove this
145 kInternalServerError);
146 return false;
147 }
148
149 void NamedCookieCommand::ExecuteGet(Response* const response) {
150 std::string cookie;
151
152 if (!session_->GetCookieByName(current_url_, cookie_name_, &cookie)) {
153 SET_WEBDRIVER_ERROR(response, "Failed to fetch cookie",
kkania 2011/02/09 16:53:13 fix indent
154 kUnknownError);
155 return;
156 }
157
158 response->set_status(kSuccess);
159 response->set_value(new StringValue(cookie));
160 }
161
162 void NamedCookieCommand::ExecuteDelete(Response* const response) {
163 if (!session_->DeleteCookie(current_url_, cookie_name_)) {
164 SET_WEBDRIVER_ERROR(response, "Failed to delete cookie",
kkania 2011/02/09 16:53:13 fix indent
165 kUnknownError);
166 return;
167 }
168
169 response->set_status(kSuccess);
170 response->set_value(new StringValue("Cookie deleted"));
171 }
172
173 } // namespace webdriver
174
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698