Roll src/third_party/WebKit abe9e39c:3d349bd (svn 182659:182669)
[chromium-blink-merge.git] / chromeos / login / auth / mock_url_fetchers.cc
bloba7944269b4b09070eb9aaf3f3256f6c0b4488ee5
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/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"
17 #include "url/gurl.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::MessageLoop::current()->PostDelayedTask(
35 FROM_HERE,
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(
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(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,
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(net::URLRequestStatus::FAILED, ECONNRESET));
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