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#include <vector>
24
27
28namespace cuttlefish {
29
30class FakeHttpClient : public HttpClient {
31 public:
32 using Handler = std::function<HttpResponse<std::string>(const HttpRequest&)>;
33
34 // The longest string that is a complete substring of `url` is used to match
35 // requests.
36 void SetResponse(std::string data, std::string url = "");
37 void SetResponse(HttpResponse<std::string> response, std::string url = "");
38 void SetResponse(Handler handler, std::string url = "");
39 bool RequestMade(std::string_view url) const;
40 // Returns response's status code.
42 HttpRequest request, HttpClient::DataCallback callback) override;
43
44 private:
45 const Handler* FindHandler(std::string_view url) const;
46
47 mutable std::mutex mutex_;
48 std::unordered_map<std::string, Handler> responses_;
49 std::vector<std::string> requested_urls_;
50};
51
52} // namespace cuttlefish
Definition: fake_http_client.h:30
bool RequestMade(std::string_view url) const
Definition: fake_http_client.cc:45
std::mutex mutex_
Definition: fake_http_client.h:47
std::unordered_map< std::string, Handler > responses_
Definition: fake_http_client.h:48
std::function< HttpResponse< std::string >(const HttpRequest &)> Handler
Definition: fake_http_client.h:32
std::vector< std::string > requested_urls_
Definition: fake_http_client.h:49
const Handler * FindHandler(std::string_view url) const
Definition: fake_http_client.cc:61
Result< HttpResponse< void > > DownloadToCallback(HttpRequest request, HttpClient::DataCallback callback) override
Definition: fake_http_client.cc:77
void SetResponse(std::string data, std::string url="")
Definition: fake_http_client.cc:28
Definition: http_client.h:95
std::function< bool(char *, size_t)> DataCallback
Definition: http_client.h:97
#define data
Definition: dict.c:56
Definition: alloc_driver.h:20
tl::expected< T, StackTraceError > Result
Definition: result_type.h:30
Definition: http_client.h:88
Definition: http_client.h:39