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 "base/string_util.h" | |
6 | |
7 #include "chrome/test/webdriver/commands/cookie_commands.h" | |
kkania
2011/02/08 18:35:22
wrong include order. the format is:
#include "chro
Joe
2011/02/09 06:32:10
Done.
| |
8 #include "chrome/test/webdriver/cookie.h" | |
9 | |
10 #include <string> | |
11 #include <vector> | |
kkania
2011/02/08 18:35:22
you need to add to your includes:
#include "base/s
Joe
2011/02/09 06:32:10
Done.
| |
12 | |
13 namespace webdriver { | |
14 | |
15 bool CookieCommand::Init(Response* const response) { | |
16 if (WebDriverCommand::Init(response)) { | |
17 if (session_->GetURL(¤t_url_)) { | |
kkania
2011/02/08 18:35:22
I think this is broken. The GetURL function takes
Joe
2011/02/09 06:32:10
It's not broken it uses void GetGURL(GURL* gurl, b
| |
18 return true; | |
19 } | |
20 SET_WEBDRIVER_ERROR(response, "Failed to query current page URL", | |
21 kInternalServerError); | |
22 return false; | |
23 } | |
24 | |
25 SET_WEBDRIVER_ERROR(response, "Failed to init Cookie Command", | |
kkania
2011/02/08 18:35:22
You don't need this. Use the error set from WebDri
Joe
2011/02/09 06:32:10
Done.
| |
26 kInternalServerError); | |
27 return false; | |
28 } | |
29 | |
30 void CookieCommand::ExecuteGet(Response* const response) { | |
31 // TODO(JMikhail): Add GetJSONCookies to automation proxy since | |
32 // GetCookies does not return the necessary information | |
33 std::string cookies; | |
34 std::vector<std::string> tokens; | |
35 | |
36 if (!session_->GetCookies(current_url_, &cookies)) { | |
37 response->set_status(kUnknownError); | |
38 response->set_value(new StringValue("Failed to fetch cookies")); | |
39 return; | |
40 } | |
41 | |
42 Tokenize(cookies, std::string(";"), &tokens); | |
kkania
2011/02/08 18:35:22
std::string is unnecessary here, just do ";"
Joe
2011/02/09 06:32:10
Done.
| |
43 ListValue* l = new ListValue(); | |
kkania
2011/02/08 18:35:22
usually the only time people accept a one characte
Joe
2011/02/09 06:32:10
Done.
| |
44 for (std::vector<std::string>::iterator i = tokens.begin(); | |
45 i != tokens.end(); ++i) { | |
46 Cookie cookie(*i); | |
47 if (cookie.IsValid()) { | |
48 l->Append(cookie.ToJSON()); | |
49 } else { | |
50 LOG(ERROR) << "Failed to parse cookie: " << *i; | |
51 } | |
52 } | |
53 | |
54 response->set_status(kSuccess); | |
55 response->set_value(l); | |
56 } | |
57 | |
58 void CookieCommand::ExecutePost(Response* const response) { | |
59 std::string cookie_string; | |
60 DictionaryValue* cookie_dict; | |
61 scoped_ptr<Cookie> cookie; | |
62 | |
63 // Check to see if the cookie was passed in as a string. | |
64 if (GetStringASCIIParameter("cookie", &cookie_string)) { | |
kkania
2011/02/08 18:35:22
get rid of this and follow the spec, which says th
Joe
2011/02/09 06:32:10
It does i just support one more format since it ne
| |
65 cookie.reset(new Cookie(cookie_string)); | |
66 } else { | |
67 // Check to see if the cookie was passed in as a JSON onject. | |
68 if (GetDictionaryParameter("cookie", &cookie_dict)) { | |
69 cookie.reset(new Cookie(*cookie_dict)); | |
70 } else { | |
71 // No valid cookie sent. | |
72 response->set_value(new StringValue("Cookie key not found")); | |
kkania
2011/02/08 18:35:22
Use SET_WEBDRIVER_ERROR
Joe
2011/02/09 06:32:10
Done.
| |
73 response->set_status(kBadRequest); | |
74 return; | |
75 } | |
76 } | |
77 | |
78 // Make sure the cookie is formated preoperly. | |
79 if (!cookie->IsValid()) { | |
80 response->set_value(new StringValue("Invalid cookie")); | |
kkania
2011/02/08 18:35:22
use SET_WEBDRIVER_ERROR
Joe
2011/02/09 06:32:10
Done.
| |
81 response->set_status(kBadRequest); | |
82 return; | |
83 } | |
84 | |
85 if (!session_->SetCookie(current_url_, cookie->ToString())) { | |
86 response->set_status(kUnableToSetCookie); | |
kkania
2011/02/08 18:35:22
use SET_WEBDRIVER_ERROR
Joe
2011/02/09 06:32:10
Done.
| |
87 response->set_value(new StringValue("Failed to set cookie")); | |
88 return; | |
89 } | |
90 | |
91 response->set_status(kSuccess); | |
92 response->set_value(new StringValue(cookie->ToString())); | |
kkania
2011/02/08 18:35:22
the spec does not list a return value
Joe
2011/02/09 06:32:10
I set the value for my own debugging, it's ignored
| |
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 response->set_status(kUnknownError); | |
kkania
2011/02/08 18:35:22
SET_WEBDRIVER_ERR
Joe
2011/02/09 06:32:10
Done.
| |
101 response->set_value(new StringValue("Failed to fetch cookies")); | |
102 return; | |
103 } | |
104 | |
105 Tokenize(cookies, std::string(":"), &tokens); | |
kkania
2011/02/08 18:35:22
std::string is unnecessary
Joe
2011/02/09 06:32:10
Done.
| |
106 for (std::vector<std::string>::iterator i = tokens.begin(); | |
107 i != tokens.end(); ++i) { | |
108 Cookie cookie(*i); | |
109 if (cookie.IsValid()) { | |
110 if (!session_->DeleteCookie(current_url_, cookie.get_name())) { | |
111 LOG(INFO) << "Could not delete cookie: " << cookie.get_name() << "\n" | |
kkania
2011/02/08 18:35:22
use VLOG(1) instead of LOG(INFO)
Joe
2011/02/09 06:32:10
Done.
| |
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 } | |
120 } | |
121 | |
122 response->set_status(kSuccess); | |
123 response->set_value(new StringValue("Cookies delete")); | |
124 } | |
125 | |
126 bool NamedCookieCommand::Init(Response* const response) { | |
127 if (WebDriverCommand::Init(response)) { | |
128 if (!session_->GetURL(¤t_url_)) { | |
129 SET_WEBDRIVER_ERROR(response, "Failed to query current page URL", | |
130 kInternalServerError); | |
131 return false; | |
132 } | |
133 | |
134 // There should be at least 5 segments to match | |
135 // /session/:sessionId/cookie/:name | |
136 cookie_name_ = GetPathVariable(4); | |
137 SET_WEBDRIVER_ERROR(response, "No cookie name specified", | |
138 kBadRequest); | |
139 return false; | |
140 } | |
141 | |
142 SET_WEBDRIVER_ERROR(response, "Failed to init Named Cookie Command", | |
143 kInternalServerError); | |
144 return false; | |
145 } | |
146 | |
147 void NamedCookieCommand::ExecuteGet(Response* const response) { | |
kkania
2011/02/08 18:35:22
GET isn't listed in the spec for named cookie. Why
Joe
2011/02/09 06:32:10
I added it for completeness and would like to also
| |
148 std::string cookie; | |
149 | |
150 if (!session_->GetCookieByName(current_url_, cookie_name_, &cookie)) { | |
151 response->set_status(kUnknownError); | |
152 response->set_value(new StringValue("Failed to fetch cookie")); | |
153 return; | |
154 } | |
155 | |
156 response->set_status(kSuccess); | |
157 response->set_value(new StringValue(cookie)); | |
158 } | |
159 | |
160 void NamedCookieCommand::ExecuteDelete(Response* const response) { | |
161 if (!session_->DeleteCookie(current_url_, cookie_name_)) { | |
162 response->set_status(kUnknownError); | |
kkania
2011/02/08 18:35:22
use SET_WEBDRIVER_ERROR
Joe
2011/02/09 06:32:10
Done.
| |
163 response->set_value(new StringValue("Failed to delete cookie")); | |
164 return; | |
165 } | |
166 | |
167 response->set_status(kSuccess); | |
168 response->set_value(new StringValue("Cookie deleted")); | |
169 } | |
170 | |
171 } // namespace webdriver | |
172 | |
OLD | NEW |