• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chromeos/login/auth/mock_url_fetchers.h"
6 
7 #include <errno.h>
8 
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"
18 
19 namespace chromeos {
20 
ExpectCanceledFetcher(bool success,const GURL & url,const std::string & results,net::URLFetcher::RequestType request_type,net::URLFetcherDelegate * d)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) {
28 }
29 
~ExpectCanceledFetcher()30 ExpectCanceledFetcher::~ExpectCanceledFetcher() {
31 }
32 
Start()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));
39 }
40 
CompleteFetch()41 void ExpectCanceledFetcher::CompleteFetch() {
42   ADD_FAILURE() << "Fetch completed in ExpectCanceledFetcher!";
43   base::MessageLoop::current()->Quit();  // Allow exiting even if we mess up.
44 }
45 
GotCanceledFetcher(bool success,const GURL & url,const std::string & results,net::URLFetcher::RequestType request_type,net::URLFetcherDelegate * d)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);
56 }
57 
~GotCanceledFetcher()58 GotCanceledFetcher::~GotCanceledFetcher() {
59 }
60 
Start()61 void GotCanceledFetcher::Start() {
62   delegate()->OnURLFetchComplete(this);
63 }
64 
SuccessFetcher(bool success,const GURL & url,const std::string & results,net::URLFetcher::RequestType request_type,net::URLFetcherDelegate * d)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);
74 }
75 
~SuccessFetcher()76 SuccessFetcher::~SuccessFetcher() {
77 }
78 
Start()79 void SuccessFetcher::Start() {
80   delegate()->OnURLFetchComplete(this);
81 }
82 
FailFetcher(bool success,const GURL & url,const std::string & results,net::URLFetcher::RequestType request_type,net::URLFetcherDelegate * d)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);
92 }
93 
~FailFetcher()94 FailFetcher::~FailFetcher() {
95 }
96 
Start()97 void FailFetcher::Start() {
98   delegate()->OnURLFetchComplete(this);
99 }
100 
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";
109 
CaptchaFetcher(bool success,const GURL & url,const std::string & results,net::URLFetcher::RequestType request_type,net::URLFetcherDelegate * d)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));
128 }
129 
~CaptchaFetcher()130 CaptchaFetcher::~CaptchaFetcher() {
131 }
132 
133 // static
GetCaptchaToken()134 std::string CaptchaFetcher::GetCaptchaToken() {
135   return kCaptchaToken;
136 }
137 
138 // static
GetCaptchaUrl()139 std::string CaptchaFetcher::GetCaptchaUrl() {
140   return std::string(kCaptchaUrlBase).append(kCaptchaUrlFragment);
141 }
142 
143 // static
GetUnlockUrl()144 std::string CaptchaFetcher::GetUnlockUrl() {
145   return kUnlockUrl;
146 }
147 
Start()148 void CaptchaFetcher::Start() {
149   delegate()->OnURLFetchComplete(this);
150 }
151 
HostedFetcher(bool success,const GURL & url,const std::string & results,net::URLFetcher::RequestType request_type,net::URLFetcherDelegate * d)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);
161 }
162 
~HostedFetcher()163 HostedFetcher::~HostedFetcher() {
164 }
165 
Start()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");
172   }
173   delegate()->OnURLFetchComplete(this);
174 }
175 
176 }  // namespace chromeos
177