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