1 // Copyright 2014 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.
5 #include "chromeos/login/auth/mock_url_fetchers.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/stringprintf.h"
12 #include "net/http/http_status_code.h"
13 #include "net/url_request/url_fetcher.h"
14 #include "net/url_request/url_fetcher_delegate.h"
15 #include "net/url_request/url_request_status.h"
16 #include "testing/gtest/include/gtest/gtest.h"
21 ExpectCanceledFetcher::ExpectCanceledFetcher(
24 const std::string
& results
,
25 net::URLFetcher::RequestType request_type
,
26 net::URLFetcherDelegate
* d
)
27 : net::TestURLFetcher(0, url
, d
), weak_factory_(this) {
30 ExpectCanceledFetcher::~ExpectCanceledFetcher() {
33 void ExpectCanceledFetcher::Start() {
34 base::MessageLoop::current()->PostDelayedTask(
36 base::Bind(&ExpectCanceledFetcher::CompleteFetch
,
37 weak_factory_
.GetWeakPtr()),
38 base::TimeDelta::FromMilliseconds(100));
41 void ExpectCanceledFetcher::CompleteFetch() {
42 ADD_FAILURE() << "Fetch completed in ExpectCanceledFetcher!";
43 base::MessageLoop::current()->Quit(); // Allow exiting even if we mess up.
46 GotCanceledFetcher::GotCanceledFetcher(
49 const std::string
& results
,
50 net::URLFetcher::RequestType request_type
,
51 net::URLFetcherDelegate
* d
)
52 : net::TestURLFetcher(0, url
, d
) {
54 set_status(net::URLRequestStatus(net::URLRequestStatus::CANCELED
, 0));
55 set_response_code(net::HTTP_FORBIDDEN
);
58 GotCanceledFetcher::~GotCanceledFetcher() {
61 void GotCanceledFetcher::Start() {
62 delegate()->OnURLFetchComplete(this);
65 SuccessFetcher::SuccessFetcher(bool success
,
67 const std::string
& results
,
68 net::URLFetcher::RequestType request_type
,
69 net::URLFetcherDelegate
* d
)
70 : net::TestURLFetcher(0, url
, d
) {
72 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0));
73 set_response_code(net::HTTP_OK
);
76 SuccessFetcher::~SuccessFetcher() {
79 void SuccessFetcher::Start() {
80 delegate()->OnURLFetchComplete(this);
83 FailFetcher::FailFetcher(bool success
,
85 const std::string
& results
,
86 net::URLFetcher::RequestType request_type
,
87 net::URLFetcherDelegate
* d
)
88 : net::TestURLFetcher(0, url
, d
) {
90 set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED
, ECONNRESET
));
91 set_response_code(net::HTTP_OK
);
94 FailFetcher::~FailFetcher() {
97 void FailFetcher::Start() {
98 delegate()->OnURLFetchComplete(this);
102 const char CaptchaFetcher::kCaptchaToken
[] = "token";
104 const char CaptchaFetcher::kCaptchaUrlBase
[] = "http://accounts.google.com/";
106 const char CaptchaFetcher::kCaptchaUrlFragment
[] = "fragment";
108 const char CaptchaFetcher::kUnlockUrl
[] = "http://what.ever";
110 CaptchaFetcher::CaptchaFetcher(bool success
,
112 const std::string
& results
,
113 net::URLFetcher::RequestType request_type
,
114 net::URLFetcherDelegate
* d
)
115 : net::TestURLFetcher(0, url
, d
) {
117 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0));
118 set_response_code(net::HTTP_FORBIDDEN
);
119 SetResponseString(base::StringPrintf(
130 CaptchaFetcher::~CaptchaFetcher() {
134 std::string
CaptchaFetcher::GetCaptchaToken() {
135 return kCaptchaToken
;
139 std::string
CaptchaFetcher::GetCaptchaUrl() {
140 return std::string(kCaptchaUrlBase
).append(kCaptchaUrlFragment
);
144 std::string
CaptchaFetcher::GetUnlockUrl() {
148 void CaptchaFetcher::Start() {
149 delegate()->OnURLFetchComplete(this);
152 HostedFetcher::HostedFetcher(bool success
,
154 const std::string
& results
,
155 net::URLFetcher::RequestType request_type
,
156 net::URLFetcherDelegate
* d
)
157 : net::TestURLFetcher(0, url
, d
) {
159 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0));
160 set_response_code(net::HTTP_OK
);
163 HostedFetcher::~HostedFetcher() {
166 void HostedFetcher::Start() {
167 VLOG(1) << upload_data();
168 if (upload_data().find("HOSTED") == std::string::npos
) {
169 VLOG(1) << "HostedFetcher failing request";
170 set_response_code(net::HTTP_FORBIDDEN
);
171 SetResponseString("Error=BadAuthentication");
173 delegate()->OnURLFetchComplete(this);
176 } // namespace chromeos