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/strings/stringprintf.h"
11 #include "net/http/http_status_code.h"
12 #include "net/url_request/url_fetcher.h"
13 #include "net/url_request/url_fetcher_delegate.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 ExpectCanceledFetcher::ExpectCanceledFetcher(
21 const std::string
& results
,
22 net::URLFetcher::RequestType request_type
,
23 net::URLFetcherDelegate
* d
)
24 : net::TestURLFetcher(0, url
, d
), weak_factory_(this) {
27 ExpectCanceledFetcher::~ExpectCanceledFetcher() {
30 void ExpectCanceledFetcher::Start() {
31 base::MessageLoop::current()->PostDelayedTask(
33 base::Bind(&ExpectCanceledFetcher::CompleteFetch
,
34 weak_factory_
.GetWeakPtr()),
35 base::TimeDelta::FromMilliseconds(100));
38 void ExpectCanceledFetcher::CompleteFetch() {
39 ADD_FAILURE() << "Fetch completed in ExpectCanceledFetcher!";
40 base::MessageLoop::current()->Quit(); // Allow exiting even if we mess up.
43 GotCanceledFetcher::GotCanceledFetcher(
46 const std::string
& results
,
47 net::URLFetcher::RequestType request_type
,
48 net::URLFetcherDelegate
* d
)
49 : net::TestURLFetcher(0, url
, d
) {
51 set_status(net::URLRequestStatus(net::URLRequestStatus::CANCELED
, 0));
52 set_response_code(net::HTTP_FORBIDDEN
);
55 GotCanceledFetcher::~GotCanceledFetcher() {
58 void GotCanceledFetcher::Start() {
59 delegate()->OnURLFetchComplete(this);
62 SuccessFetcher::SuccessFetcher(bool success
,
64 const std::string
& results
,
65 net::URLFetcher::RequestType request_type
,
66 net::URLFetcherDelegate
* d
)
67 : net::TestURLFetcher(0, url
, d
) {
69 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0));
70 set_response_code(net::HTTP_OK
);
73 SuccessFetcher::~SuccessFetcher() {
76 void SuccessFetcher::Start() {
77 delegate()->OnURLFetchComplete(this);
80 FailFetcher::FailFetcher(bool success
,
82 const std::string
& results
,
83 net::URLFetcher::RequestType request_type
,
84 net::URLFetcherDelegate
* d
)
85 : net::TestURLFetcher(0, url
, d
) {
87 set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED
, ECONNRESET
));
88 set_response_code(net::HTTP_OK
);
91 FailFetcher::~FailFetcher() {
94 void FailFetcher::Start() {
95 delegate()->OnURLFetchComplete(this);
99 const char CaptchaFetcher::kCaptchaToken
[] = "token";
101 const char CaptchaFetcher::kCaptchaUrlBase
[] = "http://accounts.google.com/";
103 const char CaptchaFetcher::kCaptchaUrlFragment
[] = "fragment";
105 const char CaptchaFetcher::kUnlockUrl
[] = "http://what.ever";
107 CaptchaFetcher::CaptchaFetcher(bool success
,
109 const std::string
& results
,
110 net::URLFetcher::RequestType request_type
,
111 net::URLFetcherDelegate
* d
)
112 : net::TestURLFetcher(0, url
, d
) {
114 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0));
115 set_response_code(net::HTTP_FORBIDDEN
);
116 SetResponseString(base::StringPrintf(
127 CaptchaFetcher::~CaptchaFetcher() {
131 std::string
CaptchaFetcher::GetCaptchaToken() {
132 return kCaptchaToken
;
136 std::string
CaptchaFetcher::GetCaptchaUrl() {
137 return std::string(kCaptchaUrlBase
).append(kCaptchaUrlFragment
);
141 std::string
CaptchaFetcher::GetUnlockUrl() {
145 void CaptchaFetcher::Start() {
146 delegate()->OnURLFetchComplete(this);
149 HostedFetcher::HostedFetcher(bool success
,
151 const std::string
& results
,
152 net::URLFetcher::RequestType request_type
,
153 net::URLFetcherDelegate
* d
)
154 : net::TestURLFetcher(0, url
, d
) {
156 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0));
157 set_response_code(net::HTTP_OK
);
160 HostedFetcher::~HostedFetcher() {
163 void HostedFetcher::Start() {
164 VLOG(1) << upload_data();
165 if (upload_data().find("HOSTED") == std::string::npos
) {
166 VLOG(1) << "HostedFetcher failing request";
167 set_response_code(net::HTTP_FORBIDDEN
);
168 SetResponseString("Error=BadAuthentication");
170 delegate()->OnURLFetchComplete(this);
173 } // namespace chromeos