1 // Copyright (c) 2011 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 "chrome/browser/chromeos/login/mock_url_fetchers.h" 6 7 #include <errno.h> 8 9 #include "base/message_loop.h" 10 #include "base/stringprintf.h" 11 #include "chrome/common/net/http_return.h" 12 #include "chrome/common/net/url_fetcher.h" 13 #include "content/browser/browser_thread.h" 14 #include "googleurl/src/gurl.h" 15 #include "net/url_request/url_request_status.h" 16 #include "testing/gtest/include/gtest/gtest.h" 17 18 namespace chromeos { 19 20 ExpectCanceledFetcher::ExpectCanceledFetcher( 21 bool success, 22 const GURL& url, 23 const std::string& results, 24 URLFetcher::RequestType request_type, 25 URLFetcher::Delegate* d) 26 : URLFetcher(url, request_type, d) { 27 } 28 29 ExpectCanceledFetcher::~ExpectCanceledFetcher() { 30 task_->Cancel(); 31 } 32 33 void ExpectCanceledFetcher::Start() { 34 task_ = NewRunnableFunction(&ExpectCanceledFetcher::CompleteFetch); 35 BrowserThread::PostDelayedTask(BrowserThread::UI, 36 FROM_HERE, 37 task_, 38 100); 39 } 40 41 // static 42 void ExpectCanceledFetcher::CompleteFetch() { 43 ADD_FAILURE() << "Fetch completed in ExpectCanceledFetcher!"; 44 MessageLoop::current()->Quit(); // Allow exiting even if we mess up. 45 } 46 47 GotCanceledFetcher::GotCanceledFetcher(bool success, 48 const GURL& url, 49 const std::string& results, 50 URLFetcher::RequestType request_type, 51 URLFetcher::Delegate* d) 52 : URLFetcher(url, request_type, d), 53 url_(url) { 54 } 55 56 GotCanceledFetcher::~GotCanceledFetcher() {} 57 58 void GotCanceledFetcher::Start() { 59 net::URLRequestStatus status; 60 status.set_status(net::URLRequestStatus::CANCELED); 61 delegate()->OnURLFetchComplete(this, 62 url_, 63 status, 64 RC_FORBIDDEN, 65 ResponseCookies(), 66 std::string()); 67 } 68 69 SuccessFetcher::SuccessFetcher(bool success, 70 const GURL& url, 71 const std::string& results, 72 URLFetcher::RequestType request_type, 73 URLFetcher::Delegate* d) 74 : URLFetcher(url, request_type, d), 75 url_(url) { 76 } 77 78 SuccessFetcher::~SuccessFetcher() {} 79 80 void SuccessFetcher::Start() { 81 net::URLRequestStatus success(net::URLRequestStatus::SUCCESS, 0); 82 delegate()->OnURLFetchComplete(this, 83 url_, 84 success, 85 RC_REQUEST_OK, 86 ResponseCookies(), 87 std::string()); 88 } 89 90 FailFetcher::FailFetcher(bool success, 91 const GURL& url, 92 const std::string& results, 93 URLFetcher::RequestType request_type, 94 URLFetcher::Delegate* d) 95 : URLFetcher(url, request_type, d), 96 url_(url) { 97 } 98 99 FailFetcher::~FailFetcher() {} 100 101 void FailFetcher::Start() { 102 net::URLRequestStatus failed(net::URLRequestStatus::FAILED, ECONNRESET); 103 delegate()->OnURLFetchComplete(this, 104 url_, 105 failed, 106 RC_REQUEST_OK, 107 ResponseCookies(), 108 std::string()); 109 } 110 111 // static 112 const char CaptchaFetcher::kCaptchaToken[] = "token"; 113 // static 114 const char CaptchaFetcher::kCaptchaUrlBase[] = "http://www.google.com/accounts/"; 115 // static 116 const char CaptchaFetcher::kCaptchaUrlFragment[] = "fragment"; 117 // static 118 const char CaptchaFetcher::kUnlockUrl[] = "http://what.ever"; 119 120 121 CaptchaFetcher::CaptchaFetcher(bool success, 122 const GURL& url, 123 const std::string& results, 124 URLFetcher::RequestType request_type, 125 URLFetcher::Delegate* d) 126 : URLFetcher(url, request_type, d), 127 url_(url) { 128 } 129 130 CaptchaFetcher::~CaptchaFetcher() {} 131 132 // static 133 std::string CaptchaFetcher::GetCaptchaToken() { 134 return kCaptchaToken; 135 } 136 137 // static 138 std::string CaptchaFetcher::GetCaptchaUrl() { 139 return std::string(kCaptchaUrlBase).append(kCaptchaUrlFragment); 140 } 141 142 // static 143 std::string CaptchaFetcher::GetUnlockUrl() { 144 return kUnlockUrl; 145 } 146 147 void CaptchaFetcher::Start() { 148 net::URLRequestStatus success(net::URLRequestStatus::SUCCESS, 0); 149 std::string body = base::StringPrintf("Error=%s\n" 150 "Url=%s\n" 151 "CaptchaUrl=%s\n" 152 "CaptchaToken=%s\n", 153 "CaptchaRequired", 154 kUnlockUrl, 155 kCaptchaUrlFragment, 156 kCaptchaToken); 157 delegate()->OnURLFetchComplete(this, 158 url_, 159 success, 160 RC_FORBIDDEN, 161 ResponseCookies(), 162 body); 163 } 164 165 HostedFetcher::HostedFetcher(bool success, 166 const GURL& url, 167 const std::string& results, 168 URLFetcher::RequestType request_type, 169 URLFetcher::Delegate* d) 170 : URLFetcher(url, request_type, d), 171 url_(url) { 172 } 173 174 HostedFetcher::~HostedFetcher() {} 175 176 void HostedFetcher::Start() { 177 net::URLRequestStatus success(net::URLRequestStatus::SUCCESS, 0); 178 int response_code = RC_REQUEST_OK; 179 std::string data; 180 VLOG(1) << upload_data(); 181 if (upload_data().find("HOSTED") == std::string::npos) { 182 VLOG(1) << "HostedFetcher failing request"; 183 response_code = RC_FORBIDDEN; 184 data.assign("Error=BadAuthentication"); 185 } 186 delegate()->OnURLFetchComplete(this, 187 url_, 188 success, 189 response_code, 190 ResponseCookies(), 191 data); 192 } 193 194 } // namespace chromeos 195