File: protobuf_http_test_responder.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (159 lines) | stat: -rw-r--r-- 5,665 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/base/protobuf_http_test_responder.h"

#include <algorithm>

#include "base/containers/adapters.h"
#include "base/run_loop.h"
#include "net/http/http_status_code.h"
#include "remoting/base/http_status.h"
#include "remoting/base/protobuf_http_client_messages.pb.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "third_party/protobuf/src/google/protobuf/message_lite.h"

namespace remoting {

namespace {

protobufhttpclient::Status ToProtobufStatus(const HttpStatus& status) {
  protobufhttpclient::Status result;
  result.set_code(static_cast<int>(status.error_code()));
  result.set_message(status.error_message());
  return result;
}

}  // namespace

ProtobufHttpTestResponder::ProtobufHttpTestResponder() = default;

ProtobufHttpTestResponder::~ProtobufHttpTestResponder() = default;

// static
bool ProtobufHttpTestResponder::ParseRequestMessage(
    const network::ResourceRequest& resource_request,
    google::protobuf::MessageLite* out_message) {
  std::string unified_data;
  for (const auto& data_element : *resource_request.request_body->elements()) {
    if (data_element.type() == network::DataElement::Tag::kBytes) {
      const auto piece =
          data_element.As<network::DataElementBytes>().AsStringPiece();
      unified_data.append(piece);
    }
  }
  return out_message->ParseFromString(unified_data);
}

scoped_refptr<network::SharedURLLoaderFactory>
ProtobufHttpTestResponder::GetUrlLoaderFactory() {
  return base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
      &test_url_loader_factory_);
}

void ProtobufHttpTestResponder::AddResponse(
    const std::string& url,
    const google::protobuf::MessageLite& response_message) {
  test_url_loader_factory_.AddResponse(url,
                                       response_message.SerializeAsString());
}

void ProtobufHttpTestResponder::AddResponse(const std::string& url,
                                            const std::string& response) {
  test_url_loader_factory_.AddResponse(url, response);
}

void ProtobufHttpTestResponder::AddResponseToMostRecentRequestUrl(
    const google::protobuf::MessageLite& response_message) {
  AddResponse(GetMostRecentRequestUrl(), response_message);
}

void ProtobufHttpTestResponder::AddError(const std::string& url,
                                         const HttpStatus& error_status) {
  test_url_loader_factory_.AddResponse(
      url, ToProtobufStatus(error_status).SerializeAsString(),
      net::HTTP_INTERNAL_SERVER_ERROR);
}

void ProtobufHttpTestResponder::AddErrorToMostRecentRequestUrl(
    const HttpStatus& error_status) {
  AddError(GetMostRecentRequestUrl(), error_status);
}

void ProtobufHttpTestResponder::AddStreamResponse(
    const std::string& url,
    const std::vector<const google::protobuf::MessageLite*>& messages,
    const HttpStatus& status) {
  protobufhttpclient::StreamBody messages_body;
  for (const auto* message : messages) {
    messages_body.add_messages(message->SerializeAsString());
  }
  std::string stream_data = messages_body.SerializeAsString();
  protobufhttpclient::StreamBody status_body;
  *status_body.mutable_status() = ToProtobufStatus(status);
  stream_data += status_body.SerializeAsString();
  test_url_loader_factory_.AddResponse(url, stream_data);
}

void ProtobufHttpTestResponder::AddStreamResponseToMostRecentRequestUrl(
    const std::vector<const google::protobuf::MessageLite*>& messages,
    const HttpStatus& status) {
  AddStreamResponse(GetMostRecentRequestUrl(), messages, status);
}

bool ProtobufHttpTestResponder::GetRequestMessage(
    const std::string& url,
    google::protobuf::MessageLite* out_message) {
  base::RunLoop().RunUntilIdle();
  auto pending_request_it = std::ranges::find(
      base::Reversed(*test_url_loader_factory_.pending_requests()), url,
      [](const network::TestURLLoaderFactory::PendingRequest& request) {
        return request.request.url.spec();
      });
  if (pending_request_it ==
      test_url_loader_factory_.pending_requests()->rend()) {
    return false;
  }
  return ParseRequestMessage(pending_request_it->request, out_message);
}

bool ProtobufHttpTestResponder::GetMostRecentRequestMessage(
    google::protobuf::MessageLite* out_message) {
  return ParseRequestMessage(GetMostRecentPendingRequest().request,
                             out_message);
}

int ProtobufHttpTestResponder::GetNumPending() {
  base::RunLoop().RunUntilIdle();
  return test_url_loader_factory_.pending_requests()->size();
}

network::TestURLLoaderFactory::PendingRequest&
ProtobufHttpTestResponder::GetPendingRequest(size_t index) {
  base::RunLoop().RunUntilIdle();
  DCHECK_LT(index, test_url_loader_factory_.pending_requests()->size());
  return (*test_url_loader_factory_.pending_requests())[index];
}

network::TestURLLoaderFactory::PendingRequest&
ProtobufHttpTestResponder::GetMostRecentPendingRequest() {
  base::RunLoop().RunUntilIdle();
  DCHECK(!test_url_loader_factory_.pending_requests()->empty());
  return test_url_loader_factory_.pending_requests()->back();
}

std::string ProtobufHttpTestResponder::GetMostRecentRequestUrl() {
  return GetMostRecentPendingRequest().request.url.spec();
}

ProtobufHttpTestResponder::MockInterceptor&
ProtobufHttpTestResponder::GetMockInterceptor() {
  if (!mock_interceptor_) {
    mock_interceptor_ = std::make_unique<MockInterceptor>();
    test_url_loader_factory_.SetInterceptor(mock_interceptor_->Get());
  }
  return *mock_interceptor_;
}

}  // namespace remoting