OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/login/mock_url_fetchers.h" | 5 #include "chrome/browser/chromeos/login/mock_url_fetchers.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
13 #include "content/public/common/url_fetcher.h" | 13 #include "content/public/common/url_fetcher.h" |
14 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
15 #include "net/http/http_status_code.h" | 15 #include "net/http/http_status_code.h" |
16 #include "net/url_request/url_fetcher_delegate.h" | 16 #include "net/url_request/url_fetcher_delegate.h" |
17 #include "net/url_request/url_request_status.h" | 17 #include "net/url_request/url_request_status.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 | 19 |
20 namespace chromeos { | 20 namespace chromeos { |
21 | 21 |
22 ExpectCanceledFetcher::ExpectCanceledFetcher( | 22 ExpectCanceledFetcher::ExpectCanceledFetcher( |
23 bool success, | 23 bool success, |
24 const GURL& url, | 24 const GURL& url, |
25 const std::string& results, | 25 const std::string& results, |
26 content::URLFetcher::RequestType request_type, | 26 net::URLFetcher::RequestType request_type, |
27 net::URLFetcherDelegate* d) | 27 net::URLFetcherDelegate* d) |
28 : TestURLFetcher(0, url, d), | 28 : TestURLFetcher(0, url, d), |
29 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 29 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
30 } | 30 } |
31 | 31 |
32 ExpectCanceledFetcher::~ExpectCanceledFetcher() { | 32 ExpectCanceledFetcher::~ExpectCanceledFetcher() { |
33 } | 33 } |
34 | 34 |
35 void ExpectCanceledFetcher::Start() { | 35 void ExpectCanceledFetcher::Start() { |
36 MessageLoop::current()->PostDelayedTask( | 36 MessageLoop::current()->PostDelayedTask( |
37 FROM_HERE, | 37 FROM_HERE, |
38 base::Bind(&ExpectCanceledFetcher::CompleteFetch, | 38 base::Bind(&ExpectCanceledFetcher::CompleteFetch, |
39 weak_factory_.GetWeakPtr()), | 39 weak_factory_.GetWeakPtr()), |
40 base::TimeDelta::FromMilliseconds(100)); | 40 base::TimeDelta::FromMilliseconds(100)); |
41 } | 41 } |
42 | 42 |
43 void ExpectCanceledFetcher::CompleteFetch() { | 43 void ExpectCanceledFetcher::CompleteFetch() { |
44 ADD_FAILURE() << "Fetch completed in ExpectCanceledFetcher!"; | 44 ADD_FAILURE() << "Fetch completed in ExpectCanceledFetcher!"; |
45 MessageLoop::current()->Quit(); // Allow exiting even if we mess up. | 45 MessageLoop::current()->Quit(); // Allow exiting even if we mess up. |
46 } | 46 } |
47 | 47 |
48 GotCanceledFetcher::GotCanceledFetcher( | 48 GotCanceledFetcher::GotCanceledFetcher( |
49 bool success, | 49 bool success, |
50 const GURL& url, | 50 const GURL& url, |
51 const std::string& results, | 51 const std::string& results, |
52 content::URLFetcher::RequestType request_type, | 52 net::URLFetcher::RequestType request_type, |
53 net::URLFetcherDelegate* d) | 53 net::URLFetcherDelegate* d) |
54 : TestURLFetcher(0, url, d) { | 54 : TestURLFetcher(0, url, d) { |
55 set_url(url); | 55 set_url(url); |
56 set_status(net::URLRequestStatus(net::URLRequestStatus::CANCELED, 0)); | 56 set_status(net::URLRequestStatus(net::URLRequestStatus::CANCELED, 0)); |
57 set_response_code(net::HTTP_FORBIDDEN); | 57 set_response_code(net::HTTP_FORBIDDEN); |
58 } | 58 } |
59 | 59 |
60 GotCanceledFetcher::~GotCanceledFetcher() {} | 60 GotCanceledFetcher::~GotCanceledFetcher() {} |
61 | 61 |
62 void GotCanceledFetcher::Start() { | 62 void GotCanceledFetcher::Start() { |
63 delegate()->OnURLFetchComplete(this); | 63 delegate()->OnURLFetchComplete(this); |
64 } | 64 } |
65 | 65 |
66 SuccessFetcher::SuccessFetcher(bool success, | 66 SuccessFetcher::SuccessFetcher(bool success, |
67 const GURL& url, | 67 const GURL& url, |
68 const std::string& results, | 68 const std::string& results, |
69 content::URLFetcher::RequestType request_type, | 69 net::URLFetcher::RequestType request_type, |
70 net::URLFetcherDelegate* d) | 70 net::URLFetcherDelegate* d) |
71 : TestURLFetcher(0, url, d) { | 71 : TestURLFetcher(0, url, d) { |
72 set_url(url); | 72 set_url(url); |
73 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); | 73 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); |
74 set_response_code(net::HTTP_OK); | 74 set_response_code(net::HTTP_OK); |
75 } | 75 } |
76 | 76 |
77 SuccessFetcher::~SuccessFetcher() {} | 77 SuccessFetcher::~SuccessFetcher() {} |
78 | 78 |
79 void SuccessFetcher::Start() { | 79 void SuccessFetcher::Start() { |
80 delegate()->OnURLFetchComplete(this); | 80 delegate()->OnURLFetchComplete(this); |
81 } | 81 } |
82 | 82 |
83 FailFetcher::FailFetcher(bool success, | 83 FailFetcher::FailFetcher(bool success, |
84 const GURL& url, | 84 const GURL& url, |
85 const std::string& results, | 85 const std::string& results, |
86 content::URLFetcher::RequestType request_type, | 86 net::URLFetcher::RequestType request_type, |
87 net::URLFetcherDelegate* d) | 87 net::URLFetcherDelegate* d) |
88 : TestURLFetcher(0, url, d) { | 88 : TestURLFetcher(0, url, d) { |
89 set_url(url); | 89 set_url(url); |
90 set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, ECONNRESET)); | 90 set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, ECONNRESET)); |
91 set_response_code(net::HTTP_OK); | 91 set_response_code(net::HTTP_OK); |
92 } | 92 } |
93 | 93 |
94 FailFetcher::~FailFetcher() {} | 94 FailFetcher::~FailFetcher() {} |
95 | 95 |
96 void FailFetcher::Start() { | 96 void FailFetcher::Start() { |
97 delegate()->OnURLFetchComplete(this); | 97 delegate()->OnURLFetchComplete(this); |
98 } | 98 } |
99 | 99 |
100 // static | 100 // static |
101 const char CaptchaFetcher::kCaptchaToken[] = "token"; | 101 const char CaptchaFetcher::kCaptchaToken[] = "token"; |
102 // static | 102 // static |
103 const char CaptchaFetcher::kCaptchaUrlBase[] = "http://accounts.google.com/"; | 103 const char CaptchaFetcher::kCaptchaUrlBase[] = "http://accounts.google.com/"; |
104 // static | 104 // static |
105 const char CaptchaFetcher::kCaptchaUrlFragment[] = "fragment"; | 105 const char CaptchaFetcher::kCaptchaUrlFragment[] = "fragment"; |
106 // static | 106 // static |
107 const char CaptchaFetcher::kUnlockUrl[] = "http://what.ever"; | 107 const char CaptchaFetcher::kUnlockUrl[] = "http://what.ever"; |
108 | 108 |
109 | 109 |
110 CaptchaFetcher::CaptchaFetcher(bool success, | 110 CaptchaFetcher::CaptchaFetcher(bool success, |
111 const GURL& url, | 111 const GURL& url, |
112 const std::string& results, | 112 const std::string& results, |
113 content::URLFetcher::RequestType request_type, | 113 net::URLFetcher::RequestType request_type, |
114 net::URLFetcherDelegate* d) | 114 net::URLFetcherDelegate* d) |
115 : TestURLFetcher(0, url, d) { | 115 : TestURLFetcher(0, url, d) { |
116 set_url(url); | 116 set_url(url); |
117 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); | 117 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); |
118 set_response_code(net::HTTP_FORBIDDEN); | 118 set_response_code(net::HTTP_FORBIDDEN); |
119 SetResponseString(base::StringPrintf("Error=%s\n" | 119 SetResponseString(base::StringPrintf("Error=%s\n" |
120 "Url=%s\n" | 120 "Url=%s\n" |
121 "CaptchaUrl=%s\n" | 121 "CaptchaUrl=%s\n" |
122 "CaptchaToken=%s\n", | 122 "CaptchaToken=%s\n", |
123 "CaptchaRequired", | 123 "CaptchaRequired", |
(...skipping 19 matching lines...) Expand all Loading... |
143 return kUnlockUrl; | 143 return kUnlockUrl; |
144 } | 144 } |
145 | 145 |
146 void CaptchaFetcher::Start() { | 146 void CaptchaFetcher::Start() { |
147 delegate()->OnURLFetchComplete(this); | 147 delegate()->OnURLFetchComplete(this); |
148 } | 148 } |
149 | 149 |
150 HostedFetcher::HostedFetcher(bool success, | 150 HostedFetcher::HostedFetcher(bool success, |
151 const GURL& url, | 151 const GURL& url, |
152 const std::string& results, | 152 const std::string& results, |
153 content::URLFetcher::RequestType request_type, | 153 net::URLFetcher::RequestType request_type, |
154 net::URLFetcherDelegate* d) | 154 net::URLFetcherDelegate* d) |
155 : TestURLFetcher(0, url, d) { | 155 : TestURLFetcher(0, url, d) { |
156 set_url(url); | 156 set_url(url); |
157 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); | 157 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); |
158 set_response_code(net::HTTP_OK); | 158 set_response_code(net::HTTP_OK); |
159 } | 159 } |
160 | 160 |
161 HostedFetcher::~HostedFetcher() {} | 161 HostedFetcher::~HostedFetcher() {} |
162 | 162 |
163 void HostedFetcher::Start() { | 163 void HostedFetcher::Start() { |
164 VLOG(1) << upload_data(); | 164 VLOG(1) << upload_data(); |
165 if (upload_data().find("HOSTED") == std::string::npos) { | 165 if (upload_data().find("HOSTED") == std::string::npos) { |
166 VLOG(1) << "HostedFetcher failing request"; | 166 VLOG(1) << "HostedFetcher failing request"; |
167 set_response_code(net::HTTP_FORBIDDEN); | 167 set_response_code(net::HTTP_FORBIDDEN); |
168 SetResponseString("Error=BadAuthentication"); | 168 SetResponseString("Error=BadAuthentication"); |
169 } | 169 } |
170 delegate()->OnURLFetchComplete(this); | 170 delegate()->OnURLFetchComplete(this); |
171 } | 171 } |
172 | 172 |
173 } // namespace chromeos | 173 } // namespace chromeos |
OLD | NEW |