protobuf_http_test_responder.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2020 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. #include "remoting/base/protobuf_http_test_responder.h"
  5. #include <algorithm>
  6. #include "base/run_loop.h"
  7. #include "net/http/http_status_code.h"
  8. #include "remoting/base/protobuf_http_client_messages.pb.h"
  9. #include "remoting/base/protobuf_http_status.h"
  10. #include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
  11. #include "third_party/protobuf/src/google/protobuf/message_lite.h"
  12. namespace remoting {
  13. namespace {
  14. protobufhttpclient::Status ToProtobufStatus(const ProtobufHttpStatus& status) {
  15. protobufhttpclient::Status result;
  16. result.set_code(static_cast<int>(status.error_code()));
  17. result.set_message(status.error_message());
  18. return result;
  19. }
  20. } // namespace
  21. ProtobufHttpTestResponder::ProtobufHttpTestResponder() = default;
  22. ProtobufHttpTestResponder::~ProtobufHttpTestResponder() = default;
  23. // static
  24. bool ProtobufHttpTestResponder::ParseRequestMessage(
  25. const network::ResourceRequest& resource_request,
  26. google::protobuf::MessageLite* out_message) {
  27. std::string unified_data;
  28. for (const auto& data_element : *resource_request.request_body->elements()) {
  29. if (data_element.type() == network::DataElement::Tag::kBytes) {
  30. const auto piece =
  31. data_element.As<network::DataElementBytes>().AsStringPiece();
  32. unified_data.append(piece.data(), piece.size());
  33. }
  34. }
  35. return out_message->ParseFromString(unified_data);
  36. }
  37. scoped_refptr<network::SharedURLLoaderFactory>
  38. ProtobufHttpTestResponder::GetUrlLoaderFactory() {
  39. return base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
  40. &test_url_loader_factory_);
  41. }
  42. void ProtobufHttpTestResponder::AddResponse(
  43. const std::string& url,
  44. const google::protobuf::MessageLite& response_message) {
  45. test_url_loader_factory_.AddResponse(url,
  46. response_message.SerializeAsString());
  47. }
  48. void ProtobufHttpTestResponder::AddResponseToMostRecentRequestUrl(
  49. const google::protobuf::MessageLite& response_message) {
  50. AddResponse(GetMostRecentRequestUrl(), response_message);
  51. }
  52. void ProtobufHttpTestResponder::AddError(
  53. const std::string& url,
  54. const ProtobufHttpStatus& error_status) {
  55. test_url_loader_factory_.AddResponse(
  56. url, ToProtobufStatus(error_status).SerializeAsString(),
  57. net::HTTP_INTERNAL_SERVER_ERROR);
  58. }
  59. void ProtobufHttpTestResponder::AddErrorToMostRecentRequestUrl(
  60. const ProtobufHttpStatus& error_status) {
  61. AddError(GetMostRecentRequestUrl(), error_status);
  62. }
  63. void ProtobufHttpTestResponder::AddStreamResponse(
  64. const std::string& url,
  65. const std::vector<const google::protobuf::MessageLite*>& messages,
  66. const ProtobufHttpStatus& status) {
  67. protobufhttpclient::StreamBody messages_body;
  68. for (const auto* message : messages) {
  69. messages_body.add_messages(message->SerializeAsString());
  70. }
  71. std::string stream_data = messages_body.SerializeAsString();
  72. protobufhttpclient::StreamBody status_body;
  73. *status_body.mutable_status() = ToProtobufStatus(status);
  74. stream_data += status_body.SerializeAsString();
  75. test_url_loader_factory_.AddResponse(url, stream_data);
  76. }
  77. void ProtobufHttpTestResponder::AddStreamResponseToMostRecentRequestUrl(
  78. const std::vector<const google::protobuf::MessageLite*>& messages,
  79. const ProtobufHttpStatus& status) {
  80. AddStreamResponse(GetMostRecentRequestUrl(), messages, status);
  81. }
  82. bool ProtobufHttpTestResponder::GetRequestMessage(
  83. const std::string& url,
  84. google::protobuf::MessageLite* out_message) {
  85. base::RunLoop().RunUntilIdle();
  86. auto pending_request_it = std::find_if(
  87. test_url_loader_factory_.pending_requests()->rbegin(),
  88. test_url_loader_factory_.pending_requests()->rend(),
  89. [url](const network::TestURLLoaderFactory::PendingRequest& request) {
  90. return request.request.url.spec() == url;
  91. });
  92. if (pending_request_it ==
  93. test_url_loader_factory_.pending_requests()->rend()) {
  94. return false;
  95. }
  96. return ParseRequestMessage(pending_request_it->request, out_message);
  97. }
  98. bool ProtobufHttpTestResponder::GetMostRecentRequestMessage(
  99. google::protobuf::MessageLite* out_message) {
  100. return ParseRequestMessage(GetMostRecentPendingRequest().request,
  101. out_message);
  102. }
  103. int ProtobufHttpTestResponder::GetNumPending() {
  104. base::RunLoop().RunUntilIdle();
  105. return test_url_loader_factory_.pending_requests()->size();
  106. }
  107. network::TestURLLoaderFactory::PendingRequest&
  108. ProtobufHttpTestResponder::GetPendingRequest(size_t index) {
  109. base::RunLoop().RunUntilIdle();
  110. DCHECK_LT(index, test_url_loader_factory_.pending_requests()->size());
  111. return (*test_url_loader_factory_.pending_requests())[index];
  112. }
  113. network::TestURLLoaderFactory::PendingRequest&
  114. ProtobufHttpTestResponder::GetMostRecentPendingRequest() {
  115. base::RunLoop().RunUntilIdle();
  116. DCHECK(!test_url_loader_factory_.pending_requests()->empty());
  117. return test_url_loader_factory_.pending_requests()->back();
  118. }
  119. std::string ProtobufHttpTestResponder::GetMostRecentRequestUrl() {
  120. return GetMostRecentPendingRequest().request.url.spec();
  121. }
  122. ProtobufHttpTestResponder::MockInterceptor&
  123. ProtobufHttpTestResponder::GetMockInterceptor() {
  124. if (!mock_interceptor_) {
  125. mock_interceptor_ = std::make_unique<MockInterceptor>();
  126. test_url_loader_factory_.SetInterceptor(mock_interceptor_->Get());
  127. }
  128. return *mock_interceptor_;
  129. }
  130. } // namespace remoting