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/location.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "net/http/http_status_code.h"
15 #include "net/url_request/url_fetcher.h"
16 #include "net/url_request/url_fetcher_delegate.h"
17 #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::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
35 FROM_HERE
, base::Bind(&ExpectCanceledFetcher::CompleteFetch
,
36 weak_factory_
.GetWeakPtr()),
37 base::TimeDelta::FromMilliseconds(100));
40 void ExpectCanceledFetcher::CompleteFetch() {
41 ADD_FAILURE() << "Fetch completed in ExpectCanceledFetcher!";
42 base::MessageLoop::current()->Quit(); // Allow exiting even if we mess up.
45 GotCanceledFetcher::GotCanceledFetcher(
48 const std::string
& results
,
49 net::URLFetcher::RequestType request_type
,
50 net::URLFetcherDelegate
* d
)
51 : net::TestURLFetcher(0, url
, d
) {
53 set_status(net::URLRequestStatus(net::URLRequestStatus::CANCELED
, 0));
54 set_response_code(net::HTTP_FORBIDDEN
);
57 GotCanceledFetcher::~GotCanceledFetcher() {
60 void GotCanceledFetcher::Start() {
61 delegate()->OnURLFetchComplete(this);
64 SuccessFetcher::SuccessFetcher(bool success
,
66 const std::string
& results
,
67 net::URLFetcher::RequestType request_type
,
68 net::URLFetcherDelegate
* d
)
69 : net::TestURLFetcher(0, url
, d
) {
71 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0));
72 set_response_code(net::HTTP_OK
);
75 SuccessFetcher::~SuccessFetcher() {
78 void SuccessFetcher::Start() {
79 delegate()->OnURLFetchComplete(this);
82 FailFetcher::FailFetcher(bool success
,
84 const std::string
& results
,
85 net::URLFetcher::RequestType request_type
,
86 net::URLFetcherDelegate
* d
)
87 : net::TestURLFetcher(0, url
, d
) {
89 set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED
, ECONNRESET
));
90 set_response_code(net::HTTP_OK
);
93 FailFetcher::~FailFetcher() {
96 void FailFetcher::Start() {
97 delegate()->OnURLFetchComplete(this);
101 const char CaptchaFetcher::kCaptchaToken
[] = "token";
103 const char CaptchaFetcher::kCaptchaUrlBase
[] = "http://accounts.google.com/";
105 const char CaptchaFetcher::kCaptchaUrlFragment
[] = "fragment";
107 const char CaptchaFetcher::kUnlockUrl
[] = "http://what.ever";
109 CaptchaFetcher::CaptchaFetcher(bool success
,
111 const std::string
& results
,
112 net::URLFetcher::RequestType request_type
,
113 net::URLFetcherDelegate
* d
)
114 : net::TestURLFetcher(0, url
, d
) {
116 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0));
117 set_response_code(net::HTTP_FORBIDDEN
);
118 SetResponseString(base::StringPrintf(
129 CaptchaFetcher::~CaptchaFetcher() {
133 std::string
CaptchaFetcher::GetCaptchaToken() {
134 return kCaptchaToken
;
138 std::string
CaptchaFetcher::GetCaptchaUrl() {
139 return std::string(kCaptchaUrlBase
).append(kCaptchaUrlFragment
);
143 std::string
CaptchaFetcher::GetUnlockUrl() {
147 void CaptchaFetcher::Start() {
148 delegate()->OnURLFetchComplete(this);
151 HostedFetcher::HostedFetcher(bool success
,
153 const std::string
& results
,
154 net::URLFetcher::RequestType request_type
,
155 net::URLFetcherDelegate
* d
)
156 : net::TestURLFetcher(0, url
, d
) {
158 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, 0));
159 set_response_code(net::HTTP_OK
);
162 HostedFetcher::~HostedFetcher() {
165 void HostedFetcher::Start() {
166 VLOG(1) << upload_data();
167 if (upload_data().find("HOSTED") == std::string::npos
) {
168 VLOG(1) << "HostedFetcher failing request";
169 set_response_code(net::HTTP_FORBIDDEN
);
170 SetResponseString("Error=BadAuthentication");
172 delegate()->OnURLFetchComplete(this);
175 } // namespace chromeos