- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / local_discovery / privet_http_unittest.cc
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
3 // found in the LICENSE file.
4
5 #include "base/bind.h"
6 #include "base/message_loop/message_loop.h"
7 #include "chrome/browser/local_discovery/privet_http_impl.h"
8 #include "net/base/host_port_pair.h"
9 #include "net/base/net_errors.h"
10 #include "net/url_request/test_url_fetcher_factory.h"
11 #include "net/url_request/url_request_test_util.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 using testing::StrictMock;
16 using testing::NiceMock;
17
18 namespace local_discovery {
19
20 namespace {
21
22 const char kSampleInfoResponse[] = "{"
23     "       \"version\": \"1.0\","
24     "       \"name\": \"Common printer\","
25     "       \"description\": \"Printer connected through Chrome connector\","
26     "       \"url\": \"https://www.google.com/cloudprint\","
27     "       \"type\": ["
28     "               \"printer\""
29     "       ],"
30     "       \"id\": \"\","
31     "       \"device_state\": \"idle\","
32     "       \"connection_state\": \"online\","
33     "       \"manufacturer\": \"Google\","
34     "       \"model\": \"Google Chrome\","
35     "       \"serial_number\": \"1111-22222-33333-4444\","
36     "       \"firmware\": \"24.0.1312.52\","
37     "       \"uptime\": 600,"
38     "       \"setup_url\": \"http://support.google.com/\","
39     "       \"support_url\": \"http://support.google.com/cloudprint/?hl=en\","
40     "       \"update_url\": \"http://support.google.com/cloudprint/?hl=en\","
41     "       \"x-privet-token\": \"SampleTokenForTesting\","
42     "       \"api\": ["
43     "               \"/privet/accesstoken\","
44     "               \"/privet/capabilities\","
45     "               \"/privet/printer/submitdoc\","
46     "       ]"
47     "}";
48
49 const char kSampleInfoResponseRegistered[] = "{"
50     "       \"version\": \"1.0\","
51     "       \"name\": \"Common printer\","
52     "       \"description\": \"Printer connected through Chrome connector\","
53     "       \"url\": \"https://www.google.com/cloudprint\","
54     "       \"type\": ["
55     "               \"printer\""
56     "       ],"
57     "       \"id\": \"MyDeviceID\","
58     "       \"device_state\": \"idle\","
59     "       \"connection_state\": \"online\","
60     "       \"manufacturer\": \"Google\","
61     "       \"model\": \"Google Chrome\","
62     "       \"serial_number\": \"1111-22222-33333-4444\","
63     "       \"firmware\": \"24.0.1312.52\","
64     "       \"uptime\": 600,"
65     "       \"setup_url\": \"http://support.google.com/\","
66     "       \"support_url\": \"http://support.google.com/cloudprint/?hl=en\","
67     "       \"update_url\": \"http://support.google.com/cloudprint/?hl=en\","
68     "       \"x-privet-token\": \"SampleTokenForTesting\","
69     "       \"api\": ["
70     "               \"/privet/accesstoken\","
71     "               \"/privet/capabilities\","
72     "               \"/privet/printer/submitdoc\","
73     "       ]"
74     "}";
75
76
77 const char kSampleRegisterStartResponse[] = "{"
78     "\"user\": \"example@google.com\","
79     "\"action\": \"start\""
80     "}";
81
82 const char kSampleRegisterGetClaimTokenResponse[] = "{"
83     "       \"action\": \"getClaimToken\","
84     "       \"user\": \"example@google.com\","
85     "       \"token\": \"MySampleToken\","
86     "       \"claim_url\": \"https://domain.com/SoMeUrL\""
87     "}";
88
89 const char kSampleRegisterCompleteResponse[] = "{"
90     "\"user\": \"example@google.com\","
91     "\"action\": \"complete\","
92     "\"device_id\": \"MyDeviceID\""
93     "}";
94
95 const char kSampleXPrivetErrorResponse[] =
96     "{ \"error\": \"invalid_x_privet_token\" }";
97
98 const char kSampleRegisterErrorTransient[] =
99     "{ \"error\": \"device_busy\", \"timeout\": 1}";
100
101 const char kSampleRegisterErrorPermanent[] =
102     "{ \"error\": \"user_cancel\" }";
103
104 const char kSampleInfoResponseBadJson[] = "{";
105
106 const char kSampleRegisterCancelResponse[] = "{"
107     "\"user\": \"example@google.com\","
108     "\"action\": \"cancel\""
109     "}";
110
111 const char kSampleLocalPrintResponse[] = "{"
112     "\"job_id\": \"123\","
113     "\"expires_in\": 500,"
114     "\"job_type\": \"application/pdf\","
115     "\"job_size\": 16,"
116     "\"job_name\": \"Sample job name\","
117     "}";
118
119 const char kSampleCapabilitiesResponse[] = "{"
120     "\"version\" : \"1.0\","
121     "\"printer\" : {"
122     "  \"supported_content_type\" : ["
123     "   { \"content_type\" : \"application/pdf\" },"
124     "   { \"content_type\" : \"image/pwg-raster\" }"
125     "  ]"
126     "}"
127     "}";
128
129 const char kSampleCapabilitiesResponsePWGOnly[] = "{"
130     "\"version\" : \"1.0\","
131     "\"printer\" : {"
132     "  \"supported_content_type\" : ["
133     "   { \"content_type\" : \"image/pwg-raster\" }"
134     "  ]"
135     "}"
136     "}";
137
138 class MockTestURLFetcherFactoryDelegate
139     : public net::TestURLFetcher::DelegateForTests {
140  public:
141   // Callback issued correspondingly to the call to the |Start()| method.
142   MOCK_METHOD1(OnRequestStart, void(int fetcher_id));
143
144   // Callback issued correspondingly to the call to |AppendChunkToUpload|.
145   // Uploaded chunks can be retrieved with the |upload_chunks()| getter.
146   MOCK_METHOD1(OnChunkUpload, void(int fetcher_id));
147
148   // Callback issued correspondingly to the destructor.
149   MOCK_METHOD1(OnRequestEnd, void(int fetcher_id));
150 };
151
152 class PrivetHTTPTest : public ::testing::Test {
153  public:
154   PrivetHTTPTest() {
155     request_context_= new net::TestURLRequestContextGetter(
156         base::MessageLoopProxy::current());
157     privet_client_.reset(new PrivetHTTPClientImpl(
158         "sampleDevice._privet._tcp.local",
159         net::HostPortPair("10.0.0.8", 6006),
160         request_context_.get()));
161     fetcher_factory_.SetDelegateForTests(&fetcher_delegate_);
162   }
163
164   virtual ~PrivetHTTPTest() {
165   }
166
167   bool SuccessfulResponseToURL(const GURL& url,
168                                const std::string& response) {
169     return SuccessfulResponseToURLAndData(url, "", response);
170   }
171
172   bool SuccessfulResponseToURLAndData(const GURL& url,
173                                       const std::string& data,
174                                       const std::string& response) {
175     net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
176     EXPECT_TRUE(fetcher);
177     EXPECT_EQ(url, fetcher->GetOriginalURL());
178
179     if (!data.empty()) {
180       EXPECT_EQ(data, fetcher->upload_data());
181     }
182
183     if (!fetcher || url != fetcher->GetOriginalURL() ||
184         (!data.empty() && data != fetcher->upload_data()))
185       return false;
186
187     fetcher->SetResponseString(response);
188     fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
189                                               net::OK));
190     fetcher->set_response_code(200);
191     fetcher->delegate()->OnURLFetchComplete(fetcher);
192     return true;
193   }
194
195  protected:
196   base::MessageLoop loop_;
197   scoped_refptr<net::TestURLRequestContextGetter> request_context_;
198   net::TestURLFetcherFactory fetcher_factory_;
199   scoped_ptr<PrivetHTTPClient> privet_client_;
200   NiceMock<MockTestURLFetcherFactoryDelegate> fetcher_delegate_;
201 };
202
203 class MockInfoDelegate : public PrivetInfoOperation::Delegate {
204  public:
205   MockInfoDelegate() {}
206   ~MockInfoDelegate() {}
207
208   virtual void OnPrivetInfoDone(PrivetInfoOperation* operation,
209                                 int response_code,
210                                 const base::DictionaryValue* value) OVERRIDE {
211     if (!value) {
212       value_.reset();
213     } else {
214       value_.reset(value->DeepCopy());
215     }
216
217     OnPrivetInfoDoneInternal(response_code);
218   }
219
220   MOCK_METHOD1(OnPrivetInfoDoneInternal, void(int response_code));
221
222   const base::DictionaryValue* value() { return value_.get(); }
223  protected:
224   scoped_ptr<base::DictionaryValue> value_;
225 };
226
227 class MockCapabilitiesDelegate : public PrivetCapabilitiesOperation::Delegate {
228  public:
229   MockCapabilitiesDelegate() {}
230   ~MockCapabilitiesDelegate() {}
231
232   virtual void OnPrivetCapabilities(
233       PrivetCapabilitiesOperation* operation,
234       int response_code,
235       const base::DictionaryValue* value) OVERRIDE {
236     if (!value) {
237       value_.reset();
238     } else {
239       value_.reset(value->DeepCopy());
240     }
241
242     OnPrivetCapabilitiesDoneInternal(response_code);
243   }
244
245   MOCK_METHOD1(OnPrivetCapabilitiesDoneInternal, void(int response_code));
246
247   const base::DictionaryValue* value() { return value_.get(); }
248  protected:
249   scoped_ptr<base::DictionaryValue> value_;
250 };
251
252 class MockRegisterDelegate : public PrivetRegisterOperation::Delegate {
253  public:
254   MockRegisterDelegate() {
255   }
256   ~MockRegisterDelegate() {
257   }
258
259   virtual void OnPrivetRegisterClaimToken(
260       PrivetRegisterOperation* operation,
261       const std::string& token,
262       const GURL& url) OVERRIDE {
263     OnPrivetRegisterClaimTokenInternal(token, url);
264   }
265
266   MOCK_METHOD2(OnPrivetRegisterClaimTokenInternal, void(
267       const std::string& token,
268       const GURL& url));
269
270   virtual void OnPrivetRegisterError(
271       PrivetRegisterOperation* operation,
272       const std::string& action,
273       PrivetRegisterOperation::FailureReason reason,
274       int printer_http_code,
275       const DictionaryValue* json) OVERRIDE {
276     // TODO(noamsml): Save and test for JSON?
277     OnPrivetRegisterErrorInternal(action, reason, printer_http_code);
278   }
279
280   MOCK_METHOD3(OnPrivetRegisterErrorInternal,
281                void(const std::string& action,
282                     PrivetRegisterOperation::FailureReason reason,
283                     int printer_http_code));
284
285   virtual void OnPrivetRegisterDone(
286       PrivetRegisterOperation* operation,
287       const std::string& device_id) OVERRIDE {
288     OnPrivetRegisterDoneInternal(device_id);
289   }
290
291   MOCK_METHOD1(OnPrivetRegisterDoneInternal,
292                void(const std::string& device_id));
293 };
294
295 class MockLocalPrintDelegate : public PrivetLocalPrintOperation::Delegate {
296  public:
297   MockLocalPrintDelegate() {}
298   ~MockLocalPrintDelegate() {}
299
300   virtual void OnPrivetPrintingRequestPDF(
301       const PrivetLocalPrintOperation* print_operation) {
302     OnPrivetPrintingRequestPDFInternal();
303   }
304
305   MOCK_METHOD0(OnPrivetPrintingRequestPDFInternal, void());
306
307   virtual void OnPrivetPrintingRequestPWGRaster(
308       const PrivetLocalPrintOperation* print_operation) {
309     OnPrivetPrintingRequestPWGRasterInternal();
310   }
311
312   MOCK_METHOD0(OnPrivetPrintingRequestPWGRasterInternal, void());
313
314   virtual void OnPrivetPrintingDone(
315       const PrivetLocalPrintOperation* print_operation) {
316     OnPrivetPrintingDoneInternal();
317   }
318
319   MOCK_METHOD0(OnPrivetPrintingDoneInternal, void());
320
321   virtual void OnPrivetPrintingError(
322       const PrivetLocalPrintOperation* print_operation, int http_code) {
323     OnPrivetPrintingErrorInternal(http_code);
324   }
325
326   MOCK_METHOD1(OnPrivetPrintingErrorInternal, void(int http_code));
327 };
328
329 class PrivetInfoTest : public PrivetHTTPTest {
330  public:
331   PrivetInfoTest() {}
332
333   virtual ~PrivetInfoTest() {}
334
335   virtual void SetUp() OVERRIDE {
336     info_operation_ = privet_client_->CreateInfoOperation(&info_delegate_);
337   }
338
339  protected:
340   scoped_ptr<PrivetInfoOperation> info_operation_;
341   StrictMock<MockInfoDelegate> info_delegate_;
342 };
343
344 TEST_F(PrivetInfoTest, SuccessfulInfo) {
345   info_operation_->Start();
346
347   net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
348   ASSERT_TRUE(fetcher != NULL);
349   EXPECT_EQ(GURL("http://10.0.0.8:6006/privet/info"),
350             fetcher->GetOriginalURL());
351
352   fetcher->SetResponseString(kSampleInfoResponse);
353   fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
354                                             net::OK));
355   fetcher->set_response_code(200);
356
357   EXPECT_CALL(info_delegate_, OnPrivetInfoDoneInternal(200));
358   fetcher->delegate()->OnURLFetchComplete(fetcher);
359
360   std::string name;
361
362   privet_client_->GetCachedInfo()->GetString("name", &name);
363   EXPECT_EQ("Common printer", name);
364 };
365
366 TEST_F(PrivetInfoTest, InfoSaveToken) {
367   info_operation_->Start();
368
369   net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
370   ASSERT_TRUE(fetcher != NULL);
371   fetcher->SetResponseString(kSampleInfoResponse);
372   fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
373                                             net::OK));
374   fetcher->set_response_code(200);
375
376   EXPECT_CALL(info_delegate_, OnPrivetInfoDoneInternal(200));
377   fetcher->delegate()->OnURLFetchComplete(fetcher);
378
379   info_operation_ = privet_client_->CreateInfoOperation(&info_delegate_);
380   info_operation_->Start();
381
382   fetcher = fetcher_factory_.GetFetcherByID(0);
383   ASSERT_TRUE(fetcher != NULL);
384   net::HttpRequestHeaders headers;
385   fetcher->GetExtraRequestHeaders(&headers);
386   std::string header_token;
387   ASSERT_TRUE(headers.GetHeader("X-Privet-Token", &header_token));
388   EXPECT_EQ("SampleTokenForTesting", header_token);
389 };
390
391 TEST_F(PrivetInfoTest, InfoFailureHTTP) {
392   info_operation_->Start();
393
394   net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
395   ASSERT_TRUE(fetcher != NULL);
396   fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
397                                             net::OK));
398   fetcher->set_response_code(404);
399
400   EXPECT_CALL(info_delegate_, OnPrivetInfoDoneInternal(404));
401   fetcher->delegate()->OnURLFetchComplete(fetcher);
402   EXPECT_EQ(NULL, privet_client_->GetCachedInfo());
403 };
404
405 TEST_F(PrivetInfoTest, InfoFailureInternal) {
406   info_operation_->Start();
407
408   net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
409   ASSERT_TRUE(fetcher != NULL);
410   fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
411                                             net::OK));
412   fetcher->set_response_code(200);
413
414   EXPECT_CALL(info_delegate_, OnPrivetInfoDoneInternal(-1));
415   fetcher->delegate()->OnURLFetchComplete(fetcher);
416   EXPECT_EQ(NULL, privet_client_->GetCachedInfo());
417 };
418
419 class PrivetRegisterTest : public PrivetHTTPTest {
420  public:
421   PrivetRegisterTest() {
422   }
423   virtual ~PrivetRegisterTest() {
424   }
425
426   virtual void SetUp() OVERRIDE {
427     info_operation_ = privet_client_->CreateInfoOperation(&info_delegate_);
428     register_operation_ =
429         privet_client_->CreateRegisterOperation("example@google.com",
430                                                 &register_delegate_);
431   }
432
433  protected:
434   bool SuccessfulResponseToURL(const GURL& url,
435                                const std::string& response) {
436     net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
437     EXPECT_TRUE(fetcher);
438     EXPECT_EQ(url, fetcher->GetOriginalURL());
439     if (!fetcher || url != fetcher->GetOriginalURL())
440       return false;
441
442     fetcher->SetResponseString(response);
443     fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
444                                               net::OK));
445     fetcher->set_response_code(200);
446     fetcher->delegate()->OnURLFetchComplete(fetcher);
447     return true;
448   }
449
450   void RunFor(base::TimeDelta time_period) {
451     base::CancelableCallback<void()> callback(base::Bind(
452         &PrivetRegisterTest::Stop, base::Unretained(this)));
453     base::MessageLoop::current()->PostDelayedTask(
454         FROM_HERE, callback.callback(), time_period);
455
456     base::MessageLoop::current()->Run();
457     callback.Cancel();
458   }
459
460   void Stop() {
461     base::MessageLoop::current()->Quit();
462   }
463
464   scoped_ptr<PrivetInfoOperation> info_operation_;
465   NiceMock<MockInfoDelegate> info_delegate_;
466   scoped_ptr<PrivetRegisterOperation> register_operation_;
467   StrictMock<MockRegisterDelegate> register_delegate_;
468 };
469
470 TEST_F(PrivetRegisterTest, RegisterSuccessSimple) {
471   // Start with info request first to populate XSRF token.
472   info_operation_->Start();
473
474   EXPECT_TRUE(SuccessfulResponseToURL(
475       GURL("http://10.0.0.8:6006/privet/info"),
476       kSampleInfoResponse));
477
478   register_operation_->Start();
479
480   EXPECT_TRUE(SuccessfulResponseToURL(
481       GURL("http://10.0.0.8:6006/privet/register?"
482            "action=start&user=example%40google.com"),
483       kSampleRegisterStartResponse));
484
485   EXPECT_CALL(register_delegate_, OnPrivetRegisterClaimTokenInternal(
486       "MySampleToken",
487       GURL("https://domain.com/SoMeUrL")));
488
489   EXPECT_TRUE(SuccessfulResponseToURL(
490       GURL("http://10.0.0.8:6006/privet/register?"
491            "action=getClaimToken&user=example%40google.com"),
492       kSampleRegisterGetClaimTokenResponse));
493
494   register_operation_->CompleteRegistration();
495
496   EXPECT_TRUE(SuccessfulResponseToURL(
497       GURL("http://10.0.0.8:6006/privet/register?"
498            "action=complete&user=example%40google.com"),
499       kSampleRegisterCompleteResponse));
500
501   EXPECT_CALL(register_delegate_, OnPrivetRegisterDoneInternal(
502       "MyDeviceID"));
503
504   EXPECT_TRUE(SuccessfulResponseToURL(
505       GURL("http://10.0.0.8:6006/privet/info"),
506       kSampleInfoResponseRegistered));
507 }
508
509 TEST_F(PrivetRegisterTest, RegisterNoInfoCall) {
510   register_operation_->Start();
511
512   EXPECT_TRUE(SuccessfulResponseToURL(
513       GURL("http://10.0.0.8:6006/privet/info"),
514       kSampleInfoResponse));
515
516   EXPECT_TRUE(SuccessfulResponseToURL(
517       GURL("http://10.0.0.8:6006/privet/register?"
518            "action=start&user=example%40google.com"),
519       kSampleRegisterStartResponse));
520 }
521
522 TEST_F(PrivetRegisterTest, RegisterXSRFFailure) {
523   register_operation_->Start();
524
525   EXPECT_TRUE(SuccessfulResponseToURL(
526       GURL("http://10.0.0.8:6006/privet/info"),
527       kSampleInfoResponse));
528
529   EXPECT_TRUE(SuccessfulResponseToURL(
530       GURL("http://10.0.0.8:6006/privet/register?"
531            "action=start&user=example%40google.com"),
532       kSampleRegisterStartResponse));
533
534   EXPECT_TRUE(SuccessfulResponseToURL(
535       GURL("http://10.0.0.8:6006/privet/register?"
536            "action=getClaimToken&user=example%40google.com"),
537       kSampleXPrivetErrorResponse));
538
539   EXPECT_TRUE(SuccessfulResponseToURL(
540       GURL("http://10.0.0.8:6006/privet/info"),
541       kSampleInfoResponse));
542
543   EXPECT_CALL(register_delegate_, OnPrivetRegisterClaimTokenInternal(
544       "MySampleToken", GURL("https://domain.com/SoMeUrL")));
545
546   EXPECT_TRUE(SuccessfulResponseToURL(
547       GURL("http://10.0.0.8:6006/privet/register?"
548            "action=getClaimToken&user=example%40google.com"),
549       kSampleRegisterGetClaimTokenResponse));
550 }
551
552 TEST_F(PrivetRegisterTest, TransientFailure) {
553   register_operation_->Start();
554
555   EXPECT_TRUE(SuccessfulResponseToURL(
556       GURL("http://10.0.0.8:6006/privet/info"),
557       kSampleInfoResponse));
558
559   EXPECT_TRUE(SuccessfulResponseToURL(
560       GURL("http://10.0.0.8:6006/privet/register?"
561            "action=start&user=example%40google.com"),
562       kSampleRegisterErrorTransient));
563
564   EXPECT_CALL(fetcher_delegate_, OnRequestStart(0));
565
566   RunFor(base::TimeDelta::FromSeconds(2));
567
568   testing::Mock::VerifyAndClearExpectations(&fetcher_delegate_);
569
570   EXPECT_TRUE(SuccessfulResponseToURL(
571       GURL("http://10.0.0.8:6006/privet/register?"
572            "action=start&user=example%40google.com"),
573       kSampleRegisterStartResponse));
574 }
575
576 TEST_F(PrivetRegisterTest, PermanentFailure) {
577   register_operation_->Start();
578
579   EXPECT_TRUE(SuccessfulResponseToURL(
580       GURL("http://10.0.0.8:6006/privet/info"),
581       kSampleInfoResponse));
582
583   EXPECT_TRUE(SuccessfulResponseToURL(
584       GURL("http://10.0.0.8:6006/privet/register?"
585            "action=start&user=example%40google.com"),
586       kSampleRegisterStartResponse));
587
588   EXPECT_CALL(register_delegate_,
589               OnPrivetRegisterErrorInternal(
590                   "getClaimToken",
591                   PrivetRegisterOperation::FAILURE_JSON_ERROR,
592                   200));
593
594   EXPECT_TRUE(SuccessfulResponseToURL(
595       GURL("http://10.0.0.8:6006/privet/register?"
596            "action=getClaimToken&user=example%40google.com"),
597       kSampleRegisterErrorPermanent));
598 }
599
600 TEST_F(PrivetRegisterTest, InfoFailure) {
601   register_operation_->Start();
602
603   EXPECT_CALL(register_delegate_,
604               OnPrivetRegisterErrorInternal(
605                   "start",
606                   PrivetRegisterOperation::FAILURE_TOKEN,
607                   -1));
608
609   EXPECT_TRUE(SuccessfulResponseToURL(
610       GURL("http://10.0.0.8:6006/privet/info"),
611       kSampleInfoResponseBadJson));
612 }
613
614 TEST_F(PrivetRegisterTest, RegisterCancel) {
615   // Start with info request first to populate XSRF token.
616   info_operation_->Start();
617
618   EXPECT_TRUE(SuccessfulResponseToURL(
619       GURL("http://10.0.0.8:6006/privet/info"),
620       kSampleInfoResponse));
621
622   register_operation_->Start();
623
624   EXPECT_TRUE(SuccessfulResponseToURL(
625       GURL("http://10.0.0.8:6006/privet/register?"
626            "action=start&user=example%40google.com"),
627       kSampleRegisterStartResponse));
628
629   register_operation_->Cancel();
630
631   EXPECT_TRUE(SuccessfulResponseToURL(
632       GURL("http://10.0.0.8:6006/privet/register?"
633            "action=cancel&user=example%40google.com"),
634       kSampleRegisterCancelResponse));
635
636   // Must keep mocks alive for 3 seconds so the cancelation object can be
637   // deleted.
638   RunFor(base::TimeDelta::FromSeconds(3));
639 }
640
641 class PrivetCapabilitiesTest : public PrivetHTTPTest {
642  public:
643   PrivetCapabilitiesTest() {}
644
645   virtual ~PrivetCapabilitiesTest() {}
646
647   virtual void SetUp() OVERRIDE {
648     capabilities_operation_ = privet_client_->CreateCapabilitiesOperation(
649         &capabilities_delegate_);
650   }
651
652  protected:
653   scoped_ptr<PrivetCapabilitiesOperation> capabilities_operation_;
654   StrictMock<MockCapabilitiesDelegate> capabilities_delegate_;
655 };
656
657 TEST_F(PrivetCapabilitiesTest, SuccessfulCapabilities) {
658   capabilities_operation_->Start();
659
660   EXPECT_TRUE(SuccessfulResponseToURL(
661       GURL("http://10.0.0.8:6006/privet/info"),
662       kSampleInfoResponse));
663
664   EXPECT_CALL(capabilities_delegate_, OnPrivetCapabilitiesDoneInternal(200));
665
666   EXPECT_TRUE(SuccessfulResponseToURL(
667       GURL("http://10.0.0.8:6006/privet/capabilities"),
668       kSampleCapabilitiesResponse));
669
670   std::string version;
671   EXPECT_TRUE(capabilities_delegate_.value()->GetString("version", &version));
672   EXPECT_EQ("1.0", version);
673 };
674
675 TEST_F(PrivetCapabilitiesTest, CacheToken) {
676   capabilities_operation_->Start();
677
678   EXPECT_TRUE(SuccessfulResponseToURL(
679       GURL("http://10.0.0.8:6006/privet/info"),
680       kSampleInfoResponse));
681
682   EXPECT_CALL(capabilities_delegate_, OnPrivetCapabilitiesDoneInternal(200));
683
684   EXPECT_TRUE(SuccessfulResponseToURL(
685       GURL("http://10.0.0.8:6006/privet/capabilities"),
686       kSampleCapabilitiesResponse));
687
688   capabilities_operation_ = privet_client_->CreateCapabilitiesOperation(
689       &capabilities_delegate_);
690
691   capabilities_operation_->Start();
692
693   EXPECT_CALL(capabilities_delegate_, OnPrivetCapabilitiesDoneInternal(200));
694
695   EXPECT_TRUE(SuccessfulResponseToURL(
696       GURL("http://10.0.0.8:6006/privet/capabilities"),
697       kSampleCapabilitiesResponse));
698 };
699
700 TEST_F(PrivetCapabilitiesTest, BadToken) {
701   capabilities_operation_->Start();
702
703   EXPECT_TRUE(SuccessfulResponseToURL(
704       GURL("http://10.0.0.8:6006/privet/info"),
705       kSampleInfoResponse));
706
707   EXPECT_TRUE(SuccessfulResponseToURL(
708       GURL("http://10.0.0.8:6006/privet/capabilities"),
709       kSampleXPrivetErrorResponse));
710
711   EXPECT_TRUE(SuccessfulResponseToURL(
712       GURL("http://10.0.0.8:6006/privet/info"),
713       kSampleInfoResponse));
714
715   EXPECT_CALL(capabilities_delegate_, OnPrivetCapabilitiesDoneInternal(200));
716
717   EXPECT_TRUE(SuccessfulResponseToURL(
718       GURL("http://10.0.0.8:6006/privet/capabilities"),
719       kSampleCapabilitiesResponse));
720 };
721
722 class PrivetLocalPrintTest : public PrivetHTTPTest {
723  public:
724   PrivetLocalPrintTest() {}
725
726   virtual ~PrivetLocalPrintTest() {}
727
728   virtual void SetUp() OVERRIDE {
729     local_print_operation_ = privet_client_->CreateLocalPrintOperation(
730         &local_print_delegate_);
731   }
732
733  protected:
734   scoped_ptr<PrivetLocalPrintOperation> local_print_operation_;
735   StrictMock<MockLocalPrintDelegate> local_print_delegate_;
736 };
737
738 TEST_F(PrivetLocalPrintTest, SuccessfulLocalPrint) {
739   local_print_operation_->SetUsername("sample@gmail.com");
740   local_print_operation_->SetJobname("Sample job name");
741   local_print_operation_->Start();
742
743   EXPECT_TRUE(SuccessfulResponseToURL(
744       GURL("http://10.0.0.8:6006/privet/info"),
745       kSampleInfoResponse));
746
747   EXPECT_CALL(local_print_delegate_, OnPrivetPrintingRequestPDFInternal());
748
749   EXPECT_TRUE(SuccessfulResponseToURL(
750       GURL("http://10.0.0.8:6006/privet/capabilities"),
751       kSampleCapabilitiesResponse));
752
753   local_print_operation_->SendData("Sample print data");
754
755   EXPECT_CALL(local_print_delegate_, OnPrivetPrintingDoneInternal());
756
757   // TODO(noamsml): Is encoding spaces as pluses standard?
758   EXPECT_TRUE(SuccessfulResponseToURLAndData(
759       GURL("http://10.0.0.8:6006/privet/printer/submitdoc?"
760            "user=sample%40gmail.com&jobname=Sample+job+name"),
761       "Sample print data",
762       kSampleLocalPrintResponse));
763 };
764
765 TEST_F(PrivetLocalPrintTest, SuccessfulPWGLocalPrint) {
766   local_print_operation_->SetUsername("sample@gmail.com");
767   local_print_operation_->SetJobname("Sample job name");
768   local_print_operation_->Start();
769
770   EXPECT_TRUE(SuccessfulResponseToURL(
771       GURL("http://10.0.0.8:6006/privet/info"),
772       kSampleInfoResponse));
773
774   EXPECT_CALL(local_print_delegate_,
775               OnPrivetPrintingRequestPWGRasterInternal());
776
777   EXPECT_TRUE(SuccessfulResponseToURL(
778       GURL("http://10.0.0.8:6006/privet/capabilities"),
779       kSampleCapabilitiesResponsePWGOnly));
780
781   local_print_operation_->SendData("Sample print data");
782
783   EXPECT_CALL(local_print_delegate_, OnPrivetPrintingDoneInternal());
784
785   // TODO(noamsml): Is encoding spaces as pluses standard?
786   EXPECT_TRUE(SuccessfulResponseToURLAndData(
787       GURL("http://10.0.0.8:6006/privet/printer/submitdoc?"
788            "user=sample%40gmail.com&jobname=Sample+job+name"),
789       "Sample print data",
790       kSampleLocalPrintResponse));
791 };
792
793 }  // namespace
794
795 }  // namespace local_discovery