OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "components/copresence/rpc/rpc_handler.h" | 5 #include "components/copresence/rpc/rpc_handler.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 18 matching lines...) Expand all Loading... |
29 using testing::ElementsAre; | 29 using testing::ElementsAre; |
30 using testing::Property; | 30 using testing::Property; |
31 using testing::SizeIs; | 31 using testing::SizeIs; |
32 | 32 |
33 namespace copresence { | 33 namespace copresence { |
34 | 34 |
35 namespace { | 35 namespace { |
36 | 36 |
37 const char kChromeVersion[] = "Chrome Version String"; | 37 const char kChromeVersion[] = "Chrome Version String"; |
38 | 38 |
39 void CreateSubscribedMessage(const std::vector<std::string>& subscription_ids, | 39 void IgnoreMessages( |
40 const std::string& message_string, | 40 const RepeatedPtrField<SubscribedMessage>& /* messages */) {} |
41 SubscribedMessage* message_proto) { | |
42 message_proto->mutable_published_message()->set_payload(message_string); | |
43 for (const std::string& subscription_id : subscription_ids) { | |
44 message_proto->add_subscription_id(subscription_id); | |
45 } | |
46 } | |
47 | 41 |
48 } // namespace | 42 } // namespace |
49 | 43 |
50 class RpcHandlerTest : public testing::Test, public CopresenceDelegate { | 44 class RpcHandlerTest : public testing::Test, public CopresenceDelegate { |
51 public: | 45 public: |
52 RpcHandlerTest() | 46 RpcHandlerTest() |
53 : whispernet_client_(new StubWhispernetClient), | 47 : whispernet_client_(new StubWhispernetClient), |
54 // TODO(ckehoe): Use a FakeCopresenceState here | 48 // TODO(ckehoe): Use a FakeCopresenceState here |
55 // and test that it gets called correctly. | 49 // and test that it gets called correctly. |
56 state_(new CopresenceStateImpl), | 50 state_(new CopresenceStateImpl), |
57 rpc_handler_(this, | 51 rpc_handler_(this, |
58 state_.get(), | 52 state_.get(), |
59 &directive_handler_, | 53 &directive_handler_, |
60 nullptr, | 54 nullptr, |
| 55 base::Bind(&IgnoreMessages), |
61 base::Bind(&RpcHandlerTest::CaptureHttpPost, | 56 base::Bind(&RpcHandlerTest::CaptureHttpPost, |
62 base::Unretained(this))), | 57 base::Unretained(this))), |
63 status_(SUCCESS) {} | 58 status_(SUCCESS) {} |
64 | 59 |
65 // CopresenceDelegate implementation | 60 // CopresenceDelegate implementation |
66 | 61 |
67 void HandleMessages(const std::string& /* app_id */, | 62 void HandleMessages(const std::string& /* app_id */, |
68 const std::string& subscription_id, | 63 const std::string& subscription_id, |
69 const std::vector<Message>& messages) override { | 64 const std::vector<Message>& messages) override { |
70 // app_id is unused for now, pending a server fix. | 65 NOTREACHED(); |
71 for (const Message& message : messages) { | |
72 messages_by_subscription_[subscription_id].push_back(message.payload()); | |
73 } | |
74 } | 66 } |
75 | 67 |
76 void HandleStatusUpdate(CopresenceStatus /* status */) override {} | 68 void HandleStatusUpdate(CopresenceStatus /* status */) override { |
| 69 NOTREACHED(); |
| 70 } |
77 | 71 |
78 net::URLRequestContextGetter* GetRequestContext() const override { | 72 net::URLRequestContextGetter* GetRequestContext() const override { |
79 return nullptr; | 73 return nullptr; |
80 } | 74 } |
81 | 75 |
82 const std::string GetPlatformVersionString() const override { | 76 const std::string GetPlatformVersionString() const override { |
83 return kChromeVersion; | 77 return kChromeVersion; |
84 } | 78 } |
85 | 79 |
86 const std::string GetAPIKey(const std::string& app_id) const override { | 80 const std::string GetAPIKey(const std::string& app_id) const override { |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 scoped_ptr<WhispernetClient> whispernet_client_; | 154 scoped_ptr<WhispernetClient> whispernet_client_; |
161 FakeDirectiveHandler directive_handler_; | 155 FakeDirectiveHandler directive_handler_; |
162 scoped_ptr<CopresenceStateImpl> state_; | 156 scoped_ptr<CopresenceStateImpl> state_; |
163 RpcHandler rpc_handler_; | 157 RpcHandler rpc_handler_; |
164 | 158 |
165 CopresenceStatus status_; | 159 CopresenceStatus status_; |
166 std::string rpc_name_; | 160 std::string rpc_name_; |
167 std::string api_key_; | 161 std::string api_key_; |
168 std::string auth_token_; | 162 std::string auth_token_; |
169 ScopedVector<MessageLite> request_protos_; | 163 ScopedVector<MessageLite> request_protos_; |
170 std::map<std::string, std::vector<std::string>> messages_by_subscription_; | |
171 | 164 |
172 private: | 165 private: |
173 void CaptureHttpPost( | 166 void CaptureHttpPost( |
174 net::URLRequestContextGetter* url_context_getter, | 167 net::URLRequestContextGetter* url_context_getter, |
175 const std::string& rpc_name, | 168 const std::string& rpc_name, |
176 const std::string& api_key, | 169 const std::string& api_key, |
177 const std::string& auth_token, | 170 const std::string& auth_token, |
178 scoped_ptr<MessageLite> request_proto, | 171 scoped_ptr<MessageLite> request_proto, |
179 const RpcHandler::PostCleanupCallback& response_callback) { | 172 const RpcHandler::PostCleanupCallback& response_callback) { |
180 rpc_name_ = rpc_name; | 173 rpc_name_ = rpc_name; |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 Property(&TokenObservation::token_id, "current inaudible"))); | 306 Property(&TokenObservation::token_id, "current inaudible"))); |
314 } | 307 } |
315 | 308 |
316 TEST_F(RpcHandlerTest, ReportResponseHandler) { | 309 TEST_F(RpcHandlerTest, ReportResponseHandler) { |
317 // Fail on HTTP status != 200. | 310 // Fail on HTTP status != 200. |
318 scoped_ptr<ReportResponse> response(new ReportResponse); | 311 scoped_ptr<ReportResponse> response(new ReportResponse); |
319 status_ = SUCCESS; | 312 status_ = SUCCESS; |
320 SendReportResponse(net::HTTP_BAD_REQUEST, response.Pass()); | 313 SendReportResponse(net::HTTP_BAD_REQUEST, response.Pass()); |
321 EXPECT_EQ(FAIL, status_); | 314 EXPECT_EQ(FAIL, status_); |
322 | 315 |
323 // Construct test subscriptions. | |
324 std::vector<std::string> subscription_1(1, "Subscription 1"); | |
325 std::vector<std::string> subscription_2(1, "Subscription 2"); | |
326 std::vector<std::string> both_subscriptions; | |
327 both_subscriptions.push_back("Subscription 1"); | |
328 both_subscriptions.push_back("Subscription 2"); | |
329 | |
330 // Construct a test ReportResponse. | 316 // Construct a test ReportResponse. |
331 response.reset(new ReportResponse); | 317 response.reset(new ReportResponse); |
332 response->mutable_header()->mutable_status()->set_code(OK); | 318 response->mutable_header()->mutable_status()->set_code(OK); |
333 UpdateSignalsResponse* update_response = | 319 UpdateSignalsResponse* update_response = |
334 response->mutable_update_signals_response(); | 320 response->mutable_update_signals_response(); |
335 update_response->set_status(util::error::OK); | 321 update_response->set_status(util::error::OK); |
336 Token* invalid_token = update_response->add_token(); | 322 Token* invalid_token = update_response->add_token(); |
337 invalid_token->set_id("bad token"); | 323 invalid_token->set_id("bad token"); |
338 invalid_token->set_status(INVALID); | 324 invalid_token->set_status(INVALID); |
339 CreateSubscribedMessage( | |
340 subscription_1, "Message A", update_response->add_message()); | |
341 CreateSubscribedMessage( | |
342 subscription_2, "Message B", update_response->add_message()); | |
343 CreateSubscribedMessage( | |
344 both_subscriptions, "Message C", update_response->add_message()); | |
345 update_response->add_directive()->set_subscription_id("Subscription 1"); | 325 update_response->add_directive()->set_subscription_id("Subscription 1"); |
346 update_response->add_directive()->set_subscription_id("Subscription 2"); | 326 update_response->add_directive()->set_subscription_id("Subscription 2"); |
347 | 327 |
348 // Process it. | 328 // Check processing. |
349 messages_by_subscription_.clear(); | |
350 status_ = FAIL; | 329 status_ = FAIL; |
351 SendReportResponse(net::HTTP_OK, response.Pass()); | 330 SendReportResponse(net::HTTP_OK, response.Pass()); |
352 | |
353 // Check processing. | |
354 EXPECT_EQ(SUCCESS, status_); | 331 EXPECT_EQ(SUCCESS, status_); |
355 EXPECT_TRUE(TokenIsInvalid("bad token")); | 332 EXPECT_TRUE(TokenIsInvalid("bad token")); |
356 EXPECT_THAT(messages_by_subscription_["Subscription 1"], | |
357 ElementsAre("Message A", "Message C")); | |
358 EXPECT_THAT(messages_by_subscription_["Subscription 2"], | |
359 ElementsAre("Message B", "Message C")); | |
360 EXPECT_THAT(directive_handler_.added_directives(), | 333 EXPECT_THAT(directive_handler_.added_directives(), |
361 ElementsAre("Subscription 1", "Subscription 2")); | 334 ElementsAre("Subscription 1", "Subscription 2")); |
362 } | 335 } |
363 | 336 |
364 } // namespace copresence | 337 } // namespace copresence |
OLD | NEW |