OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
7 #include "chrome/browser/local_discovery/privet_http_impl.h" | 7 #include "chrome/browser/local_discovery/privet_http_impl.h" |
8 #include "net/base/host_port_pair.h" | 8 #include "net/base/host_port_pair.h" |
9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
10 #include "net/url_request/test_url_fetcher_factory.h" | 10 #include "net/url_request/test_url_fetcher_factory.h" |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 net::HostPortPair("10.0.0.8", 6006), | 203 net::HostPortPair("10.0.0.8", 6006), |
204 request_context_.get())); | 204 request_context_.get())); |
205 fetcher_factory_.SetDelegateForTests(&fetcher_delegate_); | 205 fetcher_factory_.SetDelegateForTests(&fetcher_delegate_); |
206 } | 206 } |
207 | 207 |
208 virtual ~PrivetHTTPTest() { | 208 virtual ~PrivetHTTPTest() { |
209 } | 209 } |
210 | 210 |
211 bool SuccessfulResponseToURL(const GURL& url, | 211 bool SuccessfulResponseToURL(const GURL& url, |
212 const std::string& response) { | 212 const std::string& response) { |
213 return SuccessfulResponseToURLAndData(url, "", response); | |
214 } | |
215 | |
216 bool SuccessfulResponseToURLAndData(const GURL& url, | |
217 const std::string& data, | |
218 const std::string& response) { | |
219 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); | 213 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); |
220 EXPECT_TRUE(fetcher); | 214 EXPECT_TRUE(fetcher); |
221 EXPECT_EQ(url, fetcher->GetOriginalURL()); | 215 EXPECT_EQ(url, fetcher->GetOriginalURL()); |
222 | 216 |
223 if (!data.empty()) { | 217 if (!fetcher || url != fetcher->GetOriginalURL()) |
224 EXPECT_EQ(data, fetcher->upload_data()); | |
225 } | |
226 | |
227 if (!fetcher || url != fetcher->GetOriginalURL() || | |
228 (!data.empty() && data != fetcher->upload_data())) | |
229 return false; | 218 return false; |
230 | 219 |
231 fetcher->SetResponseString(response); | 220 fetcher->SetResponseString(response); |
232 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, | 221 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, |
233 net::OK)); | 222 net::OK)); |
234 fetcher->set_response_code(200); | 223 fetcher->set_response_code(200); |
235 fetcher->delegate()->OnURLFetchComplete(fetcher); | 224 fetcher->delegate()->OnURLFetchComplete(fetcher); |
236 return true; | 225 return true; |
237 } | 226 } |
238 | 227 |
| 228 bool SuccessfulResponseToURLAndData(const GURL& url, |
| 229 const std::string& data, |
| 230 const std::string& response) { |
| 231 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); |
| 232 EXPECT_TRUE(fetcher); |
| 233 EXPECT_EQ(url, fetcher->GetOriginalURL()); |
| 234 |
| 235 if (!fetcher) return false; |
| 236 |
| 237 EXPECT_EQ(data, fetcher->upload_data()); |
| 238 if (data != fetcher->upload_data()) return false; |
| 239 |
| 240 return SuccessfulResponseToURL(url, response); |
| 241 } |
| 242 |
| 243 bool SuccessfulResponseToURLAndFilePath(const GURL& url, |
| 244 const base::FilePath& file_path, |
| 245 const std::string& response) { |
| 246 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); |
| 247 EXPECT_TRUE(fetcher); |
| 248 EXPECT_EQ(url, fetcher->GetOriginalURL()); |
| 249 |
| 250 if (!fetcher) return false; |
| 251 |
| 252 EXPECT_EQ(file_path, fetcher->upload_file_path()); |
| 253 if (file_path != fetcher->upload_file_path()) return false; |
| 254 |
| 255 return SuccessfulResponseToURL(url, response); |
| 256 } |
| 257 |
| 258 |
239 void RunFor(base::TimeDelta time_period) { | 259 void RunFor(base::TimeDelta time_period) { |
240 base::CancelableCallback<void()> callback(base::Bind( | 260 base::CancelableCallback<void()> callback(base::Bind( |
241 &PrivetHTTPTest::Stop, base::Unretained(this))); | 261 &PrivetHTTPTest::Stop, base::Unretained(this))); |
242 base::MessageLoop::current()->PostDelayedTask( | 262 base::MessageLoop::current()->PostDelayedTask( |
243 FROM_HERE, callback.callback(), time_period); | 263 FROM_HERE, callback.callback(), time_period); |
244 | 264 |
245 base::MessageLoop::current()->Run(); | 265 base::MessageLoop::current()->Run(); |
246 callback.Cancel(); | 266 callback.Cancel(); |
247 } | 267 } |
248 | 268 |
(...skipping 579 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
828 GURL("http://10.0.0.8:6006/privet/info"), | 848 GURL("http://10.0.0.8:6006/privet/info"), |
829 kSampleInfoResponse)); | 849 kSampleInfoResponse)); |
830 | 850 |
831 EXPECT_CALL(local_print_delegate_, | 851 EXPECT_CALL(local_print_delegate_, |
832 OnPrivetPrintingRequestPWGRasterInternal()); | 852 OnPrivetPrintingRequestPWGRasterInternal()); |
833 | 853 |
834 EXPECT_TRUE(SuccessfulResponseToURL( | 854 EXPECT_TRUE(SuccessfulResponseToURL( |
835 GURL("http://10.0.0.8:6006/privet/capabilities"), | 855 GURL("http://10.0.0.8:6006/privet/capabilities"), |
836 kSampleCapabilitiesResponsePWGOnly)); | 856 kSampleCapabilitiesResponsePWGOnly)); |
837 | 857 |
838 local_print_operation_->SendData("Sample print data"); | 858 local_print_operation_->SendDataFile( |
| 859 base::FilePath(FILE_PATH_LITERAL("sample/file/path"))); |
839 | 860 |
840 EXPECT_CALL(local_print_delegate_, OnPrivetPrintingDoneInternal()); | 861 EXPECT_CALL(local_print_delegate_, OnPrivetPrintingDoneInternal()); |
841 | 862 |
842 // TODO(noamsml): Is encoding spaces as pluses standard? | 863 // TODO(noamsml): Is encoding spaces as pluses standard? |
843 EXPECT_TRUE(SuccessfulResponseToURLAndData( | 864 EXPECT_TRUE(SuccessfulResponseToURLAndFilePath( |
844 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?" | 865 GURL("http://10.0.0.8:6006/privet/printer/submitdoc?" |
845 "user=sample%40gmail.com&jobname=Sample+job+name"), | 866 "user=sample%40gmail.com&jobname=Sample+job+name"), |
846 "Sample print data", | 867 base::FilePath(FILE_PATH_LITERAL("sample/file/path")), |
847 kSampleLocalPrintResponse)); | 868 kSampleLocalPrintResponse)); |
848 }; | 869 }; |
849 | 870 |
850 TEST_F(PrivetLocalPrintTest, SuccessfulLocalPrintWithCreatejob) { | 871 TEST_F(PrivetLocalPrintTest, SuccessfulLocalPrintWithCreatejob) { |
851 local_print_operation_->SetUsername("sample@gmail.com"); | 872 local_print_operation_->SetUsername("sample@gmail.com"); |
852 local_print_operation_->SetJobname("Sample job name"); | 873 local_print_operation_->SetJobname("Sample job name"); |
853 local_print_operation_->SetTicket("Sample print ticket"); | 874 local_print_operation_->SetTicket("Sample print ticket"); |
854 local_print_operation_->Start(); | 875 local_print_operation_->Start(); |
855 | 876 |
856 EXPECT_TRUE(SuccessfulResponseToURL( | 877 EXPECT_TRUE(SuccessfulResponseToURL( |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
913 | 934 |
914 EXPECT_TRUE(SuccessfulResponseToURL( | 935 EXPECT_TRUE(SuccessfulResponseToURL( |
915 GURL("http://10.0.0.8:6006/privet/printer/createjob"), | 936 GURL("http://10.0.0.8:6006/privet/printer/createjob"), |
916 kSampleCreatejobResponse)); | 937 kSampleCreatejobResponse)); |
917 }; | 938 }; |
918 | 939 |
919 | 940 |
920 } // namespace | 941 } // namespace |
921 | 942 |
922 } // namespace local_discovery | 943 } // namespace local_discovery |
OLD | NEW |