Android-cuttlefish cvd tool
fake_http_client.h
Go to the documentation of this file.
1//
2// Copyright (C) 2025 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#pragma once
17
18#include <functional>
19#include <mutex>
20#include <string>
21#include <string_view>
22#include <unordered_map>
23
26
27namespace cuttlefish {
28
29class FakeHttpClient : public HttpClient {
30 public:
31 using Handler = std::function<HttpResponse<std::string>(const HttpRequest&)>;
32
33 // The longest string that is a complete substring of `url` is used to match
34 // requests.
35 void SetResponse(std::string data, std::string url = "");
36 void SetResponse(Handler handler, std::string url = "");
37 // Returns response's status code.
39 HttpRequest request, HttpClient::DataCallback callback) override;
40
41 private:
42 const Handler* FindHandler(std::string_view url) const;
43
44 std::mutex mutex_;
45 std::unordered_map<std::string, Handler> responses_;
46};
47
48} // namespace cuttlefish
Definition: expected.h:86
Definition: fake_http_client.h:29
std::mutex mutex_
Definition: fake_http_client.h:44
std::unordered_map< std::string, Handler > responses_
Definition: fake_http_client.h:45
std::function< HttpResponse< std::string >(const HttpRequest &)> Handler
Definition: fake_http_client.h:31
const Handler * FindHandler(std::string_view url) const
Definition: fake_http_client.cc:44
Result< HttpResponse< void > > DownloadToCallback(HttpRequest request, HttpClient::DataCallback callback) override
Definition: fake_http_client.cc:60
void SetResponse(std::string data, std::string url="")
Definition: fake_http_client.cc:28
Definition: http_client.h:84
std::function< bool(char *, size_t)> DataCallback
Definition: http_client.h:86
char data[Size]
Definition: incremental_server.cpp:1
Definition: alloc_utils.cpp:23
Definition: http_client.h:77