cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / chromeos / login / auth / mock_url_fetchers.cc
blob0b4879bc79fee96a402c73ac5c6fb927472fba85
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"
7 #include <errno.h>
9 #include "base/bind.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/base/net_errors.h"
15 #include "net/http/http_status_code.h"
16 #include "net/url_request/url_fetcher.h"
17 #include "net/url_request/url_fetcher_delegate.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 namespace chromeos {
22 ExpectCanceledFetcher::ExpectCanceledFetcher(
23 bool success,
24 const GURL& url,
25 const std::string& results,
26 net::URLFetcher::RequestType request_type,
27 net::URLFetcherDelegate* d)
28 : net::TestURLFetcher(0, url, d), weak_factory_(this) {
31 ExpectCanceledFetcher::~ExpectCanceledFetcher() {
34 void ExpectCanceledFetcher::Start() {
35 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
36 FROM_HERE, 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(
47 bool success,
48 const GURL& url,
49 const std::string& results,
50 net::URLFetcher::RequestType request_type,
51 net::URLFetcherDelegate* d)
52 : net::TestURLFetcher(0, url, d) {
53 set_url(url);
54 set_status(net::URLRequestStatus::FromError(net::ERR_ABORTED));
55 set_response_code(net::HTTP_FORBIDDEN);
58 GotCanceledFetcher::~GotCanceledFetcher() {
61 void GotCanceledFetcher::Start() {
62 delegate()->OnURLFetchComplete(this);
65 SuccessFetcher::SuccessFetcher(bool success,
66 const GURL& url,
67 const std::string& results,
68 net::URLFetcher::RequestType request_type,
69 net::URLFetcherDelegate* d)
70 : net::TestURLFetcher(0, url, d) {
71 set_url(url);
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,
84 const GURL& url,
85 const std::string& results,
86 net::URLFetcher::RequestType request_type,
87 net::URLFetcherDelegate* d)
88 : net::TestURLFetcher(0, url, d) {
89 set_url(url);
90 set_status(net::URLRequestStatus::FromError(net::ERR_CONNECTION_RESET));
91 set_response_code(net::HTTP_OK);
94 FailFetcher::~FailFetcher() {
97 void FailFetcher::Start() {
98 delegate()->OnURLFetchComplete(this);
101 // static
102 const char CaptchaFetcher::kCaptchaToken[] = "token";
103 // static
104 const char CaptchaFetcher::kCaptchaUrlBase[] = "http://accounts.google.com/";
105 // static
106 const char CaptchaFetcher::kCaptchaUrlFragment[] = "fragment";
107 // static
108 const char CaptchaFetcher::kUnlockUrl[] = "http://what.ever";
110 CaptchaFetcher::CaptchaFetcher(bool success,
111 const GURL& url,
112 const std::string& results,
113 net::URLFetcher::RequestType request_type,
114 net::URLFetcherDelegate* d)
115 : net::TestURLFetcher(0, url, d) {
116 set_url(url);
117 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0));
118 set_response_code(net::HTTP_FORBIDDEN);
119 SetResponseString(base::StringPrintf(
120 "Error=%s\n"
121 "Url=%s\n"
122 "CaptchaUrl=%s\n"
123 "CaptchaToken=%s\n",
124 "CaptchaRequired",
125 kUnlockUrl,
126 kCaptchaUrlFragment,
127 kCaptchaToken));
130 CaptchaFetcher::~CaptchaFetcher() {
133 // static
134 std::string CaptchaFetcher::GetCaptchaToken() {
135 return kCaptchaToken;
138 // static
139 std::string CaptchaFetcher::GetCaptchaUrl() {
140 return std::string(kCaptchaUrlBase).append(kCaptchaUrlFragment);
143 // static
144 std::string CaptchaFetcher::GetUnlockUrl() {
145 return kUnlockUrl;
148 void CaptchaFetcher::Start() {
149 delegate()->OnURLFetchComplete(this);
152 HostedFetcher::HostedFetcher(bool success,
153 const GURL& url,
154 const std::string& results,
155 net::URLFetcher::RequestType request_type,
156 net::URLFetcherDelegate* d)
157 : net::TestURLFetcher(0, url, d) {
158 set_url(url);
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