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/public/common/url_fetcher_delegate.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 content::URLFetcher::RequestType request_type, |
26 content::URLFetcherDelegate* d) | 26 content::URLFetcherDelegate* d) |
27 : URLFetcher(url, request_type, d), | 27 : TestURLFetcher(0, 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( |
48 const GURL& url, | 48 bool success, |
49 const std::string& results, | 49 const GURL& url, |
50 URLFetcher::RequestType request_type, | 50 const std::string& results, |
51 content::URLFetcherDelegate* d) | 51 content::URLFetcher::RequestType request_type, |
52 : URLFetcher(url, request_type, d), | 52 content::URLFetcherDelegate* d) |
53 url_(url), | 53 : TestURLFetcher(0, url, request_type, d) { |
54 status_(net::URLRequestStatus::CANCELED, 0) { | 54 set_url(url); |
| 55 set_status(net::URLRequestStatus(net::URLRequestStatus::CANCELED, 0)); |
| 56 set_response_code(RC_FORBIDDEN); |
55 } | 57 } |
56 | 58 |
57 GotCanceledFetcher::~GotCanceledFetcher() {} | 59 GotCanceledFetcher::~GotCanceledFetcher() {} |
58 | 60 |
59 void GotCanceledFetcher::Start() { | 61 void GotCanceledFetcher::Start() { |
60 delegate()->OnURLFetchComplete(this); | 62 delegate()->OnURLFetchComplete(this); |
61 } | 63 } |
62 | 64 |
63 const GURL& GotCanceledFetcher::GetUrl() const { | |
64 return url_; | |
65 } | |
66 | |
67 const net::URLRequestStatus& GotCanceledFetcher::GetStatus() const { | |
68 return status_; | |
69 } | |
70 | |
71 int GotCanceledFetcher::GetResponseCode() const { | |
72 return RC_FORBIDDEN; | |
73 } | |
74 | |
75 SuccessFetcher::SuccessFetcher(bool success, | 65 SuccessFetcher::SuccessFetcher(bool success, |
76 const GURL& url, | 66 const GURL& url, |
77 const std::string& results, | 67 const std::string& results, |
78 URLFetcher::RequestType request_type, | 68 content::URLFetcher::RequestType request_type, |
79 content::URLFetcherDelegate* d) | 69 content::URLFetcherDelegate* d) |
80 : URLFetcher(url, request_type, d), | 70 : TestURLFetcher(0, url, request_type, d) { |
81 url_(url), | 71 set_url(url); |
82 status_(net::URLRequestStatus::SUCCESS, 0) { | 72 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); |
| 73 set_response_code(RC_REQUEST_OK); |
83 } | 74 } |
84 | 75 |
85 SuccessFetcher::~SuccessFetcher() {} | 76 SuccessFetcher::~SuccessFetcher() {} |
86 | 77 |
87 void SuccessFetcher::Start() { | 78 void SuccessFetcher::Start() { |
88 delegate()->OnURLFetchComplete(this); | 79 delegate()->OnURLFetchComplete(this); |
89 } | 80 } |
90 | 81 |
91 const GURL& SuccessFetcher::GetUrl() const { | |
92 return url_; | |
93 } | |
94 | |
95 const net::URLRequestStatus& SuccessFetcher::GetStatus() const { | |
96 return status_; | |
97 } | |
98 | |
99 int SuccessFetcher::GetResponseCode() const { | |
100 return RC_REQUEST_OK; | |
101 } | |
102 | |
103 FailFetcher::FailFetcher(bool success, | 82 FailFetcher::FailFetcher(bool success, |
104 const GURL& url, | 83 const GURL& url, |
105 const std::string& results, | 84 const std::string& results, |
106 URLFetcher::RequestType request_type, | 85 content::URLFetcher::RequestType request_type, |
107 content::URLFetcherDelegate* d) | 86 content::URLFetcherDelegate* d) |
108 : URLFetcher(url, request_type, d), | 87 : TestURLFetcher(0, url, request_type, d) { |
109 url_(url), | 88 set_url(url); |
110 status_(net::URLRequestStatus::FAILED, ECONNRESET) { | 89 set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, ECONNRESET)); |
| 90 set_response_code(RC_REQUEST_OK); |
111 } | 91 } |
112 | 92 |
113 FailFetcher::~FailFetcher() {} | 93 FailFetcher::~FailFetcher() {} |
114 | 94 |
115 void FailFetcher::Start() { | 95 void FailFetcher::Start() { |
116 delegate()->OnURLFetchComplete(this); | 96 delegate()->OnURLFetchComplete(this); |
117 } | 97 } |
118 | 98 |
119 const GURL& FailFetcher::GetUrl() const { | |
120 return url_; | |
121 } | |
122 | |
123 const net::URLRequestStatus& FailFetcher::GetStatus() const { | |
124 return status_; | |
125 } | |
126 | |
127 int FailFetcher::GetResponseCode() const { | |
128 return RC_REQUEST_OK; | |
129 } | |
130 | |
131 // static | 99 // static |
132 const char CaptchaFetcher::kCaptchaToken[] = "token"; | 100 const char CaptchaFetcher::kCaptchaToken[] = "token"; |
133 // static | 101 // static |
134 const char CaptchaFetcher::kCaptchaUrlBase[] = "http://www.google.com/accounts/"
; | 102 const char CaptchaFetcher::kCaptchaUrlBase[] = "http://www.google.com/accounts/"
; |
135 // static | 103 // static |
136 const char CaptchaFetcher::kCaptchaUrlFragment[] = "fragment"; | 104 const char CaptchaFetcher::kCaptchaUrlFragment[] = "fragment"; |
137 // static | 105 // static |
138 const char CaptchaFetcher::kUnlockUrl[] = "http://what.ever"; | 106 const char CaptchaFetcher::kUnlockUrl[] = "http://what.ever"; |
139 | 107 |
140 | 108 |
141 CaptchaFetcher::CaptchaFetcher(bool success, | 109 CaptchaFetcher::CaptchaFetcher(bool success, |
142 const GURL& url, | 110 const GURL& url, |
143 const std::string& results, | 111 const std::string& results, |
144 URLFetcher::RequestType request_type, | 112 content::URLFetcher::RequestType request_type, |
145 content::URLFetcherDelegate* d) | 113 content::URLFetcherDelegate* d) |
146 : URLFetcher(url, request_type, d), | 114 : TestURLFetcher(0, url, request_type, d) { |
147 url_(url), | 115 set_url(url); |
148 status_(net::URLRequestStatus::SUCCESS, 0) { | 116 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); |
149 data_ = base::StringPrintf("Error=%s\n" | 117 set_response_code(RC_FORBIDDEN); |
150 "Url=%s\n" | 118 SetResponseString(base::StringPrintf("Error=%s\n" |
151 "CaptchaUrl=%s\n" | 119 "Url=%s\n" |
152 "CaptchaToken=%s\n", | 120 "CaptchaUrl=%s\n" |
153 "CaptchaRequired", | 121 "CaptchaToken=%s\n", |
154 kUnlockUrl, | 122 "CaptchaRequired", |
155 kCaptchaUrlFragment, | 123 kUnlockUrl, |
156 kCaptchaToken); | 124 kCaptchaUrlFragment, |
| 125 kCaptchaToken)); |
157 } | 126 } |
158 | 127 |
159 CaptchaFetcher::~CaptchaFetcher() {} | 128 CaptchaFetcher::~CaptchaFetcher() {} |
160 | 129 |
161 // static | 130 // static |
162 std::string CaptchaFetcher::GetCaptchaToken() { | 131 std::string CaptchaFetcher::GetCaptchaToken() { |
163 return kCaptchaToken; | 132 return kCaptchaToken; |
164 } | 133 } |
165 | 134 |
166 // static | 135 // static |
167 std::string CaptchaFetcher::GetCaptchaUrl() { | 136 std::string CaptchaFetcher::GetCaptchaUrl() { |
168 return std::string(kCaptchaUrlBase).append(kCaptchaUrlFragment); | 137 return std::string(kCaptchaUrlBase).append(kCaptchaUrlFragment); |
169 } | 138 } |
170 | 139 |
171 // static | 140 // static |
172 std::string CaptchaFetcher::GetUnlockUrl() { | 141 std::string CaptchaFetcher::GetUnlockUrl() { |
173 return kUnlockUrl; | 142 return kUnlockUrl; |
174 } | 143 } |
175 | 144 |
176 void CaptchaFetcher::Start() { | 145 void CaptchaFetcher::Start() { |
177 delegate()->OnURLFetchComplete(this); | 146 delegate()->OnURLFetchComplete(this); |
178 } | 147 } |
179 | 148 |
180 const GURL& CaptchaFetcher::GetUrl() const { | |
181 return url_; | |
182 } | |
183 | |
184 const net::URLRequestStatus& CaptchaFetcher::GetStatus() const { | |
185 return status_; | |
186 } | |
187 | |
188 int CaptchaFetcher::GetResponseCode() const { | |
189 return RC_FORBIDDEN; | |
190 } | |
191 | |
192 bool CaptchaFetcher::GetResponseAsString( | |
193 std::string* out_response_string) const { | |
194 *out_response_string = data_; | |
195 return true; | |
196 } | |
197 | |
198 HostedFetcher::HostedFetcher(bool success, | 149 HostedFetcher::HostedFetcher(bool success, |
199 const GURL& url, | 150 const GURL& url, |
200 const std::string& results, | 151 const std::string& results, |
201 URLFetcher::RequestType request_type, | 152 content::URLFetcher::RequestType request_type, |
202 content::URLFetcherDelegate* d) | 153 content::URLFetcherDelegate* d) |
203 : URLFetcher(url, request_type, d), | 154 : TestURLFetcher(0, url, request_type, d) { |
204 url_(url), | 155 set_url(url); |
205 status_(net::URLRequestStatus::SUCCESS, 0), | 156 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); |
206 response_code_(RC_REQUEST_OK) { | 157 set_response_code(RC_REQUEST_OK); |
207 } | 158 } |
208 | 159 |
209 HostedFetcher::~HostedFetcher() {} | 160 HostedFetcher::~HostedFetcher() {} |
210 | 161 |
211 void HostedFetcher::Start() { | 162 void HostedFetcher::Start() { |
212 VLOG(1) << upload_data(); | 163 VLOG(1) << upload_data(); |
213 if (upload_data().find("HOSTED") == std::string::npos) { | 164 if (upload_data().find("HOSTED") == std::string::npos) { |
214 VLOG(1) << "HostedFetcher failing request"; | 165 VLOG(1) << "HostedFetcher failing request"; |
215 response_code_ = RC_FORBIDDEN; | 166 set_response_code(RC_FORBIDDEN); |
216 data_.assign("Error=BadAuthentication"); | 167 SetResponseString("Error=BadAuthentication"); |
217 } | 168 } |
218 delegate()->OnURLFetchComplete(this); | 169 delegate()->OnURLFetchComplete(this); |
219 } | 170 } |
220 | 171 |
221 const GURL& HostedFetcher::GetUrl() const { | |
222 return url_; | |
223 } | |
224 | |
225 const net::URLRequestStatus& HostedFetcher::GetStatus() const { | |
226 return status_; | |
227 } | |
228 | |
229 int HostedFetcher::GetResponseCode() 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; | |
237 } | |
238 | |
239 } // namespace chromeos | 172 } // namespace chromeos |
OLD | NEW |