OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/login/mock_url_fetchers.h" | 5 #include "chrome/browser/chromeos/login/mock_url_fetchers.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
12 #include "chrome/common/net/http_return.h" | 12 #include "chrome/common/net/http_return.h" |
13 #include "content/browser/browser_thread.h" | 13 #include "content/browser/browser_thread.h" |
14 #include "content/common/net/url_fetcher.h" | 14 #include "content/public/common/url_fetcher_delegate.h" |
15 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
16 #include "net/url_request/url_request_status.h" | 16 #include "net/url_request/url_request_status.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 namespace chromeos { | 19 namespace chromeos { |
20 | 20 |
21 ExpectCanceledFetcher::ExpectCanceledFetcher( | 21 ExpectCanceledFetcher::ExpectCanceledFetcher( |
22 bool success, | 22 bool success, |
23 const GURL& url, | 23 const GURL& url, |
24 const std::string& results, | 24 const std::string& results, |
25 URLFetcher::RequestType request_type, | 25 URLFetcher::RequestType request_type, |
26 URLFetcher::Delegate* d) | 26 content::URLFetcherDelegate* d) |
27 : URLFetcher(url, request_type, d), | 27 : URLFetcher(url, request_type, d), |
28 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 28 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
29 } | 29 } |
30 | 30 |
31 ExpectCanceledFetcher::~ExpectCanceledFetcher() { | 31 ExpectCanceledFetcher::~ExpectCanceledFetcher() { |
32 } | 32 } |
33 | 33 |
34 void ExpectCanceledFetcher::Start() { | 34 void ExpectCanceledFetcher::Start() { |
35 MessageLoop::current()->PostDelayedTask( | 35 MessageLoop::current()->PostDelayedTask( |
36 FROM_HERE, | 36 FROM_HERE, |
37 base::Bind(&ExpectCanceledFetcher::CompleteFetch, | 37 base::Bind(&ExpectCanceledFetcher::CompleteFetch, |
38 weak_factory_.GetWeakPtr()), | 38 weak_factory_.GetWeakPtr()), |
39 100); | 39 100); |
40 } | 40 } |
41 | 41 |
42 void ExpectCanceledFetcher::CompleteFetch() { | 42 void ExpectCanceledFetcher::CompleteFetch() { |
43 ADD_FAILURE() << "Fetch completed in ExpectCanceledFetcher!"; | 43 ADD_FAILURE() << "Fetch completed in ExpectCanceledFetcher!"; |
44 MessageLoop::current()->Quit(); // Allow exiting even if we mess up. | 44 MessageLoop::current()->Quit(); // Allow exiting even if we mess up. |
45 } | 45 } |
46 | 46 |
47 GotCanceledFetcher::GotCanceledFetcher(bool success, | 47 GotCanceledFetcher::GotCanceledFetcher(bool success, |
48 const GURL& url, | 48 const GURL& url, |
49 const std::string& results, | 49 const std::string& results, |
50 URLFetcher::RequestType request_type, | 50 URLFetcher::RequestType request_type, |
51 URLFetcher::Delegate* d) | 51 content::URLFetcherDelegate* d) |
52 : URLFetcher(url, request_type, d), | 52 : URLFetcher(url, request_type, d), |
53 url_(url) { | 53 url_(url), |
| 54 status_(net::URLRequestStatus::CANCELED, 0) { |
54 } | 55 } |
55 | 56 |
56 GotCanceledFetcher::~GotCanceledFetcher() {} | 57 GotCanceledFetcher::~GotCanceledFetcher() {} |
57 | 58 |
58 void GotCanceledFetcher::Start() { | 59 void GotCanceledFetcher::Start() { |
59 net::URLRequestStatus status; | 60 delegate()->OnURLFetchComplete(this); |
60 status.set_status(net::URLRequestStatus::CANCELED); | 61 } |
61 delegate()->OnURLFetchComplete(this, | 62 |
62 url_, | 63 const GURL& GotCanceledFetcher::url() const { |
63 status, | 64 return url_; |
64 RC_FORBIDDEN, | 65 } |
65 net::ResponseCookies(), | 66 |
66 std::string()); | 67 const net::URLRequestStatus& GotCanceledFetcher::status() const { |
| 68 return status_; |
| 69 } |
| 70 |
| 71 int GotCanceledFetcher::response_code() const { |
| 72 return RC_FORBIDDEN; |
67 } | 73 } |
68 | 74 |
69 SuccessFetcher::SuccessFetcher(bool success, | 75 SuccessFetcher::SuccessFetcher(bool success, |
70 const GURL& url, | 76 const GURL& url, |
71 const std::string& results, | 77 const std::string& results, |
72 URLFetcher::RequestType request_type, | 78 URLFetcher::RequestType request_type, |
73 URLFetcher::Delegate* d) | 79 content::URLFetcherDelegate* d) |
74 : URLFetcher(url, request_type, d), | 80 : URLFetcher(url, request_type, d), |
75 url_(url) { | 81 url_(url), |
| 82 status_(net::URLRequestStatus::SUCCESS, 0) { |
76 } | 83 } |
77 | 84 |
78 SuccessFetcher::~SuccessFetcher() {} | 85 SuccessFetcher::~SuccessFetcher() {} |
79 | 86 |
80 void SuccessFetcher::Start() { | 87 void SuccessFetcher::Start() { |
81 net::URLRequestStatus success(net::URLRequestStatus::SUCCESS, 0); | 88 delegate()->OnURLFetchComplete(this); |
82 delegate()->OnURLFetchComplete(this, | 89 } |
83 url_, | 90 |
84 success, | 91 const GURL& SuccessFetcher::url() const { |
85 RC_REQUEST_OK, | 92 return url_; |
86 net::ResponseCookies(), | 93 } |
87 std::string()); | 94 |
| 95 const net::URLRequestStatus& SuccessFetcher::status() const { |
| 96 return status_; |
| 97 } |
| 98 |
| 99 int SuccessFetcher::response_code() const { |
| 100 return RC_REQUEST_OK; |
88 } | 101 } |
89 | 102 |
90 FailFetcher::FailFetcher(bool success, | 103 FailFetcher::FailFetcher(bool success, |
91 const GURL& url, | 104 const GURL& url, |
92 const std::string& results, | 105 const std::string& results, |
93 URLFetcher::RequestType request_type, | 106 URLFetcher::RequestType request_type, |
94 URLFetcher::Delegate* d) | 107 content::URLFetcherDelegate* d) |
95 : URLFetcher(url, request_type, d), | 108 : URLFetcher(url, request_type, d), |
96 url_(url) { | 109 url_(url), |
| 110 status_(net::URLRequestStatus::FAILED, ECONNRESET) { |
97 } | 111 } |
98 | 112 |
99 FailFetcher::~FailFetcher() {} | 113 FailFetcher::~FailFetcher() {} |
100 | 114 |
101 void FailFetcher::Start() { | 115 void FailFetcher::Start() { |
102 net::URLRequestStatus failed(net::URLRequestStatus::FAILED, ECONNRESET); | 116 delegate()->OnURLFetchComplete(this); |
103 delegate()->OnURLFetchComplete(this, | 117 } |
104 url_, | 118 |
105 failed, | 119 const GURL& FailFetcher::url() const { |
106 RC_REQUEST_OK, | 120 return url_; |
107 net::ResponseCookies(), | 121 } |
108 std::string()); | 122 |
| 123 const net::URLRequestStatus& FailFetcher::status() const { |
| 124 return status_; |
| 125 } |
| 126 |
| 127 int FailFetcher::response_code() const { |
| 128 return RC_REQUEST_OK; |
109 } | 129 } |
110 | 130 |
111 // static | 131 // static |
112 const char CaptchaFetcher::kCaptchaToken[] = "token"; | 132 const char CaptchaFetcher::kCaptchaToken[] = "token"; |
113 // static | 133 // static |
114 const char CaptchaFetcher::kCaptchaUrlBase[] = "http://www.google.com/accounts/"
; | 134 const char CaptchaFetcher::kCaptchaUrlBase[] = "http://www.google.com/accounts/"
; |
115 // static | 135 // static |
116 const char CaptchaFetcher::kCaptchaUrlFragment[] = "fragment"; | 136 const char CaptchaFetcher::kCaptchaUrlFragment[] = "fragment"; |
117 // static | 137 // static |
118 const char CaptchaFetcher::kUnlockUrl[] = "http://what.ever"; | 138 const char CaptchaFetcher::kUnlockUrl[] = "http://what.ever"; |
119 | 139 |
120 | 140 |
121 CaptchaFetcher::CaptchaFetcher(bool success, | 141 CaptchaFetcher::CaptchaFetcher(bool success, |
122 const GURL& url, | 142 const GURL& url, |
123 const std::string& results, | 143 const std::string& results, |
124 URLFetcher::RequestType request_type, | 144 URLFetcher::RequestType request_type, |
125 URLFetcher::Delegate* d) | 145 content::URLFetcherDelegate* d) |
126 : URLFetcher(url, request_type, d), | 146 : URLFetcher(url, request_type, d), |
127 url_(url) { | 147 url_(url), |
| 148 status_(net::URLRequestStatus::SUCCESS, 0) { |
| 149 data_ = base::StringPrintf("Error=%s\n" |
| 150 "Url=%s\n" |
| 151 "CaptchaUrl=%s\n" |
| 152 "CaptchaToken=%s\n", |
| 153 "CaptchaRequired", |
| 154 kUnlockUrl, |
| 155 kCaptchaUrlFragment, |
| 156 kCaptchaToken); |
128 } | 157 } |
129 | 158 |
130 CaptchaFetcher::~CaptchaFetcher() {} | 159 CaptchaFetcher::~CaptchaFetcher() {} |
131 | 160 |
132 // static | 161 // static |
133 std::string CaptchaFetcher::GetCaptchaToken() { | 162 std::string CaptchaFetcher::GetCaptchaToken() { |
134 return kCaptchaToken; | 163 return kCaptchaToken; |
135 } | 164 } |
136 | 165 |
137 // static | 166 // static |
138 std::string CaptchaFetcher::GetCaptchaUrl() { | 167 std::string CaptchaFetcher::GetCaptchaUrl() { |
139 return std::string(kCaptchaUrlBase).append(kCaptchaUrlFragment); | 168 return std::string(kCaptchaUrlBase).append(kCaptchaUrlFragment); |
140 } | 169 } |
141 | 170 |
142 // static | 171 // static |
143 std::string CaptchaFetcher::GetUnlockUrl() { | 172 std::string CaptchaFetcher::GetUnlockUrl() { |
144 return kUnlockUrl; | 173 return kUnlockUrl; |
145 } | 174 } |
146 | 175 |
147 void CaptchaFetcher::Start() { | 176 void CaptchaFetcher::Start() { |
148 net::URLRequestStatus success(net::URLRequestStatus::SUCCESS, 0); | 177 delegate()->OnURLFetchComplete(this); |
149 std::string body = base::StringPrintf("Error=%s\n" | 178 } |
150 "Url=%s\n" | 179 |
151 "CaptchaUrl=%s\n" | 180 const GURL& CaptchaFetcher::url() const { |
152 "CaptchaToken=%s\n", | 181 return url_; |
153 "CaptchaRequired", | 182 } |
154 kUnlockUrl, | 183 |
155 kCaptchaUrlFragment, | 184 const net::URLRequestStatus& CaptchaFetcher::status() const { |
156 kCaptchaToken); | 185 return status_; |
157 delegate()->OnURLFetchComplete(this, | 186 } |
158 url_, | 187 |
159 success, | 188 int CaptchaFetcher::response_code() const { |
160 RC_FORBIDDEN, | 189 return RC_FORBIDDEN; |
161 net::ResponseCookies(), | 190 } |
162 body); | 191 |
| 192 bool CaptchaFetcher::GetResponseAsString( |
| 193 std::string* out_response_string) const { |
| 194 *out_response_string = data_; |
| 195 return true; |
163 } | 196 } |
164 | 197 |
165 HostedFetcher::HostedFetcher(bool success, | 198 HostedFetcher::HostedFetcher(bool success, |
166 const GURL& url, | 199 const GURL& url, |
167 const std::string& results, | 200 const std::string& results, |
168 URLFetcher::RequestType request_type, | 201 URLFetcher::RequestType request_type, |
169 URLFetcher::Delegate* d) | 202 content::URLFetcherDelegate* d) |
170 : URLFetcher(url, request_type, d), | 203 : URLFetcher(url, request_type, d), |
171 url_(url) { | 204 url_(url), |
| 205 status_(net::URLRequestStatus::SUCCESS, 0), |
| 206 response_code_(RC_REQUEST_OK) { |
172 } | 207 } |
173 | 208 |
174 HostedFetcher::~HostedFetcher() {} | 209 HostedFetcher::~HostedFetcher() {} |
175 | 210 |
176 void HostedFetcher::Start() { | 211 void HostedFetcher::Start() { |
177 net::URLRequestStatus success(net::URLRequestStatus::SUCCESS, 0); | |
178 int response_code = RC_REQUEST_OK; | |
179 std::string data; | |
180 VLOG(1) << upload_data(); | 212 VLOG(1) << upload_data(); |
181 if (upload_data().find("HOSTED") == std::string::npos) { | 213 if (upload_data().find("HOSTED") == std::string::npos) { |
182 VLOG(1) << "HostedFetcher failing request"; | 214 VLOG(1) << "HostedFetcher failing request"; |
183 response_code = RC_FORBIDDEN; | 215 response_code_ = RC_FORBIDDEN; |
184 data.assign("Error=BadAuthentication"); | 216 data_.assign("Error=BadAuthentication"); |
185 } | 217 } |
186 delegate()->OnURLFetchComplete(this, | 218 delegate()->OnURLFetchComplete(this); |
187 url_, | 219 } |
188 success, | 220 |
189 response_code, | 221 const GURL& HostedFetcher::url() const { |
190 net::ResponseCookies(), | 222 return url_; |
191 data); | 223 } |
| 224 |
| 225 const net::URLRequestStatus& HostedFetcher::status() const { |
| 226 return status_; |
| 227 } |
| 228 |
| 229 int HostedFetcher::response_code() const { |
| 230 return response_code_;; |
| 231 } |
| 232 |
| 233 bool HostedFetcher::GetResponseAsString( |
| 234 std::string* out_response_string) const { |
| 235 *out_response_string = data_; |
| 236 return true; |
192 } | 237 } |
193 | 238 |
194 } // namespace chromeos | 239 } // namespace chromeos |
OLD | NEW |