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
|
// 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.
#ifndef REMOTING_BASE_PROTOBUF_HTTP_TEST_RESPONDER_H_
#define REMOTING_BASE_PROTOBUF_HTTP_TEST_RESPONDER_H_
#include <memory>
#include <string>
#include <vector>
#include "base/memory/scoped_refptr.h"
#include "base/test/mock_callback.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
namespace google {
namespace protobuf {
class MessageLite;
} // namespace protobuf
} // namespace google
namespace remoting {
class HttpStatus;
// Helper class to send responses to Protobuf HTTP requests.
class ProtobufHttpTestResponder final {
public:
using MockInterceptor =
base::MockCallback<network::TestURLLoaderFactory::Interceptor>;
ProtobufHttpTestResponder();
~ProtobufHttpTestResponder();
ProtobufHttpTestResponder(const ProtobufHttpTestResponder&) = delete;
ProtobufHttpTestResponder& operator=(const ProtobufHttpTestResponder&) =
delete;
static bool ParseRequestMessage(
const network::ResourceRequest& resource_request,
google::protobuf::MessageLite* out_message);
// Returns the URL loader factory to be used to create the ProtobufHttpClient.
// Note that the returned factory *can't be used* after |this| is deleted.
scoped_refptr<network::SharedURLLoaderFactory> GetUrlLoaderFactory();
// Note that if you have multiple requests with the same URL, all of them will
// be resolved with the same response/error.
void AddResponse(const std::string& url,
const google::protobuf::MessageLite& response_message);
void AddResponse(const std::string& url, const std::string& response);
void AddResponseToMostRecentRequestUrl(
const google::protobuf::MessageLite& response_message);
void AddError(const std::string& url, const HttpStatus& error_status);
void AddErrorToMostRecentRequestUrl(const HttpStatus& error_status);
// Adds response to a pending stream then immediately closes it with |status|.
void AddStreamResponse(
const std::string& url,
const std::vector<const google::protobuf::MessageLite*>& messages,
const HttpStatus& status);
void AddStreamResponseToMostRecentRequestUrl(
const std::vector<const google::protobuf::MessageLite*>& messages,
const HttpStatus& status);
// Gets the most recent request message matching the URL and writes it to
// |out_message|. Returns true if the request message is successfully
// retrieved.
bool GetRequestMessage(const std::string& url,
google::protobuf::MessageLite* out_message);
bool GetMostRecentRequestMessage(google::protobuf::MessageLite* out_message);
// Gets number of pending requests. Unlike
// network::TestURLLoaderFactory::NumPending(), this method also counts
// pending but cancelled requests.
int GetNumPending();
// Returns the PendingRequest instance available at the given index |index|
// (including cancelled requests) or null if not existing.
network::TestURLLoaderFactory::PendingRequest& GetPendingRequest(
size_t index);
network::TestURLLoaderFactory::PendingRequest& GetMostRecentPendingRequest();
std::string GetMostRecentRequestUrl();
// Installs (if called for the first time) and returns a mock interceptor
// callback that you can add expectations:
//
// EXPECT_CALL(test_responder_.GetMockInterceptor(), Run(_))
// .WillOnce([](const ResourceRequest& request) {...});
//
// Note that if you call this without passing it to EXPECT_CALL() or ON_CALL()
// then the test will fail immediately as long as any request is sent.
MockInterceptor& GetMockInterceptor();
private:
network::TestURLLoaderFactory test_url_loader_factory_;
std::unique_ptr<MockInterceptor> mock_interceptor_;
};
} // namespace remoting
#endif // REMOTING_BASE_PROTOBUF_HTTP_TEST_RESPONDER_H_
|