Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chromeos / login / auth / mock_url_fetchers.cc
blob490f56bac2462c87a5513e255661e79078eb4689
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/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"
19 namespace chromeos {
21 ExpectCanceledFetcher::ExpectCanceledFetcher(
22 bool success,
23 const GURL& url,
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(
46 bool success,
47 const GURL& url,
48 const std::string& results,
49 net::URLFetcher::RequestType request_type,
50 net::URLFetcherDelegate* d)
51 : net::TestURLFetcher(0, url, d) {
52 set_url(url);
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,
65 const GURL& url,
66 const std::string& results,
67 net::URLFetcher::RequestType request_type,
68 net::URLFetcherDelegate* d)
69 : net::TestURLFetcher(0, url, d) {
70 set_url(url);
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,
83 const GURL& url,
84 const std::string& results,
85 net::URLFetcher::RequestType request_type,
86 net::URLFetcherDelegate* d)
87 : net::TestURLFetcher(0, url, d) {
88 set_url(url);
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);
100 // static
101 const char CaptchaFetcher::kCaptchaToken[] = "token";
102 // static
103 const char CaptchaFetcher::kCaptchaUrlBase[] = "http://accounts.google.com/";
104 // static
105 const char CaptchaFetcher::kCaptchaUrlFragment[] = "fragment";
106 // static
107 const char CaptchaFetcher::kUnlockUrl[] = "http://what.ever";
109 CaptchaFetcher::CaptchaFetcher(bool success,
110 const GURL& url,
111 const std::string& results,
112 net::URLFetcher::RequestType request_type,
113 net::URLFetcherDelegate* d)
114 : net::TestURLFetcher(0, url, d) {
115 set_url(url);
116 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0));
117 set_response_code(net::HTTP_FORBIDDEN);
118 SetResponseString(base::StringPrintf(
119 "Error=%s\n"
120 "Url=%s\n"
121 "CaptchaUrl=%s\n"
122 "CaptchaToken=%s\n",
123 "CaptchaRequired",
124 kUnlockUrl,
125 kCaptchaUrlFragment,
126 kCaptchaToken));
129 CaptchaFetcher::~CaptchaFetcher() {
132 // static
133 std::string CaptchaFetcher::GetCaptchaToken() {
134 return kCaptchaToken;
137 // static
138 std::string CaptchaFetcher::GetCaptchaUrl() {
139 return std::string(kCaptchaUrlBase).append(kCaptchaUrlFragment);
142 // static
143 std::string CaptchaFetcher::GetUnlockUrl() {
144 return kUnlockUrl;
147 void CaptchaFetcher::Start() {
148 delegate()->OnURLFetchComplete(this);
151 HostedFetcher::HostedFetcher(bool success,
152 const GURL& url,
153 const std::string& results,
154 net::URLFetcher::RequestType request_type,
155 net::URLFetcherDelegate* d)
156 : net::TestURLFetcher(0, url, d) {
157 set_url(url);
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